Q:

How can I have a command-line argument -h with Python's argparse?

I have the following python:
import argparse
parser = argparse.ArgumentParser()
requiredNamed = parser.add_argument_group('required named arguments')
requiredNamed.add_argument('-h', '--host_name', required=True, help="Host IP address")
args = parser.parse_args()

This produces the following error:
argparse.ArgumentError: argument -h/--help: conflicting option string(s): -h

Every single letter works fine except -h. It seems like it is reserved for --help. How can I make it so that it isn't -h isn't automatically reserved?

A:

ArgumentParser takes an optional parameter add_help which you can set False.
In the documentation for add_help: 

Occasionally, it may be useful to disable the addition of this help option. This can be achieved by passing False as the add_help= argument to ArgumentParser:  
>>> parser = argparse.ArgumentParser(prog='PROG', add_help=False)  
>>> parser.add_argument('--foo', help='foo help')
>>> parser.print_help()
usage: PROG [--foo FOO]

optional arguments:
 --foo FOO  foo help

