Issue
I created an argparse.ArgumentParser
and added specific arguments like:
parser.add_argument('-i', action='store', dest='i', default='i.log')
parser.add_argument('-o', action='store', dest='o', default='o.log')
But I want to be able to accept any arguments, not just the ones for which I explicitly specified the semantics.
How can I make it so that any remaining command-line arguments are gathered together and I can access them after parsing?
Solution
Another option is to add a positional argument to your parser. Specify the option without leading dashes, and argparse
will look for them when no other option is recognized.
This has the added benefit of improving the help text for the command:
>>> parser.add_argument('otherthings', nargs='*')
>>> parser.parse_args(['foo', 'bar', 'baz'])
Namespace(i='i.log', o='o.log', otherthings=['foo', 'bar', 'baz'])
and
>>> print parser.format_help()
usage: ipython-script.py [-h] [-i I] [-o O] [otherthings [otherthings ...]]
positional arguments:
otherthings
optional arguments:
-h, --help show this help message and exit
-i I
-o O
Answered By - SingleNegationElimination
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.