argparse-命令行解析模块

官方文档 https://docs.python.org/3/library/argparse.html

http://www.cnblogs.com/lovemyspring/p/3214598.html

ArgumentParser

ArgumentParser objects
https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser

1
class argparse.ArgumentParser(prog=None, usage=None, description=None, epilog=None, parents=[], formatter_class=argparse.HelpFormatter, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True, allow_abbrev=True)

Create a new ArgumentParser object. All parameters should be passed as keyword arguments. Each parameter has its own more detailed description below, but in short they are:

  • prog - The name of the program (default: sys.argv[0])
  • usage - The string describing the program usage (default: generated from arguments added to parser)
  • description - Text to display before the argument help (default: none)
  • epilog - Text to display after the argument help (default: none)
  • parents - A list of ArgumentParser objects whose arguments should also be included
  • formatter_class - A class for customizing the help output
  • prefix_chars - The set of characters that prefix optional arguments (default: ‘-‘)
  • fromfile_prefix_chars - The set of characters that prefix files from which additional arguments should be read (default: None)
  • argument_default - The global default value for arguments (default: None)
  • conflict_handler - The strategy for resolving conflicting optionals (usually unnecessary)
  • add_help - Add a -h/—help option to the parser (default: True)
  • allow_abbrev - Allows long options to be abbreviated if the abbreviation is unambiguous. (default: True)

add_argument

https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.add_argument

1
2
ArgumentParser.add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])
Define how a single command-line argument should be parsed. Each parameter has its own more detailed description below, but in short they are:

  • name or flags - Either a name or a list of option strings, e.g. foo or -f, —foo.
  • action - The basic type of action to be taken when this argument is encountered at the command line.
  • nargs - The number of command-line arguments that should be consumed.
  • const - A constant value required by some action and nargs selections.
  • default - The value produced if the argument is absent from the command line.
  • type - The type to which the command-line argument should be converted.
  • choices - A container of the allowable values for the argument.
  • required - Whether or not the command-line option may be omitted (optionals only).
  • help - A brief description of what the argument does.
  • metavar - A name for the argument in usage messages.
  • dest - The name of the attribute to be added to the object returned by parse_args().

最常用法示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

import argparse

parser = argparse.ArgumentParser(description="PyTorch Detection")

parser.add_argument(
"--config-file", # 这里虽然是 config-file, 但需要用 args.config_file来访问, 但是不能用 args.config-file 访问.
dest="cfg_file", # 可以用 args.cfg_file 来访问该参数, 比 args.confi-file简洁
type=str, # 类型
default="", # 参数的默认值
choices=["option1", "option2"] # 只能选择其一
metavar="FILE", # 可以在命令行中替代 --config-file
help="path to config file", # 帮助信息
nargs=1, # 参数的个数, 默认为1
)
1
2
3
4
5
parser.add_argument(
"opts", # 前面没有'--'
default=None,
nargs=argparse.REMAINDER # 这一行不能少, 否则会报错
)