python argparse named positional arguments? -


is there way make python's argparse.argumentparser treat command line arguments way python functions treat arguments? arguments can passed without name?

see example "integers" in documentation. don't include hyphens , argument treated positional argument.

>>> parser = argparse.argumentparser() >>> parser.add_argument('first_supplied_argument', help='help') >>> parser.add_argument('second_supplied_argument', help='help') >>> args = parser.parse_args(['1', '2']) namespace(first_supplied_argument='1', second_supplied_argument='2') 

edit based on comment:

are able supply both positional , optional arguments? think still need supply @ least 1 positional argument.

parser = argparse.argumentparser() parser.add_argument('--first', help='help') parser.add_argument('first', nargs='?', help='help') parser.add_argument('--second', help='help') parser.add_argument('second', nargs='?', help='help')  print parser.parse_args(['1', '2']) print parser.parse_args(['1', '--second', '2']) print parser.parse_args(['--first', '1', '--second', '2'])  # doesn't work print parser.parse_args(['', '--first', '1', '--second', '2'])  # not want 

output:

namespace(first='1', second='2') namespace(first='1', second='2') namespace(first=none, second=none)  # doesn't work namespace(first='1', second='2') 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -