Python argparse:如何在帮助文本中插入换行符? [英] Python argparse: How to insert newline in the help text?

查看:101
本文介绍了Python argparse:如何在帮助文本中插入换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using argparse in Python 2.7 for parsing input options. One of my options is a multiple choice. I want to make a list in its help text, e.g.

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()

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

However, argparse strips all newlines and consecutive spaces. The result looks like


~/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 d = delta e
                  = epsilon

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

How to insert newlines in the help text?

推荐答案

尝试使用 查看全文

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