I want to have some options in argparse module such as --pm-export
however when I try to use it like args.pm-export
I get the error that there is not attribute pm
. How can I get around this issue? Is it possible to have -
in command line options?

- 143,130
- 81
- 406
- 459

- 18,062
- 27
- 111
- 169
6 Answers
As indicated in the argparse
docs:
For optional argument actions, the value of dest is normally inferred from the option strings. ArgumentParser generates the value of dest by taking the first long option string and stripping away the initial
--
string. Any internal-
characters will be converted to_
characters to make sure the string is a valid attribute name
So you should be using args.pm_export
.

- 17,348
- 7
- 41
- 43

- 53,284
- 11
- 113
- 116
Unfortunately, dash-to-underscore replacement doesn't work for positional arguments
(not prefixed by --
).
E.g:
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('logs-dir',
help='Directory with .log and .log.gz files')
parser.add_argument('results-csv', type=argparse.FileType('w'),
default=sys.stdout,
help='Output .csv filename')
args = parser.parse_args()
print args
# gives
# Namespace(logs-dir='./', results-csv=<open file 'lool.csv', mode 'w' at 0x9020650>)
So, you should use 1'st argument to add_argument()
as attribute name and metavar
kwarg to set how it should look in help:
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('logs_dir', metavar='logs-dir',
nargs=1,
help='Directory with .log and .log.gz files')
parser.add_argument('results_csv', metavar='results-csv',
nargs=1,
type=argparse.FileType('w'),
default=sys.stdout,
help='Output .csv filename')
args = parser.parse_args()
print args
# gives
# Namespace(logs_dir=['./'], results_csv=[<open file 'lool.csv', mode 'w' at 0xb71385f8>])
-
12The relevant Python bug report: http://bugs.python.org/issue15125. `argparse` uses `setattr` and `getattr`, so `dest` names do not have to be valid `dot` attribute names. The user can also use `getattr`. – hpaulj Nov 27 '13 at 23:23
-
7I'd just do `positional_arg = getattr( parser, 'positional-arg' )` – Ciro Santilli OurBigBook.com Aug 05 '16 at 09:28
-
I think it's: `positional_arg = getattr(args, 'positional-arg')` For me at least. – kurczynski Aug 10 '22 at 23:36
Dashes are converted to underscores:
import argparse
pa = argparse.ArgumentParser()
pa.add_argument('--foo-bar')
args = pa.parse_args(['--foo-bar', '24'])
print args # Namespace(foo_bar='24')

- 211,518
- 52
- 313
- 390
-
12Only for optional arguments. Required ones to keep their hyphens, so require an underscore to make them accessible. – n00dle May 16 '17 at 17:22
-
I have found this to not be the case `parser.add_argument("-c", "--node-config", help = "configuration to apply to the node", required = True, default = "")` `if argument.node_config: nodeConfig = argument.node_config print ("node_config = "+nodeConfig)` produces `node_config = node config value` – NitrusCS Aug 14 '23 at 13:14
Concise and explicit but probably not always acceptable way would be to use vars()
:
#!/usr/bin/env python3
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('a-b')
args = vars(parser.parse_args())
print(args['a-b'])

- 505
- 4
- 10
getattr(args, 'positional-arg')
This is another OK workaround for positional arguments:
#!/usr/bin/env python3
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('a-b')
args = parser.parse_args(['123'])
assert getattr(args, 'a-b') == '123'
Tested on Python 3.8.2.

- 347,512
- 102
- 1,199
- 985
-
This will not pass if `a-b` is not a positional argument, which is required by the question. – taper Dec 30 '20 at 16:23
I guess the last option is to change shorten option -a
to --a
import argparse
parser = argparse.ArgumentParser(description="Help")
parser.add_argument("--a", "--argument-option", metavar="", help="") # change here
args = parser.parse_args()
option = args.a # And here
print(option)

- 23
- 7