Reputation: 2608
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.
Upvotes: 9
Views: 21430
Reputation: 531390
Using argparse
subcommand parsers
p = argparse.ArgumentParser()
subparsers = p.add_subparsers()
option1_parser = subparsers.add_parser('option1')
# Add specific options for option1 here, but here's
# an example
option1_parser.add_argument('param1')
option1_parser.set_defaults(func=do_option1)
option2_parser = subparsers.add_parser('option2')
# Add specific options for option1 here
option2_parser.set_defaults(func=do_option2)
option3_parser = subparsers.add_parser('option3')
# Add specific options for option3 here
option3_parser.set_defaults(func=do_option3)
args = p.parse_args()
args.func(args)
Then each of your do_option
functions would need to be rewritten slightly to take a single argument from which it can extract the values it needs. For example:
def do_option1(args):
param1 = args.param1
# And continue
Upvotes: 11
Reputation: 10799
I use this template that may help you out.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--option1', help='description for option1')
parser.add_argument('--option2', help='description for option2')
parser.add_argument('--option3', help='description for option3')
args = parser.parse_args()
if args.option1:
...do something
if args.option2:
...do something
if args.option3:
...do something
You can then run your script passing the arguments like this:
python script.py --option1 my-option1 --option3 my-option3 --option2 my-option2
Note that the position of the arguments is not important since you specify the name of the argument before its value.
Upvotes: 6
Reputation: 1416
As stated in the accepted answer, you can (ab?)use the type
parameter of add_argument
to execute a function.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('option2', type=interface.do_option2)
args = parser.parse_args()
Upvotes: 2
Reputation: 11039
options = {
'option_1': my_class.option_1,
'option_2': my_class.option_2,
'option_3': my_class.option_3,
}
option, params = sys.argv[0], sys.argv[1:]
options[option](*params)
Should do the trick. You'll probably want to add some checking to make sure that the user is passing at least some arguments to your script.
Upvotes: 4
Reputation: 528
If you want to directly pass in the parameters without using tags, you can just use the sys module: sys.argv
will give you ['filename.py', 'option3', 'param1', 'param2', 'param3']
for the command line of filename.py option3 param1 param2 param3
http://www.tutorialspoint.com/python/python_command_line_arguments.htm
Upvotes: 1