Python Argparse append with nargs= >1 -
so trying create parser can take arbitrary list of choices (things plot), optional parameters specified each choice. , have eschewed idea of sub-subparsers using multiple arguments of form:
--plot foo --xlim 0 10 --ylim -5 5 --plot bar --xlim -5 5 --ylim 0 10 --clim 1e24 1e27
but can't seem append action work nargs=2:
>>> import argparse >>> parser = argparse.argumentparser() >>> parser.add_argument('--foo', action='append', nargs=2, type=int) _appendaction(option_strings=['--foo'], dest='foo', nargs=2, const=none, default=none, type=<type 'int'>, choices=none, help=none, metavar=none) >>> parser.parse_args("--foo 1 2 --foo 3 4") usage: [-h] [--foo foo foo] : error: unrecognized arguments: - - f o o 1 2 - - f o o 3 4
i hoping list of lists or list of tuples. is desired behaviour unsupported argparse or doing wrong in implementation?
you should pass list of strings, not string:
>>> import argparse >>> parser = argparse.argumentparser() >>> parser.add_argument('--foo', action='append', nargs=2, type=int) _appendaction(option_strings=['--foo'], dest='foo', nargs=2, const=none, default=none, type=<type 'int'>, choices=none, help=none, metavar=none) >>> parser.parse_args(["--foo", "1", "2", "--foo", "3", "4"]) # <---- namespace(foo=[[1, 2], [3, 4]])
Comments
Post a Comment