$ ls ~yifei/notes/

Python Argparse 库的使用

Posted on:

Last modified:


import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--verbosity", help="increase output verbosity")
args = parser.parse_args()

两种不同的参数模式, positionaloptional arguments,感觉之间的区别有点像 args 和 kwargs


Actually, the argparse module is not ok with subcommand, mannually parse the first command and then pass the rest to argparse

add_argument 的参数

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. store/store_const/store_true/append/count
nargs - The number of command-line arguments that should be consumed. N/?/*/+
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. a list 
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().


尝试使用下 fire: https://github.com/google/python-fire

© 2016-2022 Yifei Kong. Powered by ynotes

All contents are under the CC-BY-NC-SA license, if not otherwise specified.

Opinions expressed here are solely my own and do not express the views or opinions of my employer.