美文网首页
极好用的Python命令行参数解析工具包:argparse

极好用的Python命令行参数解析工具包:argparse

作者: LabVIEW_Python | 来源:发表于2021-06-26 11:28 被阅读0次

什么是argparse? argparse是Python原生自带的用于解析命令行参数的工具包,它可以帮助用户快速的编写用户友好的命令行界面。

基本来说,argparse的用法只需要按照下面的范例程序来将参数修改自己所需即可:

# Demo how to use argparse
import sys
from argparse import ArgumentParser, SUPPRESS 

def build_argparser():
    parser = ArgumentParser(add_help=False)
    args = parser.add_argument_group("Options")
    args.add_argument('-h', '--help', action='help', default=SUPPRESS, help='Show this help message and exit.')
    args.add_argument("-m", "--model", help="Required. Path to an .xml or .onnx file with a trained model.",
                      required=True, type=str)
    args.add_argument("-d", "--device",
                      help="Optional. Specify the target device to infer on; "
                           "CPU, GPU, FPGA or MYRIAD is acceptable. "
                           "Sample will look for a suitable plugin for device specified (CPU by default)",
                      default="CPU", type=str)
    
    args.add_argument("-bs", "--batch_size", help="Optional. Set the batch size", default=1, type=int)


    return parser

def main():
    
    args = build_argparser().parse_args()
    print(f"Type of args:{type(args)}")
    print(f"args.model: {args.model},{type(args.model)}")
    print(f"args.device: {args.device},{type(args.device)}")
    print(f"args.batch_size: {args.batch_size},{type(args.batch_size)}")

if __name__ == '__main__':
    sys.exit(main() or 0)
上述程序的运行结果: 运行结果

相关文章

网友评论

      本文标题:极好用的Python命令行参数解析工具包:argparse

      本文链接:https://www.haomeiwen.com/subject/swtzyltx.html