移动“帮助"到python argparse中的另一个参数组 [英] Move "help" to a different Argument Group in python argparse

查看:100
本文介绍了移动“帮助"到python argparse中的另一个参数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在使用Python创建目录读取器程序. 我正在使用"argparse"从命令行解析参数.我有以下代码:

Currently I'm creating a directory reader program using Python. I'm using 'argparse' to parse the arguments from command line. I have the following code:

parser = argparse.ArgumentParser(prog = "LS.py",
                                 usage = "%(prog)s [options] [path1 [path2 [...pathN]]]\nThe paths are optional; if not given . is used.")

group = parser.add_argument_group("Options")

group.add_argument("-path", default = ".", help = argparse.SUPPRESS, metavar = "")
group.add_argument("-m", "--modified", default = False,
                    help = "show last modified date/time [default: off]",
                    metavar = "")
group.add_argument("-o ORDER", "--order=ORDER", nargs = 2, default = "name",
                    help = "order by ('name', 'n', 'modified', 'm', 'size', 's')\n[default: name]",
                    metavar = "")
group.add_argument("-r", "--recursive", default = False,
                    help = "recurse into subdirectories [default: off]",
                    metavar = "")
group.add_argument("-s", "--sizes", default = False,
                   help = "show sizes [default: off]", metavar = "")

args = parser.parse_args()
return args

以以下方式"LS.py -h"调用时,将产生以下输出:

When called in the following manner "LS.py -h" it produces the following output:

usage: LS.py [options] [path1 [path2 [...pathN]]]
The paths are optional; if not given . is used.

optional arguments:
  -h, --help            show this help message and exit

Options:
  -m , --modified       show last modified date/time [default: off]
  -o ORDER  , --order=ORDER  
                    order by ('name', 'n', 'modified', 'm', 'size', 's')
                    [default: name]
  -r , --recursive      recurse into subdirectories [default: off]
  -s , --sizes          show sizes [default: off]

我的问题:是否可以将默认帮助参数移至诸如选项"之类的组中? 另外,我似乎找不到在Option参数中用逗号删除空格的方法.理想的输出是:

My question: Is there a way to move the default help argument into a group such as Options? Also, I can't seem to find a way to remove the space before the commas in the Options arguments. The ideal output is:

Usage: ls.py [options] [path1 [path2 [...pathN]]]
The paths are optional; if not given . is used.

Options:
  -h, --help            show this help message and exit
  -m, --modified        show last modified date/time [default: off]
  -o ORDER, --order=ORDER
                        order by ('name', 'n', 'modified', 'm', 'size', 's')
                        [default: name]
  -r, --recursive       recurse into subdirectories [default: off]
  -s, --sizes           show sizes [default: off]

推荐答案

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

You can use add_help=False to disable the built-in help command and add your own instead, using action="help" (thanks @mgilson!)

要摆脱空格,请勿将metavar设置为空字符串.您应该使用action="store_true"指定您的选项,以使它们成为真实的(无参数的)选项:

To get rid of the spaces, don't set metavar to an empty string. Your options should be specified using action="store_true" to make them true (argument-less) options:

import argparse

parser = 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, --sizes           show sizes

这篇关于移动“帮助"到python argparse中的另一个参数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆