Basic usage of add_argument in python library argparse

argparse is Python’s own command line parameter parsing package, which can be used to read easily Get command line parameters. It's also relatively simple to use.
When your code needs to frequently modify parameters, use this tool to separate parameters and code, making your code more concise and applicable to a wider range of applications.

  • name or flags - a name or list of option strings, such as foo or -f, --foo.
  • action - the action when the command line encounters a parameter. The default value is store.
  • store_const means that the assignment is const;
  • append, stores the encountered values ​​into a list, that is, if the parameters are repeated, multiple values ​​will be saved;
  • append_const, saves a value defined in the parameter specification to a list;
  • count, stores the number of encounters; in addition, you can also inherit argparse.Action to customize parameter parsing;
  • nargs - the number of command line parameters that should be read, which can be a specific number, or a ? sign. When no value is specified, use default for Positional argument and const for Optional argument; or * sign, indicating 0 or more Parameters; or the + sign indicates 1 or more parameters.
  • const - the constant value required by action and nargs.
  • default - the default value when no parameters are specified.
  • type - the type that the command line arguments should be converted to.
  • choices - a container of allowed values ​​for the parameter.
  • required - whether the optional parameter can be omitted (optional parameters only).
  • help - the help information of the parameter. When specified as argparse.SUPPRESS, it means that the help information of the parameter is not displayed.
  • metavar - the parameter name in the usage description. For required parameters, it defaults to the parameter name. For optional parameters, it defaults to all uppercase parameter names.
  • dest - the parsed parameter name, by default the longest name is taken for optional parameters, and dashes are converted to underscores.

eg:

import argparse

parser = argparse.ArgumentParser(description="Demo of argparse") #创建一个 ArgumentParser 对象
# parser.add_argument('-n','--name', default=' Li ')
parser.add_argument('-y','--year', default='20')#调用 add_argument() 方法添加参数
parser.add_argument('-d', '--distributed', default='wdef',action='store_true', dest='distributed', help='Use distributed training')
parser.add_argument('--local_rank', dest='local_rank', default=0, type=int, help='Use distributed training')
parser.add_argument('-name', required=False)
parser.add_argument('--inT',default='89',type=int)
args = parser.parse_args()
print(args)
name = args.name
year = args.year
print('Hello {}  {}'.format(name,year))
print(args.distributed)
print(args.name)
print(args.inT)

Insert image description here
In fact, argparse is a parameter manager, which can better allow others to understand your Code

Hope this is useful to you!
Thank you for your likes and comments!

Guess you like

Origin blog.csdn.net/qq_44936246/article/details/120454203