为了账号安全,请及时绑定邮箱和手机立即绑定

argparse:在命令“帮助”中设置选项 arg 的名称

argparse:在命令“帮助”中设置选项 arg 的名称

狐的传说 2022-06-02 10:17:18
在 argparse 中,可以使用如下代码创建选择参数:parser = argparse.ArgumentParser()parser.add_argument("action", type=str,                    help="The action to do. Eligible values:\ninstall, remove, version", choices=['install', 'remove', 'version'])什么时候parser是一个实例argparse.ArgumentParser()但是,在显示帮助时,不是将 arg 指定为其名称,而是将其指定为{install,remove,version},整个输出为positional arguments:  {install,remove,version}                        The action to do. Eligible values: install, remove,                        versionoptional arguments:  -h, --help            show this help message and exit我怎样才能让他显示 arg 的名称,所以输出更像positional arguments:  action                The action to do. Eligible values: install, remove,                        versionoptional arguments:  -h, --help            show this help message and exit
查看完整描述

2 回答

?
桃花长相依

TA贡献1860条经验 获得超8个赞

metavar参数 toadd_argument是您正在寻找的:


parser = argparse.ArgumentParser()

parser.add_argument(

    "action",

    type=str,

    help="The action to do. Eligible values:\ninstall, remove, version",

    choices=['install', 'remove', 'version'],

    metavar="action",

)

调用parser.print_help()收益率:


usage: [-h] action


positional arguments:

  action      The action to do. Eligible values: install, remove, version


optional arguments:

  -h, --help  show this help message and exit


查看完整回答
反对 回复 2022-06-02
?
holdtom

TA贡献1805条经验 获得超10个赞

您可以指定元变量


当 ArgumentParser 生成帮助消息时,它需要某种方式来引用每个预期的参数。默认情况下,[...] 可以使用 metavar 指定替代名称:


parser = argparse.ArgumentParser()

parser.add_argument("action", type=str, metavar='action',

                    help="The action to do. Eligible values:\ninstall, remove, version", choices=['install', 'remove', 'version'])



查看完整回答
反对 回复 2022-06-02
  • 2 回答
  • 0 关注
  • 117 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号