Skip to content

Instantly share code, notes, and snippets.

@cgreer
Last active December 23, 2015 03:38
Show Gist options
  • Select an option

  • Save cgreer/6574351 to your computer and use it in GitHub Desktop.

Select an option

Save cgreer/6574351 to your computer and use it in GitHub Desktop.
Quickly run a function in a script from the command line without option parsing (useful for pipeline creation)
'''
USAGE:
Let's say you have a "find_words_with_letter" function (not method) in a script called "script_name.py"...
def find_words_with_letter(fileName, letter = "g"):
#fxn code here
...you can then call the function in the script from the command line - function name 1st argument, function args follow.
python script_name.py find_words_with_letter FILENAME "B"
TIP:
- grep "def" script_name.py will give a quick list of functions for script and arguments
'''
if __name__ == "__main__":
import sys
assert sys.argv[1] in globals(), "Need name of fxn to run from command line!"
fxnToRun = globals()[sys.argv[1]]
fxnToRun(*sys.argv[2:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment