相关疑难解决方法(0)

Python argparse:如何在帮助文本中插入换行符?

argparse在Python 2.7中用于解析输入选项.我的一个选择是多选.我想在其帮助文本中列出一个列表,例如

from argparse import ArgumentParser

parser = ArgumentParser(description='test')

parser.add_argument('-g', choices=['a', 'b', 'g', 'd', 'e'], default='a',
    help="Some option, where\n"
         " a = alpha\n"
         " b = beta\n"
         " g = gamma\n"
         " d = delta\n"
         " e = epsilon")

parser.parse_args()
Run Code Online (Sandbox Code Playgroud)

但是,argparse删除所有换行符和连续空格.结果看起来像

~/Downloads:52$ python2.7 x.py -h
usage: x.py [-h] [-g {a,b,g,d,e}]

test

optional arguments:
  -h, --help      show this help message and exit
  -g {a,b,g,d,e}  Some option, where a = alpha b = beta g = gamma …

python argparse

317
推荐指数
6
解决办法
7万
查看次数

Python argparse:在帮助条目之间插入空白行

使用argparse时,传递--help给程序会生成帮助文本.不幸的是,它很难阅读,因为选项之间没有空行.这是一个摘录来说明:

optional arguments:
  -h, --help            show this help message and exit
  -u FILENAME, --up-sound FILENAME
                        The sound to play when the network comes up. Default:
                        "/path/to/some/sound/file.wav"
  -d FILENAME, --down-sound FILENAME
                        The sound to play when the network goes down. Default:
                        "/path/to/some/other/sound/file.wav"
  -p EXECUTABLE, --player EXECUTABLE
                        The program to use to play sounds. Default: "play"
  -s, --silent          If specified, network_monitor.py will not play any
                        sounds.
  -c, --no-clear-screen
                        If specified, screen will not be cleared (nor extra
                        blank lines added) …
Run Code Online (Sandbox Code Playgroud)

python argparse

9
推荐指数
1
解决办法
810
查看次数

标签 统计

argparse ×2

python ×2