1

I am trying to do this:

$ myfile.py config --user 'abc1' --password 'def1' --configname 'first' config config --user 'abc2' --password 'def2' --configname 'second'

config is a subcommand (as described in argparse.ArgumentParser documentation adding subcommands in argparse).

This should allow me to collect individual confignames, username and password for any number of configs (in this case 2) from commandline.

These I will persist as

$ cat first
[user]
abc1

[password]
def1

$ cat second
[user]
abc2

[password]
def2

When I tried this I got

error: unrecognized arguments: 

How can I achieve similar functionality.

Here is my code:

>>> import argparse
>>> parser = argparse.ArgumentParser(prog='PROG')
>>> subparsers = parser.add_subparsers(help='sub-command help')
>>> # create the parser for the "a" command
... parser_a = subparsers.add_parser('config', help='a help')
>>> parser_a.add_argument('--user', required=True)
_StoreAction(option_strings=['--user'], dest='user', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)
>>> parser_a.add_argument('--password', required=True)
_StoreAction(option_strings=['--password'], dest='password', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)
>>> parser_a.add_argument('--configname', required=True)
_StoreAction(option_strings=['--configname'], dest='configname', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)
>>> parser.parse_args(['config', '--user', 'abc1', '--password', 'def1', '--configname', 'first'])
Namespace(configname='first', password='def1', user='abc1')
>>> 
>>> 
>>> 
>>> import argparse
>>> parser = argparse.ArgumentParser(prog='PROG')
>>> subparsers = parser.add_subparsers(help='sub-command help')
>>> # create the parser for the "a" command
... parser_a = subparsers.add_parser('config', help='a help')
>>> parser_a.add_argument('--user', required=True)
_StoreAction(option_strings=['--user'], dest='user', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)
>>> parser_a.add_argument('--password', required=True)
_StoreAction(option_strings=['--password'], dest='password', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)
>>> parser_a.add_argument('--configname', required=True)
_StoreAction(option_strings=['--configname'], dest='configname', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)
>>> parser.parse_args(['config', '--user', 'abc1', '--password', 'def1', '--configname', 'first', 'config', '--user', 'abc2', '--password', 'def2', '--configname', 'second'])
usage: PROG [-h] {config} ...
PROG: error: unrecognized arguments: config
Ankur Agarwal
  • 23,692
  • 41
  • 137
  • 208
  • please post the code of `myfile.py` – yash May 13 '18 at 03:01
  • @yash please see above. – Ankur Agarwal May 13 '18 at 03:25
  • do you need to save just two config? – yash May 13 '18 at 03:43
  • @yash I'd like to be able to save n configs but I am only showing 2 as an example. – Ankur Agarwal May 13 '18 at 03:56
  • With `append` action you can provide multiple Flagged arguments, but positionals, including subparsers can only be used once. And you can't define multiple subparsers. The duplicate has many answers suggesting ways around this limitation. Also: [Argparse: parse multiple subcommands](https://stackoverflow.com/questions/39644246/argparse-parse-multiple-subcommands) – hpaulj May 13 '18 at 06:14
  • https://stackoverflow.com/questions/48013977/cannot-have-multiple-subparser-arguments/48014197#48014197; https://stackoverflow.com/questions/28477917/with-argparse-are-subparsers-inherently-mutually-exclusive – hpaulj May 13 '18 at 06:24

0 Answers0