-h
option with argparseWhen managing options using Python's ʻargparse, for example, you may want to add the
-h` option to the height specification.
example.py
parser = argparse.ArgumentParser(description = "hogehoge")
parser.add_argument("-h", type=int, help = "Height is necessary.", required=True)
However, adding the -h
option will result in an error.
argparse.ArgumentError: argument -h: conflicting option string: -h
This -h
option is managed in advance by ʻargparse` as an option for Help, so the options conflict.
-h
optionFor programs that do not require the Help option, you can use it by specifying ʻadd_help = False in the parser generation part and disabling the Help option on the ʻargparse
side.
example.py
parser = argparse.ArgumentParser(description = "hogehoge", add_help=False)
https://stackoverflow.com/questions/14950964/overriding-default-argparse-h-behaviour
Recommended Posts