So I'm stuck on a project I'm working on that involves the command line in python.
So basically, here's what I'm trying to accomplish:
I have a set of functions in a class, say,
def do_option1(self, param1, param2) :
#some python code here
def do_option2(self, param1):
#some python code here
def do_option3(self, param1, param2, param3):
#some python code here
And so basically, when a user puts filename.py option2 param1
into the command line, I want it to call the function do_option2
and pass the parameter, param1
, to it.
Similarly, when a user puts filename.py option3 param1 param2 param3
, I want it to execute the do_option3 function with the given parameters.
I know there are 2 modules in python called argparse
and optparse
, but I've had difficulty understanding the two and i'm not sure if either of the two alone will accomplish what I need done.
sys.argv
. You can use a sequence ofif
statements, or the dictionary dispatching. If you need to add more options, then take the time to learnargparse
. Your problem fits thesubparsers
model nicely. – Kakapo