移动"帮助"到python argparse中的另一个参数组

时间:2012-10-25 18:50:06

标签: python argparse

目前我正在使用Python创建目录读取器程序。 我正在使用'argparse'来解析命令行中的参数。我有以下代码:

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”时,它会产生以下输出:

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]

我的问题:有没有办法将默认帮助参数移动到诸如选项之类的组中? 此外,我似乎无法找到一种方法来删除选项参数中逗号之前的空格。理想的输出是:

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]

3 个答案:

答案 0 :(得分:26)

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

要删除空格,请不要将metavar设置为空字符串。应使用action="store_true"指定您的选项以使其成为真(无参数)选项:

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

答案 1 :(得分:7)

当然可以这样做。诀窍是只需添加add_help=False ArgumentParser构造函数,然后将自己的帮助操作添加到组中:

import argparse

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

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("-h", "--help", action='help', help='print this fabulous help message')
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()

答案 2 :(得分:0)

要缩小并简化先前answer by nneonneo的位,您可以:

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)

add_arg = parser.add_argument_group("Options").add_argument

add_arg("-h", "--help", action="help", help="show this help message and exit")
add_arg("-path", default=".", help=argparse.SUPPRESS)
add_arg("-m", "--modified", action="store_true",
        help="show last modified date/time")
add_arg("-o", "--order", nargs=1, default="name",
        help="sort order (n[ame], m[odified], s[ize])\n[default: name]")
add_arg("-r", "--recursive", action="store_true",
        help="recurse into subdirectories")
add_arg("-s", "--sizes", action="store_true",
        help="show sizes")

args = parser.parse_args()

基本上,与上一个答案略有不同的是没有必要保存group,然后重复调用add_argument方法