栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将“帮助”移至python argparse中的其他参数组

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将“帮助”移至python argparse中的其他参数组

您可以使用

add_help=False
禁用内置帮助命令并添加自己的命令
action="help"
(感谢@mgilson!)

要摆脱空格,请勿将其设置

metavar
为空字符串。您应该使用以下选项来指定您的选项,
action="store_true"
以使它们成为真实的(无参数的)选项:

import argparseparser = argparse.ArgumentParser(prog="LS.py",usage="%(prog)s [options] [paths...]nThe paths are optional; if not given . is used.",add_help=False)group = parser.add_argument_group("Options")group.add_argument("-h", "--help", action="help", help="show this help message and exit")group.add_argument("-path", default=".", help=argparse.SUPPRESS)group.add_argument("-m", "--modified", action="store_true",         help="show last modified date/time")group.add_argument("-o", "--order", nargs=1, default="name",         help="sort order (n[ame], m[odified], s[ize])n[default: name]")group.add_argument("-r", "--recursive", action="store_true",         help="recurse into subdirectories")group.add_argument("-s", "--sizes", action="store_true",        help="show sizes")args = parser.parse_args()

输出:

Options:  -h, --help show this help message and exit  -m, --modified        show last modified date/time  -o ORDER, --order ORDER  sort order (n[ame], m[odified], s[ize]) [default:  name]  -r, --recursive       recurse into subdirectories  -s, --sizesshow sizes


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/668238.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号