[Python] I want to use the -h option with argparse

An error occurs when using the -h option with argparse

When 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.

To use the -h option

For 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)

References

https://stackoverflow.com/questions/14950964/overriding-default-argparse-h-behaviour

Recommended Posts

[Python] I want to use the -h option with argparse
I want to use MATLAB feval with python
I want to use Temporary Directory with Python2
I want to inherit to the back with python dataclass
I want to use the R dataset in python
I want to debug with Python
I want to use jar from python
I want to analyze logs with Python
I want to play with aws with python
I want to use a wildcard that I want to shell with Python remove
I want to know the weather with LINE bot feat.Heroku + Python
I want to output the beginning of the next month with Python
Specify the Python executable to use with virtualenv
The easiest way to use OpenCV with python
I want to use ceres solver from python
I don't want to use -inf with np.log
#Unresolved I want to compile gobject-introspection with Python3
I want to use ip vrf with SONiC
I want to solve APG4b with Python (Chapter 2)
I want to write to a file with Python
I want to use the activation function Mish
I want to display the progress in Python!
How to use Python argparse
I want to handle optimization with python and cplex
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
I wanted to use the Python library from MATLAB
I want to work with a robot in python.
I want to write in Python! (3) Utilize the mock
I want to AWS Lambda with Python on Mac!
Specify MinGW as the compiler to use with Python
[ML Ops] I want to do multi-project with Python
I tried to solve the problem with Python Vol.1
I want to run a quantum computer with Python
Solution when you want to use cv_bridge with python3 (virtualenv)
I tried to find the entropy of the image with python
I want to use the latest gcc without sudo privileges! !!
[Python] I want to add a static directory with Flask [I want to use something other than static]
I want to initialize if the value is empty (python)
I want to use R functions easily with ipython notebook
I tried to simulate how the infection spreads with Python
Python: How to use async with
I want to specify another version of Python with pyvenv
Why can I use the module by importing with python?
maya Python I want to fix the baked animation again.
I want to be able to analyze data with Python (Part 1)
I want to use only the normalization process of SudachiPy
I want to change the Japanese flag to the Palau flag with Numpy
I want to be able to analyze data with Python (Part 4)
What I did to welcome the Python2 EOL with confidence
I want to be able to analyze data with Python (Part 2)
I want to automatically attend online classes with Python + Selenium!
I liked the tweet with python. ..
I want to use a virtual environment with jupyter notebook!
I didn't know how to use the [python] for statement
How to use FTP with Python
I want to know the features of Python and pip
I want to do ○○ with Pandas
I tried to divide the file into folders with Python
I know? Data analysis using Python or things you want to use when you want with numpy
[Python] I want to make a 3D scatter plot of the epicenter with Cartopy + Matplotlib!