242

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?

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Cemre Mengü
  • 18,062
  • 27
  • 111
  • 169

6 Answers6

356

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.

ron rothman
  • 17,348
  • 7
  • 41
  • 43
Thomas Orozco
  • 53,284
  • 11
  • 113
  • 116
157

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>])
Good Pen
  • 625
  • 6
  • 10
seriyPS
  • 6,817
  • 2
  • 25
  • 16
27

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')
georg
  • 211,518
  • 52
  • 313
  • 390
  • 12
    Only 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
4

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'])
Denis The Menace
  • 505
  • 4
  • 10
3

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.

Ciro Santilli OurBigBook.com
  • 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
0

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)
Dang Max
  • 23
  • 7