Skip to content Skip to sidebar Skip to footer

Python Argumentparser - Error: Unrecognized Arguments: Ip_2 '127.0.0.1'`

I have the following code in a python script. parser.add_argument('ip_1', default='127.0.0.1', nargs='?', help='The First IP address.') parser.add_argument('i

Solution 1:

For argparse, if the first argument (dest) does not have a prefix character (-) it will be treated simply as a positional argument. So in actuality with the arguments supplied, parser.ip_1 will be 'ip_1' and parser.ip_2 will be '127.200.300.400'. The rest of the argument will simply be treated as an error as they are unexpected. Simply omit the strings ip_1 and ip_2 for the script parameters (and also supply --port=4444 instead of --port_1) will do what you expect it to do.

Solution 2:

Solution: The only solution was to enter the following in Pycharm on the Parameters line in Edit Configurations tab

-o file.csv

If I omit the -o I get the error. Quotations around the filename are optional.

Background: I had the same problem using Argparse and Python 3.7

parser.add_argument("-o", "--outfile",
                help="Enter the name of a .csv file to contain output or default of radarOutTemp.csv will be used",
                default="radarOutTemp.csv")

Post a Comment for "Python Argumentparser - Error: Unrecognized Arguments: Ip_2 '127.0.0.1'`"