funbiscuit / embedded-cli

Single-header CLI with history and autocompletion for embedded systems (like Arduino or STM32)
MIT License
242 stars 38 forks source link

Support "Hidden" Commands #46

Closed mtgstuber closed 5 months ago

mtgstuber commented 9 months ago

This submission adds an additional "hidden" boolean to the command binding structure. Commands with the "hidden" bit set are not shown in the help menu or provided in autocomplete.

Why have hidden commands? One answer is synonyms. Maybe I want to have "quit" as a synonym to exit without taking up space in the help menu. Another is advanced features: Maybe I want some expert commands that aren't shown to the user.

codecov[bot] commented 9 months ago

Codecov Report

Attention: 4 lines in your changes are missing coverage. Please review.

Comparison is base (ffa8014) 82.67% compared to head (f453223) 82.09%.

Files Patch % Lines
lib/src/embedded_cli.c 57.14% 1 Missing and 2 partials :warning:
examples/linux-example/main.cpp 0.00% 1 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #46 +/- ## ========================================== - Coverage 82.67% 82.09% -0.59% ========================================== Files 2 2 Lines 560 564 +4 Branches 135 138 +3 ========================================== Hits 463 463 - Misses 63 65 +2 - Partials 34 36 +2 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

funbiscuit commented 9 months ago

This is a bit controversial. Adding extra argument to CliCommandBinding will force all users to provide it always, even if they don't ever need this "hidden" feature. And it's also a breaking change.

Maybe if we make a breaking change anyway, it would be better to change api of binding addition? Like so:

CliCommandBinding* embeddedCliAddBinding(EmbeddedCli *cli, const char* name)

So only command name is a required thing of a binding. And then we set only neccessary fields on returned binding itself:

CliCommandBinding* exit = embeddedCliAddBinding(cli, "exit");
exit->binding = onExit;

CliCommandBinding* quit = embeddedCliAddBinding(cli, "quit");
quit->binding = onExit;
quit->hidden = true;

Another thing that I don't like is that we're spending another byte on this bool value while we already have a tokenizeArgs flag on a binding. So probable, since this is a breaking change anyway, we should remove that flag and add new u8 called flags. And use is as bitflags. Then hidden can be renamed to autocomplete (if false, autocompletion not done). And help can be hidden if help string is null.

So tell me what do you think of these suggestions. And tell me if you're willing to add another PR first on all this command binding refactor and then update this PR to use new "binding api".

funbiscuit commented 5 months ago

Closing as stale.