Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

argparse .ArgumentParser raise ArgumentError

conflict_handler(action, confl_optionals)
  File "/usr/local/lib/python3.6/argparse.py", line 1510, in _handle_conflict_error
    raise ArgumentError(action, message % conflict_string)
argparse.ArgumentError: argument -h/--height: conflicting option string: -h

The above is the error message, here is my code, I don't see the error:

# 1) Parse the arguments

parser = argparse.ArgumentParser(description="Description for my parser")
parser.add_argument("-v", "--velocity", action="store", required=True, help="The velocity of the object is required")
parser.add_argument("-a", "--angle", action="store", type=float, required=True, help="The angle of the object is required")
parser.add_argument("-h", "--height", required=False, default= 1.2, help="The height of the object is not required. Default is set to 1.2 meters" )
like image 890
Chukas Ebuka Avatar asked Mar 04 '23 07:03

Chukas Ebuka


1 Answers

Option "-h" is by default predefined as a "help" option, which prints the description and the list of arguments. Your custom "-h --height" conflicts with this, thus causing an error.

It wouldn't be nice to overwrite the default "-h --help" option, because many users expect "-h" option to print help message. (So if I were you I would find another way to name the option.) But you can ignore it if you really need to by using add_help parameter with the constructor. Like this:

parser = argparse.ArgumentParser(description="Description for my parser", add_help=False)

If you want to keep "--help" option, you have to add another line of parser.add_argument("--help", action="help"). (Thanks to chepner)

like image 101
Ignatius Avatar answered Mar 19 '23 22:03

Ignatius