Parsing terminals input string

Dear friends,
I am looking for a clever way to “parse” strings from terminal, as user input, to activate functions and actions in my sketch.
I am not experienced in this field so I asking you, that have great ideas, for good suggestions.
To make more clear, what I need to do is a good way to get the users typing at the terminal widget and then, according to the entered text, to execute whatever needed as functions or a bunch of commands etc.

What do you thing?

Thanks and Best Regards,
Mike Kranidis

Have you checked example sketch from the library?

yes, for sure I have…
Also I have expanded some how see me code snippet:

  /// take "commands" from the terminal and acting accordingly ///
  /// ///
  /// (stringOne.equalsIgnoreCase(stringTwo)) ///
  /// stringOne.trim(); ///
  /// int strgLenght = param.getLength(); /// get the string lenght ///
  String getString = param.asStr(); /// get the string ///
  getString.trim(); /// cut off the blank spece(s) at the end of the string ///
  /// terminal.print("String Lenght= ") ;
  /// terminal.println(strgLenght);
  /// getString.toUpperCase(); /// convert all string characters to uppercase ///
  if (getString.equalsIgnoreCase("Marco")) {
    terminal.println("You said: Marco") ;
    terminal.println("I said: Polo") ;
  } else if (getString.equalsIgnoreCase("Clear")) {
    terminal.println("You said: clear , OK I will !") ;
  } else if (getString.equalsIgnoreCase("Startled")) {
    terminal.println("You said: startled , OK I will !") ;
    ledBlink = true;

but I am looking for an easy general, clever way to to it

What’s your goal? What are you trying to achieve?

to have let say 20-30 keywords that if the user write them (one at a time) at the terminal, the parsing of the terminal input to be able to execute various functions etc.
I don’t like to implement this using a pile of

if(bla bla) 
/// call the specific function ///
else if(thisthis)
/// call the specific function ///
etc etc etc

@mikekgr You may try to consider MENU widget. It may better fit your needs.

There is no other way. Maybe switch may help, however it works only with numbers.
Only workaround could be to use button for specific action.

yes, I have tested, it can work this way but I don’t like it… I prefer the terminal for this and a good parsing way.

Thanks for your help

I thought about case but I don’t know how to associate keywords with numbers in a way as 1:clear , 2:runme , 3:functionZero etc so If I get the keyword ex functionZero to execute case 3

Is it possible to implement something like this???

@mikekgr on this occasion a big list of if, else if statements is probably the best way.

You can use Arduino’s startsWith() function so users don’t have to type in full words etc. Ensuring you keep the keywords unique for startsWith().
If you have 20+ keywords I don’t think allocating numbers will be user friendly.

One more thought, instead of :

if(bla bla) 
/// call the specific function ///
else if(thisthis)
/// call the specific function ///
etc etc etc

you can use command pattern :

    String cmd = param.asStr();

void blabla(String cmd) {
   if (cmd == "blabla") {
      //do stuff

void thisthis(String cmd) {
   if (cmd == "thisthis") {
             //do stuff

It is a bit slower, however it much easier to maintain.

1 Like

Thanks a lot. I will try it!