Control formatting of the argparse help argument list?

前端 未结 4 1030
再見小時候
再見小時候 2021-02-05 01:55
import argparse
parser = argparse.ArgumentParser(prog=\'tool\')
args = [(\'-u\', \'--upf\', \'ref. upf\', dict(required=\'True\')),
        (\'-s\', \'--skew\', \'ref. s         


        
4条回答
  •  星月不相逢
    2021-02-05 02:37

    You could supply formatter_class argument:

    parser = argparse.ArgumentParser(prog='tool',
      formatter_class=lambda prog: argparse.HelpFormatter(prog,max_help_position=27))
    
    args = [('-u', '--upf', 'ref. upf', dict(required='True')),
            ('-s', '--skew', 'ref. skew', {}),
            ('-m', '--model', 'ref. model', {})]
    for args1, args2, desc, options in args:  
         parser.add_argument(args1, args2, help=desc, **options)
    
    parser.print_help()
    

    Note: Implementation of argparse.HelpFormatter is private only the name is public. Therefore the code might stop working in future versions of argparse. File a feature request to provide a public interface for the customization of max_help_position on http://bugs.python.org/

    Output

    usage: tool [-h] -u UPF [-s SKEW] [-m MODEL]
    
    optional arguments:
      -h, --help               show this help message and exit
      -u UPF, --upf UPF        ref. upf
      -s SKEW, --skew SKEW     ref. skew
      -m MODEL, --model MODEL  ref. model
    

提交回复
热议问题