argparse help without duplicate ALLCAPS

a 夏天 提交于 2019-12-30 03:49:47

问题


I'd like to display argparse help for my options the same way the default -h,--help and -v,--version are, without the ALLCAPS text after the option, or at least without the duplicated CAPS.

import argparse
p = argparse.ArgumentParser("a foo bar dustup")
p.add_argument('-i', '--ini', help="use alternate ini file")
print '\n', p.parse_args()

This is what I currently get with python foobar.py -h:

usage: a foo bar dustup [-h] [-i INI]

optional arguments:
  -h, --help            show this help message and exit
  -i INI, --ini INI     use alternate ini

And this is what I want:

usage: a foo bar dustup [-h] [-i INI]

optional arguments:
  -h, --help            show this help message and exit
  -i, --ini INI         use alternate ini

This would be acceptable too:

  -i, --ini             use alternate ini

I'm using python 2.7.


回答1:


You could customize usage and assign metavar to an empty string:

import argparse

p = argparse.ArgumentParser("a foo bar dustup", usage='%(prog)s [-h] [-i INI]')
p.add_argument('-i', '--ini', help="use alternate ini file", metavar='')
p.print_help()

Output

usage: a foo bar dustup [-h] [-i INI]

optional arguments:
  -h, --help   show this help message and exit
  -i , --ini   use alternate ini file



回答2:


Well from what I can tell you have two options,

import argparse

p = argparse.ArgumentParser(description="a foo bar dustup")
p.add_argument('-i', '--ini', metavar='', help="use alternate ini file")

print '\n', p.parse_args()

or you can write a custom formatter class, I realize the first option may not be a perfect solution, as it gets rid of the CAPS in the usage line. If it's that important here is the source for argparse, from what I can tell the default formatter classes won't do exactly what you want.

Edit:

Well I went ahead and built you your own formatter class, in the same fashion as the others... not sure I'd recommend you using this in production code as there won't be any official python documentation for it =P

import argparse
from argparse import HelpFormatter

class MyFormatter(HelpFormatter):
    """
        for matt wilkie on SO
    """

    def _format_action_invocation(self, action):
        if not action.option_strings:
            default = self._get_default_metavar_for_positional(action)
            metavar, = self._metavar_formatter(action, default)(1)
            return metavar

        else:
            parts = []

            # if the Optional doesn't take a value, format is:
            #    -s, --long
            if action.nargs == 0:
                parts.extend(action.option_strings)

            # if the Optional takes a value, format is:
            #    -s ARGS, --long ARGS
            else:
                default = self._get_default_metavar_for_optional(action)
                args_string = self._format_args(action, default)
                for option_string in action.option_strings:
                    parts.append(option_string)

                return '%s %s' % (', '.join(parts), args_string)

            return ', '.join(parts)

    def _get_default_metavar_for_optional(self, action):
        return action.dest.upper()

p = argparse.ArgumentParser("a foo bar dustup", formatter_class=MyFormatter)
p.add_argument('-i', '--ini', help="use alternate ini file")
p.print_help()


来源:https://stackoverflow.com/questions/9642692/argparse-help-without-duplicate-allcaps

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!