【问题标题】:argparse add example usageargparse 添加示例用法
【发布时间】:2018-06-08 18:50:47
【问题描述】:

我使用argparse处理输入参数,它通过parser.print_help()输出如下:

optional arguments:
  -h, --help            show this help message and exit
  -t TEMPLATES, --templates TEMPLATES
                    template names to make, should be defined as section
                    name in conf, and have related file in templates/
                    folder
  -c CONFPATH, --confpath CONFPATH
                    configuration path for template detail info

我的代码如下所示:

    import argparse
    parser = argparse.ArgumentParser(prog='base_maker', description='template maker')
    parser.add_argument('-t', '--templates', help='template names to make, should be defined as section name in conf, and have related file in templates/ folder', type=str)
    parser.add_argument('-c', '--confpath', help='configuration path for template detail info', type=str, default=os.path.join(basepath, 'conf/maker.conf'))

但是,我想添加一个关于如何使用 -t/--template 的详细示例,例如(在 example 部分添加):

optional arguments:
  -h, --help            show this help message and exit
  -t TEMPLATES, --templates TEMPLATES
                    template names to make, should be defined as section
                    name in conf, and have related file in templates/
                    folder
  -c CONFPATH, --confpath CONFPATH
                    configuration path for template detail info

 example:

     python test.py -t template/test.py
     python test.py -t template/test -c conf/test.conf
     python test.py -t test.py

我不知道应该使用哪个属性来添加“示例”部分,我检查了Print program usage example with argparse modulen,但是当我检查epilog in the official doc时不清楚并且没有详细的示例。

谁能给我一个例子来说明如何做到这一点?谢谢

【问题讨论】:

  • 您是否尝试过使用提到的epilog?它有什么问题?
  • 是的,但是里面怎么换行,我想多行解释,然后不知道有没有更好的办法
  • 您是否尝试过将"""multiline string""" 传递给epilog?我猜会这样做,其中的换行符将被保留。你可以保持整洁,就像我展示的那样here

标签: python argparse


【解决方案1】:

如果您希望在末尾打印示例帮助(epilog)并保留空白/格式(formatter_class 设置为 RawDescriptionHelpFormatter),则需要使用 Epilog 和 formatter_class 参数到 ArgumentParser。

通过修改上面的示例来举例:

import argparse

example_text = '''example:

 python test.py -t template/test.py
 python test.py -t template/test -c conf/test.conf
 python test.py -t test.py'''

parser = argparse.ArgumentParser(prog='base_maker',
                                 description='template maker',
                                 epilog=example_text,
                                 formatter_class=argparse.RawDescriptionHelpFormatter)

parser.add_argument('-t', '--templates', help='template names to make, should be defined as section name in conf, and have related file in templates/ folder', type=str)
parser.add_argument('-c', '--confpath', help='configuration path for template detail info', type=str, default=os.path.join(basepath, 'conf/maker.conf'))

【讨论】:

  • 准确,谢谢,它需要 formatter_class 设置:)
  • 我已经在使用ArgumentDefaultsHelpFormatter。猜猜我得自己动手了?
  • 您可以在示例文本中使用$(prog)s,而不是硬编码程序名称:例如example_text = "$(prog)s -t template/test.py"
  • @famzah 说明符实际上是 %(prog)s 而不是 $(prog)s
  • @pranavashok,你是对的!谢谢。正确的例子是:example_text = "%(prog)s -t template/test.py"
猜你喜欢
  • 2014-01-22
  • 2019-07-11
  • 2018-07-12
  • 1970-01-01
  • 2021-04-25
  • 1970-01-01
  • 2020-08-09
  • 2021-11-20
  • 2012-06-11
相关资源
最近更新 更多