0

So I've been frantically reading tutorials on argparse everywhere but can't seem to figure out why my program is getting an error. My code currently looks like this:

parser = argparse.ArgumentParser()
parser.add_argument("-d", "-debug", required = False, help = "optional parameter")
parser.add_argument("input_file", help = "file to be parsed")
args = parser.parse_args()

When I run my program with the command "python myprogram.py -d inputfile" it complains that there are too few arguments. Furthermore, when I just run it with inputfile as the parameter, it works.

Does anyone know why this might be happening?

1 Answer 1

3

The default action for an argument is 'store'. store actions generally expect a value to be associated with the flag.

It looks like you want this to be a boolean switch type of flag in which case you want the 'store_true' action

parser = argparse.ArgumentParser()
parser.add_argument("-d", "--debug", required = False, help = "optional parameter", action = "store_true")
parser.add_argument("input_file", help = "file to be parsed")
args = parser.parse_args()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.