1、创建解析器对象
可以调用argparse的ArgumentParser()创建解析器对象,如下,
parser = argparse.ArgumentParser(description='ArgumentParser-description')
description:使用 description=
关键字参数,简要描述这个程序的相关帮助信息。在帮助消息中,这个描述会显示在命令行用法字符串和各种参数的帮助消息之间。
2、使用add_argument()添加参数
调用解析器对象的add_argument() 方法添加参数,也就是通过命令调用程序需要使用到的参数。
ArgumentParser.add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])
参数说明:
参数 | 描述 |
name 或 flags | 选项字符串的名字或者列表,例如 foo 或者 |
action | 命令行遇到参数时的动作,默认值是 store。 store_const,表示赋值为const; append,将遇到的值存储成列表,也就是如果参数重复则会保存多个值; append_const,将参数规范中定义的一个值保存到一个列表; |
nargs | 应该读取的命令行参数个数,可以是具体的数字,或者是?号, 当不指定值时对于 Positional argument 使用 default, 对于 Optional argument 使用 const;或者是 * 号, |
const | action 和 nargs 所需要的常量值。 |
default | 不指定参数时的默认值。 |
type | 命令行参数应该被转换成的类型。 |
choices | 参数可允许的值的一个容器。 |
required | 可选参数是否可以省略 (仅针对可选参数)。 |
help | 参数的帮助信息,当指定为 argparse.SUPPRESS 时表示不显示该参数的帮助信息. |
metavar | 在 usage 说明中的参数名称,对于必选参数默认就是参数名称,对于可选参数默认是全大写的参数名称. |
dest | 解析后的参数名称,默认情况下,对于可选参数选取最长的名称,中划线转换为下划线. |
使用示例:
ap = argparse.ArgumentParser() ap.add_argument( "-m", "--model", default="u2net", type=str, choices=model_choices, help="The model name, u2net, u2netp, u2net_human_seg", ) ap.add_argument( "-a", "--alpha-matting", nargs="?", const=True, default=False, type=lambda x: bool(strtobool(x)), help="When true use alpha matting cutout.", ) ap.add_argument( "-af", "--alpha-matting-foreground-threshold", default=240, type=int, help="The trimap foreground threshold.", ) ap.add_argument( "-ab", "--alpha-matting-background-threshold", default=10, type=int, help="The trimap background threshold.", ) ap.add_argument( "-ae", "--alpha-matting-erode-size", default=10, type=int, help="Size of element used for the erosion.", ) ap.add_argument( "-az", "--alpha-matting-base-size", default=1000, type=int, help="The image base size.", ) ap.add_argument( "-wn", "--workernodes", default=1, type=int, help="Number of parallel workers" ) ap.add_argument( "-gb", "--gpubatchsize", default=2, type=int, help="GPU batchsize" ) ap.add_argument( "-fr", "--framerate", default=-1, type=int, help="Override the frame rate" ) ap.add_argument( "-fl", "--framelimit", default=-1, type=int, help="Limit the number of frames to process for quick testing.", ) ap.add_argument( "-mk", "--mattekey", nargs="?", const=True, default=False, type=lambda x: bool(strtobool(x)), help="Output the Matte key file", ) ap.add_argument( "-tv", "--transparentvideo", nargs="?", const=True, default=False, type=lambda x: bool(strtobool(x)), help="Output transparent video format mov", ) ap.add_argument( "-tov", "--transparentvideoovervideo", nargs="?", const=True, default=False, type=lambda x: bool(strtobool(x)), help="Overlay transparent video over another video", ) ap.add_argument( "-toi", "--transparentvideooverimage", nargs="?", const=True, default=False, type=lambda x: bool(strtobool(x)), help="Overlay transparent video over another image", ) ap.add_argument( "-tg", "--transparentgif", nargs="?", const=True, default=False, type=lambda x: bool(strtobool(x)), help="Make transparent gif from video", )
3、解析参数
通过调用解析器对象的 parse_args() 方法解析参数。则就可以获取到通过命令行传入的参数。具体使用如下,
import argparse from distutils.util import strtobool parser = argparse.ArgumentParser(description='ArgumentParser-description') parser.add_argument( "-m", "--model", default="u2net", type=str, #choices=model_choices, help="The model name, u2net, u2netp, u2net_human_seg", ) parser.add_argument( "-a", "--alpha-matting", nargs="?", const=True, default=False, type=lambda x: bool(strtobool(x)), help="When true use alpha matting cutout.", ) parser.add_argument( "-af", "--alpha-matting-foreground-threshold", default=240, type=int, help="The trimap foreground threshold.", ) parser.add_argument( "-ab", "--alpha-matting-background-threshold", default=10, type=int, help="The trimap background threshold.", ) parser.add_argument( "-ae", "--alpha-matting-erode-size", default=10, type=int, help="Size of element used for the erosion.", ) parser.add_argument( "-az", "--alpha-matting-base-size", default=1000, type=int, help="The image base size.", ) parser.add_argument( "-wn", "--workernodes", default=1, type=int, help="Number of parallel workers" ) parser.add_argument( "-gb", "--gpubatchsize", default=2, type=int, help="GPU batchsize" ) parser.add_argument( "-fr", "--framerate", default=-1, type=int, help="Override the frame rate" ) parser.add_argument( "-fl", "--framelimit", default=-1, type=int, help="Limit the number of frames to process for quick testing.", ) parser.add_argument( "-mk", "--mattekey", nargs="?", const=True, default=False, type=lambda x: bool(strtobool(x)), help="Output the Matte key file", ) parser.add_argument( "-tv", "--transparentvideo", nargs="?", const=True, default=False, type=lambda x: bool(strtobool(x)), help="Output transparent video format mov", ) parser.add_argument( "-tov", "--transparentvideoovervideo", nargs="?", const=True, default=False, type=lambda x: bool(strtobool(x)), help="Overlay transparent video over another video", ) parser.add_argument( "-toi", "--transparentvideooverimage", nargs="?", const=True, default=False, type=lambda x: bool(strtobool(x)), help="Overlay transparent video over another image", ) #调用parse_args()解析参数 args = parser.parse_args() print(args.model) print(args.alpha_matting_base_size) print(args.mattekey)
4、显示帮助文档
python3 main.py -h usage: main.py [-h] [-m MODEL] [-a [ALPHA_MATTING]] [-af ALPHA_MATTING_FOREGROUND_THRESHOLD] [-ab ALPHA_MATTING_BACKGROUND_THRESHOLD] [-ae ALPHA_MATTING_ERODE_SIZE] [-az ALPHA_MATTING_BASE_SIZE] [-wn WORKERNODES] [-gb GPUBATCHSIZE] [-fr FRAMERATE] [-fl FRAMELIMIT] [-mk [MATTEKEY]] [-tv [TRANSPARENTVIDEO]] [-tov [TRANSPARENTVIDEOOVERVIDEO]] [-toi [TRANSPARENTVIDEOOVERIMAGE]] ArgumentParser-description optional arguments: -h, --help show this help message and exit -m MODEL, --model MODEL The model name, u2net, u2netp, u2net_human_seg -a [ALPHA_MATTING], --alpha-matting [ALPHA_MATTING] When true use alpha matting cutout. -af ALPHA_MATTING_FOREGROUND_THRESHOLD, --alpha-matting-foreground-threshold A LPHA_MATTING_FOREGROUND_THRESHOLD The trimap foreground threshold. -ab ALPHA_MATTING_BACKGROUND_THRESHOLD, --alpha-matting-background-threshold A LPHA_MATTING_BACKGROUND_THRESHOLD The trimap background threshold. -ae ALPHA_MATTING_ERODE_SIZE, --alpha-matting-erode-size ALPHA_MATTING_ERODE_S IZE Size of element used for the erosion. -az ALPHA_MATTING_BASE_SIZE, --alpha-matting-base-size ALPHA_MATTING_BASE_SIZE The image base size. -wn WORKERNODES, --workernodes WORKERNODES Number of parallel workers -gb GPUBATCHSIZE, --gpubatchsize GPUBATCHSIZE GPU batchsize -fr FRAMERATE, --framerate FRAMERATE Override the frame rate -fl FRAMELIMIT, --framelimit FRAMELIMIT Limit the number of frames to process for quick testing. -mk [MATTEKEY], --mattekey [MATTEKEY] Output the Matte key file -tv [TRANSPARENTVIDEO], --transparentvideo [TRANSPARENTVIDEO] Output transparent video format mov -tov [TRANSPARENTVIDEOOVERVIDEO], --transparentvideoovervideo [TRANSPARENTVIDE OOVERVIDEO] Overlay transparent video over another video -toi [TRANSPARENTVIDEOOVERIMAGE], --transparentvideooverimage [TRANSPARENTVIDE OOVERIMAGE] Overlay transparent video over another image
注意:ArgumentParser-description就是parser = argparse.ArgumentParser(description='ArgumentParser-description')
中指定的描述信息。