mardi 27 juin 2017

Python command handling

Basically I've created a command-line python menu for fun and practice. The script has many fun features and commands, but the center of it is the actual command line that checks to see if the string entered corresponds to an option and executes it. I'm using if and elif to check the command, which has its downsides.

elif cmdprompt == '--help':
    print("\n"
    self.help()

The code above will take the user's input and if it is '--help' then it will run that function. However, if the user entered a string with any spaces, then it would be recognized as an invalid option. Using "or" seems to be inefficient, and it breaks my code if I use it with a lot of elifs. Is there any way I could make this command handling more flexible? Perhaps something that would just check to make sure that the user enter "--help" with any amount of spaces at the end.

Aucun commentaire:

Enregistrer un commentaire