What is the Difference between nargs='*'
and nargs='+'
?
As they both accept one or more arguments.
parser.add_argument('nums',narg='*')
And
parser.add_argument('nums',narg='+')
argparse
is following the common usage in re
syntax. quoting from https://docs.python.org/3/library/re.html
* Causes the resulting RE to match 0 or more repetitions of the preceding RE, as many repetitions as are possible. ab* will match 'a', 'ab', or 'a' followed by any number of 'b's.
+ Causes the resulting RE to match 1 or more repetitions of the preceding RE. ab+ will match 'a' followed by any non-zero number of 'b's; it will not match just 'a'.
? Causes the resulting RE to match 0 or 1 repetitions of the preceding RE. ab? will match either 'a' or 'ab'.
https://docs.python.org/3/library/argparse.html#nargs
You can also use argparse
constants, which have the same string values
argparse.ONE_OR_MORE
argparse.ZERO_OR_MORE
argparse.OPTIONAL
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.