Invoke python function from command line directly - lifuzu/cafe GitHub Wiki

#!/usr/bin/env python
# Name: call.py
# Author: Richard Lee
# Reference: https://github.com/lifuzu/cafe/wiki/Invoke-python-function-from-command-line-directly
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
⚠️ **GitHub.com Fallback** ⚠️