When running with --help
, the help output includes the description of the --help
argument itself. How can that line be avoided in the output of --help
?
I could not get this answer to work, as the following code demonstrates when run using Python 3.10:
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser(
add_help=False,
description=f'does foo')
parser.add_argument('--bar', type=str, required=True, help='the bar value')
args = parser.parse_args()
The result of running the above script file with --help
is an error message, not the help message sans the --help
option in it:
usage: scratch_54.py --bar BAR
scratch_54.py: error: the following arguments are required: --bar
I would like to avoid the help option self-referencing itself in its own output, while having the rest of the help message behave as usual.
Try this:
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument("-h,", "--help", action="help", help=argparse.SUPPRESS)
The help action will still be there, and work as usual, but it will be hidden from the help text itself.