2

I am trying to use argparse to set up some simple command line options for a program I am writing. I do not understand why I am getting an error for "-u". I am using Python 2.7. Does anyone know what I am doing wrong?

Code: main.py -s 172.17.0.3 -p 8591 –u “user” -c “pass” -r 68.2

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-s", "--server", help="ip address of the server")
parser.add_argument("-p", "--port", help="port number of the server")
parser.add_argument("-u", "--user", help="username")
parser.add_argument("-c", "--pass", help="authentication credentials")
parser.add_argument("-r", "--record", help="port number of the server")
args = parser.parse_args()

Output: usage: main.py [-h] [-s SERVER] [-p PORT] [-u USER] [-c PASS] [-r RECORD] main.py: error: unrecognized arguments: �u �user�

Process finished with exit code 2
Matt O
  • 1,336
  • 2
  • 10
  • 19
jboutsicaris
  • 23
  • 1
  • 3

1 Answers1

3

When you call main.py, check your argument that you are passing for the user (-u "user").

You are using an en dash (–) instead of a hyphen (-). These are different characters.

http://www.thepunctuationguide.com/en-dash.html

Preston Martin
  • 2,789
  • 3
  • 26
  • 42