Py学习  »  Python

python argparse:通过帮助输出在参数组之间添加节?

Andrius • 4 年前 • 601 次点击  

是否可以在可选参数上添加一些小节?所以用户更容易理解哪个参数与哪个参数相关?

我是说举个例子 psql --help ,输出这个(我不知道psql解析库使用了哪些参数,但给出了一个理想输出的好例子):

Usage:
  psql [OPTION]... [DBNAME [USERNAME]]

General options:
  -c, --command=COMMAND    run only single command (SQL or internal) and exit
  -d, --dbname=DBNAME      database name to connect to (default: "oerp")
  -f, --file=FILENAME      execute commands from file, then exit
  -l, --list               list available databases, then exit
  -v, --set=, --variable=NAME=VALUE
                           set psql variable NAME to VALUE
                           (e.g., -v ON_ERROR_STOP=1)
  -V, --version            output version information, then exit
  -X, --no-psqlrc          do not read startup file (~/.psqlrc)
  -1 ("one"), --single-transaction
                           execute as a single transaction (if non-interactive)
  -?, --help[=options]     show this help, then exit
      --help=commands      list backslash commands, then exit
      --help=variables     list special variables, then exit

Input and output options:
  -a, --echo-all           echo all input from script
  -b, --echo-errors        echo failed commands
  -e, --echo-queries       echo commands sent to server
  -E, --echo-hidden        display queries that internal commands generate
  -L, --log-file=FILENAME  send session log to file
  -n, --no-readline        disable enhanced command line editing (readline)
  -o, --output=FILENAME    send query results to file (or |pipe)
  -q, --quiet              run quietly (no messages, only query output)
  -s, --single-step        single-step mode (confirm each query)
  -S, --single-line        single-line mode (end of line terminates SQL command)

Output format options:
  -A, --no-align           unaligned table output mode
  -F, --field-separator=STRING
                           field separator for unaligned output (default: "|")
  -H, --html               HTML table output mode
  -P, --pset=VAR[=ARG]     set printing option VAR to ARG (see \pset command)
  -R, --record-separator=STRING
                           record separator for unaligned output (default: newline)
  -t, --tuples-only        print rows only
  -T, --table-attr=TEXT    set HTML table tag attributes (e.g., width, border)
  -x, --expanded           turn on expanded table output
  -z, --field-separator-zero
                           set field separator for unaligned output to zero byte
  -0, --record-separator-zero
                           set record separator for unaligned output to zero byte

Connection options:
  -h, --host=HOSTNAME      database server host or socket directory (default: "/var/run/postgresql")
  -p, --port=PORT          database server port (default: "5432")
  -U, --username=USERNAME  database user name (default: "oerp")
  -w, --no-password        never prompt for password
  -W, --password           force password prompt (should happen automatically)

For more information, type "\?" (for internal commands) or "\help" (for SQL
commands) from within psql, or consult the psql section in the PostgreSQL
documentation.

Report bugs to <pgsql-bugs@postgresql.org>.

有些章节像 General options , Input and output options 等等。

argparse 我只得到 positional arguments optional arguments 选项。是否可以为可选参数创建一些子部分?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/44137
 
601 次点击  
文章 [ 1 ]  |  最新文章 4 年前
wjandrea
Reply   •   1 楼
wjandrea    5 年前

正如评论中提到的,你想要 ArgumentParser.add_argument_group() .

在此复制示例代码,修改:

import argparse

parser = argparse.ArgumentParser(prog='PROG')

group = parser.add_argument_group('group')
group.add_argument('--foo', help='foo help')
group.add_argument('bar', help='bar help')

other_group = parser.add_argument_group('other group')
other_group.add_argument('baz', help='baz help')

parser.print_help()

输出:

usage: PROG [-h] [--foo FOO] bar baz

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

group:
  --foo FOO   foo help
  bar         bar help

other group:
  baz         baz help