Reputation: 4253
I want to run python functions from linux command prompt. So, I define a functions and dictionary of mappings from linux inputs to the functions to be exectured. So,eg.
def help():
print(...)
def dostuff():
do sth
functions ={
'help': help()'
''dostuff' : dostuff()
}
def parsecommand():
return functions[sys.argv]
if __name__='__main__':
parsecommand()
When I now run a function from Linux, [python filename.py] help the problem now is that python runs through all the functions in the dictionary and executes them when building the dictionary. How can I avoid this? Or what would be a better way to structure this?
Upvotes: 0
Views: 94
Reputation: 170074
Remove the ()
next to the function names in the dictionary.
functions ={
'help': help
'dostuff' : dostuff
}
You'd than be able to run the functions like so:
return functions[sys.argv[1]]()
Upvotes: 2
Reputation: 456
functions = {
'help': help,
'dostuff': dostuff
}
def parsecommand():
return functions[sys.argv]()
Upvotes: 0
Reputation: 500357
Change your dict like so:
functions = {
'help': help,
'dostuff' : dostuff
}
This will allow you to call the functions in the following manner:
functions[sys.argv[1]]()
Upvotes: 3