Control formatting of the argparse help argument list?

前端 未结 4 1104
再見小時候
再見小時候 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:39

    If you are providing a custom formatter_class to your ArgumentParser

    parser = argparse.ArgumentParser(formatter_class=help_formatter)
    

    and then use subparsers, the formatter will only apply to the top-level help message. In order to use the same (or some other) formatter for all subparsers, you need to provide formatter_class argument for each add_parser call:

    subparsers = parser.add_subparsers(metavar="ACTION", dest="action")
    child_parser = subparsers.add_parser(
        action_name, formatter_class=help_formatter
    )
    

提交回复
热议问题