Source From Here
Question
I am trying to pass a list as an argument to a command line program. Is there an argparse option to pass a list as option?
How-To
Use the nargs option or the append option (depending on how you want the user interface to behave).
nargs: The number of command-line arguments that should be consumed.
For example:
- test.py
Execution output:
action(append): This stores a list, and appends each argument value to the list.
For example:
- test_append.py
Execution output:
Notes. Don't use type=list!!! - There is probably no situation where you would want to use type=list with argparse. Ever.
Let's take a look in more detail at some of the different ways one might try to do this, and the end result.
- test_all.py
Here is the output you can expect:
Takeaways:
- Use nargs or action='append'
- Don't use type=list, as it will return a list of lists
Question
I am trying to pass a list as an argument to a command line program. Is there an argparse option to pass a list as option?
How-To
Use the nargs option or the append option (depending on how you want the user interface to behave).
nargs: The number of command-line arguments that should be consumed.
For example:
- test.py
- #!/usr/bin/env python3
- import argparse
- parser = argparse.ArgumentParser()
- # nargs='+' takes 1 or more arguments, nargs='*' takes zero or more.
- parser.add_argument('-l','--list', nargs='+', help='
Set flag' , required=True) - args = parser.parse_args()
- print("Given list: {}".format(args.list))
action(append): This stores a list, and appends each argument value to the list.
For example:
- test_append.py
- #!/usr/bin/env python3
- import argparse
- parser = argparse.ArgumentParser()
- # This is useful to allow an option to be specified multiple times.
- parser.add_argument('-l','--list', action='append', help='
Set flag' , required=True) - args = parser.parse_args()
- print("Given list: {}".format(args.list))
Notes. Don't use type=list!!! - There is probably no situation where you would want to use type=list with argparse. Ever.
Let's take a look in more detail at some of the different ways one might try to do this, and the end result.
- test_all.py
- #!/usr/bin/env python3
- import argparse
- parser = argparse.ArgumentParser()
- # By default it will fail with multiple arguments.
- parser.add_argument('--default')
- # Telling the type to be a list will also fail for multiple arguments,
- # but give incorrect results for a single argument.
- parser.add_argument('--list-type', type=list)
- # This will allow you to provide multiple arguments, but you will get
- # a list of lists which is not desired.
- parser.add_argument('--list-type-nargs', type=list, nargs='+')
- # This is the correct way to handle accepting multiple arguments.
- # '+' == 1 or more.
- # '*' == 0 or more.
- # '?' == 0 or 1.
- # An int is an explicit number of arguments to accept.
- parser.add_argument('--nargs', nargs='+')
- # To make the input integers
- parser.add_argument('--nargs-int-type', nargs='+', type=int)
- # An alternate way to accept multiple inputs, but you must
- # provide the flag once per input. Of course, you can use
- # type=int here if you want.
- parser.add_argument('--append-action', action='append')
- # To show the results of the given option to screen.
- for _, value in parser.parse_args()._get_kwargs():
- if value is not None:
- print(value)
Takeaways:
- Use nargs or action='append'
- Don't use type=list, as it will return a list of lists
沒有留言:
張貼留言