2

I'm writing a GUI application which needs the facility for users to interact with the system and execute tasks via a sort of command line interface.

The user will type commands in a text box, with the following format:

[Command-name] [Param-name] [Value]

Where any number of Param-names and Values can be written after the Command-name.

An example of use:

New_Customer NAME John AGE 34 

The above command would call the appropriate method where, say, a new customer is added to the database.

Another example:

PLAY FILM Harry_Potter VOLUME 50 FPS 24

The obvious and relatively straightforward way to implement this is to parse the user input, using SPACE as a delimiter, and then use a sequence of ifs and elses to process the command.

For basic commands like the ones above, this approach is probably good enough, but as the syntax and semantics of the commands become more complex, I worry that all those ifs and elses will become unwieldy and hard to manage.

Is there a design pattern or common method I may find useful?

6
  • There are several small but very good libraries written for this purpose. Have a look here. Commented Nov 4, 2019 at 20:36
  • @RobertHarvey, wouldn't that require my Param-names to be hyphenated characters?
    – 19172281
    Commented Nov 4, 2019 at 20:55
  • Don't know. You might actually have to write some code. :) Commented Nov 4, 2019 at 20:57
  • @RobertHarvey, would the Command Pattern (en.wikipedia.org/wiki/Command_pattern) be appropriate here?
    – 19172281
    Commented Nov 4, 2019 at 21:00
  • No, there's no off-the shelf solution that won't make the thing far more complicated than it has to be. Experiment with some of the point solutions on the page I linked, and pick the one you like as a starting point. Commented Nov 4, 2019 at 21:10

2 Answers 2

1

The answer is to write a parser. There are tools that can support you with that task.

Wikipedia has a huge list of parser generators, select one that suits you.

Personally I have good experiences with antlr.

The advantage of using a parser generator is, that you have a proper definition of your grammar that you can easily extend in the future.

The downside is, that you have to do some learning in the beginning.

2
  • Do you think this may be overkill considering a command will probably never be more than 8 keywords/arguments long? Is there anything wrong with the simple if/else approach I've used so far?
    – 19172281
    Commented Nov 5, 2019 at 17:59
  • @19172281 no I think this is the right tool for the job. Of course you can hack something together that works now. But if you have a proper grammar defined and code to handle the output of the parser, it's a lot easier to extend, debug and work with.
    – Josef
    Commented Nov 5, 2019 at 18:45
0

In a word Lua.

Lua was designed to be easily embeddable into another application. The lua language is reasonably simple and can be extended to provide application specific interfaces.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.