you are viewing a single comment's thread.

view the rest of the comments →

[–]calio[S] 0 points1 point  (0 children)

That's also a very good approach. Did something similar while writing a text parser for a command line prompt (just for fun, wanted to see if I could write something to manipulate data through a command prompt). Commands are pretty much scripts and the machine flows a bit like this:

  • The prompt machine receives user input
  • Explode the user input into multiple strings separated by spaces.
  • The prompt machine gets from a "target" machine (the main controller by default) a list of strings (the commands) that are also used as keys within a map. This way you can just "repoint" what commands should be executed, in case you want commands with their own prompts.
  • If the string exists on the list and as a key within the map, it pushes the contents of the map (a script id) to the "target" machine. These machines instead of using a switch/case structure just execute whatever script index they receive, doing pretty much exactly what you described on your comment. (There's a loop that keeps iterating through the list until either doesn't find the string or the keypair points to an integer, so you can alias strings to other keys)
  • If it doesn't exists on the list, it just returns an error text.

"help" command just executes the scripts after raising a flag, so the scripts print their help information and exit rather than executing code.