I am trying to work with argparse in python and I do not know how to call the program at command line to see if it works.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("DocID", type= int, help= "Insert DocID Here")
parser.add_argument("echo", help = "Enter in the FileName to be read")
args = parser.parse_args()
print args
This is my incredibly basic program that I am trying to use just to learn more about how argparse works. I just need to learn how and where to call the program to be able to use the arguments I give it.
Edit: To make my question more clear sorry. I have this code, but I do not know how to call the program as a whole so that I can run it. Like how would I run this in command line? Because when run just in IDLE it produces and error because of a lack of commands
an ArgumentParser’s
parse_argsmethod can take a list as input. That list is used for parsing the commandline arguments. So, a common idiom is:since
str.splitreturns a list. ('a b c'.split() == ['a', 'b', 'c'])Usually when you add arguments, you do so like this:
Arguments without a
-or--in front are positional arguments, so both of your arguments are positional. You can see a little of what is happening if you do:which is the same as calling your script as
python youscript.py 1 foo(without the list insideparse_args).