Skip to content

Invoke python function from command line directly

lifuzu edited this page Jun 15, 2013 · 2 revisions
#!/usr/bin/env python
# call.py
import sys

def opts(argv):
  opts = {}
  for arg in argv:
    a = arg.split("=")
    opts[a[0]] = a[1]
  return opts

def double(x=3, y='hello'):
  x = int(x)
  print str(x * x) + "." + y

if __name__ == "__main__":
  globals()[sys.argv[1]](**opts(sys.argv[2:]))

Save it as call.py, then you can call the function from shell directly, like this:

$ python call.py double x=6 y="work @anywhere"
36.work @anywhere

Clone this wiki locally