Argparse unit tests: Suppress the help message

前端 未结 3 1810
广开言路
广开言路 2020-12-19 17:17

I\'m writing test cases for argparse implementation. I intend to test \'-h\' feature. The following code does it. But it also outputs the usage for the script. Is there a wa

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-19 17:34

    Some variations on using ['-h'] include:

    parser.print_help()  # formats the help, prints it and exits
    parser.format_help() # format the help without printing or exit
    parser.exit  # can be modified to not exit, such as for a parser subclass
    parser.error # default print usage and call parser.exit
    

    These are part of the public API.

    The argparse test file (test_argparse.py) also gives ideas on how to test things. For many tests it uses an ArgumentParser subclass that has its own error method.

提交回复
热议问题