0

I currently have 2 functions in my .py script.

#1 connects to the database and does some processing.

#2 does some other processing on files

Currently before I run the script, I have to manually comment/uncomment the function I want to run in my main if statement block.

How can I use argparse, so it asks me which function to run when I run my script?

Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115
Blankman
  • 259,732
  • 324
  • 769
  • 1,199

3 Answers3

4

It is possible to tell ArgumentParser objects about the function or object that has your desired behavior directly, by means of action='store_const' and const=<stuff> pairs in an add_argument() call, or with a set_defaults() call (the latter is most useful when you're using sub-parsers). If you do that, you can look up your function on the parsed_args object you get back from the parsing, instead of say, looking it up in the global namespace.

As a little example:

import argparse

def foo(parsed_args):
    print "woop is {0!r}".format(getattr(parsed_args, 'woop'))

def bar(parsed_args):
    print "moop is {0!r}".format(getattr(parsed_args, 'moop'))

parser = argparse.ArgumentParser()

parser.add_argument('--foo', dest='action', action='store_const', const=foo)
parser.add_argument('--bar', dest='action', action='store_const', const=bar)
parser.add_argument('--woop')
parser.add_argument('--moop')

parsed_args = parser.parse_args()
if parsed_args.action is None:
    parser.parse_args(['-h'])
parsed_args.action(parsed_args)

And then you can call it like:

% python /tmp/junk.py --foo                                           
woop is None
% python /tmp/junk.py --foo --woop 8 --moop 17                        
woop is '8'
% python /tmp/junk.py --bar --woop 8 --moop 17                        
moop is '17'
Matt Anderson
  • 19,311
  • 11
  • 41
  • 57
  • nice, should this be in my if __name__=="__main__" block? – Blankman Feb 09 '11 at 01:52
  • If a python file is intended to be accessed in multiple ways (called as a script, loaded as a module from another python file), then the parts specific to "being run as a script" should be in your main-section. You can write a function `main()` and call that in a `__name__ == "__main__"` if block, or write your script action code directly in said if block. – Matt Anderson Feb 09 '11 at 14:14
  • Wouldn't this solution force us to specify the unison of all arguments as optional? I have a more complicated case where `foo` and `bar` have different sets of arguments and I want to avoid specifying all of them as optional.. – Rafs Feb 25 '21 at 14:43
2

If it's just a flag of run A or B, then a simple "store_true" argument should be fine.

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--run_a_not_b', action='store_true')
_StoreTrueAction(option_strings=['--run_a_not_b'], dest='run_a_not_b', nargs=0, const=True, default=False, type=None, choices=None, help=None, metavar=None)
>>> parser.parse_args('--run_a_not_b')
>>> parsed_args = parser.parse_args('--run_a_not_b'.split())
>>> if parsed_args.run_a_not_b:
    print "run a"
else:
    print "run b"


run a
>>> parsed_args = parser.parse_args(''.split())
>>> if parsed_args.run_a_not_b:
    print "run a"
else:
    print "run b"


run b

Or if you want to actually pass in the name of the function to call, you can do it this (somewhat hackish) way:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--func_to_run', type=str)
_StoreAction(option_strings=['--func_to_run'], dest='func_to_run', nargs=None, const=None, default=None, type=<type 'str'>, choices=None, help=None, metavar=None)
>>> parsed_args = parser.parse_args('--func_to_run my_other_func'.split())
>>> parsed_args.func_to_run
'my_other_func'
>>> f = globals()[parsed_args.func_to_run]
<function my_other_func at 0x011F6670>
>>> f()

edit : to handle an integer argument, you would simply specify the type

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--run_a_not_b', action='store_true')
>>> parser.add_argument('--func_arg', type=int)
>>> parsed_args = parser.parse_args('--run_a_not_b --arg 42'.split())
>>> parsed_args = parser.parse_args('--run_a_not_b --func_arg 42'.split())
>>> parsed_args
Namespace(func_arg=42, run_a_not_b=True)

So, you can simply get parsed_args.func_arg for the value if you choose in this example.

Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115
0

You might consider using fabric for this.

Josh Bleecher Snyder
  • 8,262
  • 3
  • 35
  • 37