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

前端 未结 2 1111
执笔经年
执笔经年 2020-12-16 05:14

There was a question that asked where they come from, and the accepted answer was a bunch of links to tutorials and source code. Explanation for argparse python modul behavi

相关标签:
2条回答
  • 2020-12-16 05:27

    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
    
    0 讨论(0)
  • 2020-12-16 05:47
    parser.add_argument('-c', '--chunksize', metavar='\b', type=int, help='chunk size in bits')
    

    seems to work

    0 讨论(0)
提交回复
热议问题