簡體   English   中英

argparse 子命令和組:在子命令中在自己的組中設置幫助對話框而不隱藏在頂級幫助對話框中

[英]argparse sub-commands and groups: Setting help-dialog in sub-command on its own group without being hidden in top-level help-dialog

我正在嘗試實現一個程序,它同時使用子命令(例如:程序子命令 [options])和組(這使得一個花哨的幫助對話框)。

我已經實現了這一目標,但有一個小例外:為了在自己的組中獲得幫助對話框,我必須在創建子命令解析器時添加標志add_help=False ,這會在運行頂級幫助對話框時刪除幫助消息(例如: program -h )。

這是我開發的代碼:

# imports
import argparse

# create the top-level parser
parser = argparse.ArgumentParser(prog="example", add_help=False, epilog="A very cool program")

# add top-level groups
toplevel = parser.add_argument_group("Global arguments")
toplevel.add_argument("-g", "--global", action="store_true", help="A global argument.")

help = parser.add_argument_group("Help dialog")
help.add_argument("-h", "--help", action="help", default=argparse.SUPPRESS, help="Show this help message and exit.")

# create subparser
subparsers = parser.add_subparsers(title="Available subcommands", dest="subcommand")

# create the parser for the "a" subcommand
parser_a = subparsers.add_parser("a", add_help=False)

# add groups for subcommand "a"
required_a = parser_a.add_argument_group("Required arguments")
required_a.add_argument("--bar", type=int, help="Flag bar help", required=True)

help_a = parser_a.add_argument_group("Help dialog")
help_a.add_argument("-h", "--help", action="help", default=argparse.SUPPRESS, help="Show this help message and exit.")

# create the parser for the "b" command
parser_b = subparsers.add_parser("b", add_help=False)

# add groups for subcommand "b"
required_b = parser_b.add_argument_group("Required arguments")
required_b.add_argument("--baz", help="Flag baz help", required=True)

optional_b = parser_b.add_argument_group("Optional arguments")
optional_b.add_argument("--tas", help="Flag tas help")

help_b = parser_b.add_argument_group("Help dialog")
help_b.add_argument("-h", "--help", action="help", default=argparse.SUPPRESS, help="Show this help message and exit.")

# parse arguments
args = parser.parse_args()

# provide args to main
print(args)

頂層幫助如下:

$ example -h
usage: example [-g] [-h] {a,b} ...

Global arguments:
  -g, --global  A global argument.

Help dialog:
  -h, --help    Show this help message and exit.

Available sub-commands:
  {a,b}

A very cool program

正如您所看到的,它沒有在子命令上顯示幫助消息。 為了讓它們出現,我必須在創建parser_aparser_b時去掉add_help=False但是,正如預期的那樣,當我定義自己的幫助標志時會引發問題。

從本質上講,我希望兩全其美,我的主要對話將是:

$ example -h
usage: example [-g] [-h] {a,b} ...

Global arguments:
  -g, --global  A global argument.

Help dialog:
  -h, --help    Show this help message and exit.

Available sub-commands:
  {a,b}
    a           Help for sub-command a.
    b           Help for sub-command b.

A very cool program

子命令將是:

$ example a -h
usage: example a --bar BAR [-h]

Required arguments:
  --bar BAR   Flag bar help

Help dialog:
  -h, --help  Show this help message and exit.

通過 argparse 源代碼后,選項conflict_handler是一個可能的解決方案? 是否可以告訴它忽略原始幫助對話框,該對話框顯示在我不想要的位置參數下,而是讓它顯示在我自己的組中但不完全禁用它?

TL;DR:尋找我的 Python 腳本所需的修改,以便 argparse 輸出前兩個代碼塊。

注:之所以在標題中寫“help-dialog”是因為堆棧溢出不允許我在標題上寫世界“幫助”,無論它寫在句子中的哪個位置。

您的代碼產生:

1138:~/mypy$ python3 stack69633930.py -h
usage: example [-g] [-h] {a,b} ...

Global arguments:
  -g, --global  A global argument.

Help dialog:
  -h, --help    Show this help message and exit.

Available subcommands:
  {a,b}

A very cool program
1140:~/mypy$ python3 stack69633930.py a -h
usage: example a --bar BAR [-h]

Required arguments:
  --bar BAR   Flag bar help

Help dialog:
  -h, --help  Show this help message and exit.

在常規幫助下

1140:~/mypy$ python3 stack69633930-1.py -h
usage: example [-h] [-g] {a,b} ...

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

Global arguments:
  -g, --global  A global argument.

Available subcommands:
  {a,b}

A very cool program
1142:~/mypy$ python3 stack69633930-1.py a -h
usage: example a [-h] --bar BAR

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

Required arguments:
  --bar BAR   Flag bar help

我看到的唯一區別是“幫助對話框”組而不是“可選參數”,以及[-h]在用法中的位置。

要獲取子命令的“幫助”:

1155:~/mypy$ python3 stack69633930-1.py -h
usage: example [-h] [-g] {a,b} ...

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

Global arguments:
  -g, --global  A global argument.

Available subcommands:
  {a,b}
    a           help for subcommand a
    b           help for subcommand b

A very cool program

添加

parser_a = subparsers.add_parser("a", help='help for subcommand a')

這也適用於您的幫助組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM