How do I avoid the capital placeholders in python's argparse module?

我的梦境 提交于 2019-11-29 02:31:14
parser.add_argument('-c', '--chunksize', metavar='\b', type=int, help='chunk size in bits')

seems to work

You can make your formatter class to format the arguments whichever way you want. It's not entirely straight forward, but here's one that produces the following output (assuming @mgilson is correct in the assumption that you wanted to only display the metavar once for the set of command names... Otherwise just specify an actual metavar='value' and it will display precisely that text.):

# without metavar specified:
-c, --chunksize CHUNKSIZE
                chunk size in bits
# with metavar specified:
-c, --chunksize some_metavar
                chunk size in bits

And the code for the class and reproducing the two outputs:

import argparse
# 2.7-3.2
class SingleMetavarHelpFormatter(argparse.HelpFormatter):
    def _format_action_invocation(self, action):
        if not action.option_strings:
            metavar, = self._metavar_formatter(action, action.dest)(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 = action.dest.upper()
                args_string = self._format_args(action, default)

                ## THIS IS THE PART REPLACED
                #~ for option_string in action.option_strings:
                    #~ parts.append('%s %s' % (option_string, args_string)) ### this is change
                ## /SECTION REPLACED

                ## NEW CODE:
                parts.extend(action.option_strings)
                parts[-1] += ' %s' % args_string
                ## /NEW CODE
            return ', '.join(parts)


parser = argparse.ArgumentParser(
    prog='PROG',
    formatter_class=SingleMetavarHelpFormatter
    )

parser.add_argument('-c', '--chunksize', type=int, help='no metavar specified')
parser.add_argument('-w', '--with_metavar', type=int, help='metavar specified', metavar='some_metavar')

parser.print_help()

edit: To not show a metavar at all, you can pass an empty string to metavar:

parser.add_argument('-e', '--with_empty_metavar', type=int, help='empty metavar specified', metavar='')

The difference between doing that with the original class and the new class is the lack extra space character after the short command syntax.

#usage: PROG [-h] [-c CHUNKSIZE] [-w some_metavar] [-e]
#
#optional arguments:
#  -h, --help            show this help message and exit
#  -c CHUNKSIZE, --chunksize CHUNKSIZE
#                        no metavar specified
#  -w some_metavar, --with_metavar some_metavar
#                        metavar specified
#  -e, --with_empty_metavar
#                        empty metavar specified
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!