Action=store_true usage in Python argparse
preamble
Python's Command Line Parameter Parsing Module Learning.
typical example
The parameter parsing module supports the action parameter, and this parameter can be set to 'store_true', 'store_false', 'store_const', and so on.
For example, the following line of code indicates that PARAM_NAME is set to True if "-PARAM_NAME" is present in the command line arguments, otherwise it is False.
parser.add_argument("--PARAM_NAME", action="store_true", help="HELP_INFO")
official document
‘store_true’ and ‘store_false’ - These are special cases of ‘store_const’ used for storing the values True and False respectively. In addition, they create default values of False and True respectively. For example:
'store_true' and 'store_false' - these are special cases of 'store_const', which are used to set True In addition, they create default values.
>>> parser = () >>> parser.add_argument('--foo', action='store_true') >>> parser.add_argument('--bar', action='store_false') >>> parser.add_argument('--baz', action='store_false') >>> parser.parse_args('--foo --bar'.split()) Namespace(foo=True, bar=False, baz=True)
Learn a little more.
customizable
You can do this by giving aAction
subclasses or other objects that implement the same interface to specify an arbitraryaction
。BooleanOptionalAction
It is the one that can be usedaction
It addsBoolean action
features, support for--foo
cap (a poem)--no-foo
The form of the
>>> import argparse >>> parser = () >>> parser.add_argument('--foo', action=) >>> parser.parse_args(['--no-foo']) Namespace(foo=False)
wrap-up
'--foo', action='store_true'
, which can be easily implemented with boolean-type parameters.
reflections
Starting with Python 3, many of the built-in modules moved to an object-oriented paradigm.
For those who started using Python in the early days, the code they saw was more procedural or functional in style, for example, you can see a lot of Python's code style in some of Google's open source projects.
Addendum: Usage of the optional parameter setting action='store_true' in the python library Argparse
store_true is true with a triggered action and false without.
Generally speaking, it means to run the program with or without parameters, see the example to understand.
I. No default
import argparse parser = (description='') parser.add_argument('--cuda', type=bool, default=True, help='use cuda') parser.add_argument('--cpu',action='store_true',help='use cpu') args = parser.parse_args() print("cuda: ",) print("cpu: ",)
If you run the command: python
Then the output is:
cuda: True
cpu: False
If you run the command: python --cpu
Then the output is:
cuda: True
cpu: True
Second, there is the DEFAULT
Of course 'store_true' can also be set to default, although that looks weird and doesn't work well. E.g.:
parser.add_argument('--cpu',default=True,action='store_true',help='use cpu') print("cpu: ",)
If you run the program with default=True with or without " --cpu ", the output will be cpu: True.
But default=False is a different story:
parser.add_argument('--cpu',default=False,action='store_true',help='use cpu') print("cpu: ",)
If you run python, the output is cpu: False.
If you run the command python --cpu, the output cpu: True
to this article on Python argparse action=store_true summary of the use of the article is introduced to this, more related Python argparse action=store_true content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!