您可以使用
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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)