Reputation: 423
I am wanting to create a CLI that will accept input from the user and run commands based on what they input.
def apple():
print("Apple")
def orange():
print("Orange")
def grape():
print("Grape")
userinput = input("Which function do you want to run? ")
userinput()
What I am trying to get to work is, when the user enters "Orange" it will print Orange. Same for apple and grape. My real project will incorporate a lot more functions the user can input but this is the part I am stuck on at the moment.
Upvotes: 0
Views: 60
Reputation: 811
If I understood you correctly, here's how I would implement something like that:
class Commands:
@staticmethod
def _apple():
print("Apple")
@staticmethod
def _orange():
print("Orange")
@staticmethod
def _grape():
print("Grape")
def run(self, cmd_name):
getattr(Commands, '_' + cmd_name.lower())()
You can then run it with Commands.run(input())
Upvotes: 1