From 028f137367c21edcb4f21d85db9c04105560c804 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Mon, 13 Apr 2026 20:47:17 -0700 Subject: [PATCH] [cli] add support to configure prompt output (#12884) This commit introduces the ability to configure whether the CLI interpreter outputs the prompt string (`> `) at runtime. - Adds `mPromptEnabled` boolean flag (enabled by default) under the `OPENTHREAD_CONFIG_CLI_PROMPT_ENABLE` configuration. - Adds the `Interpreter::SetPromptConfig()` method to toggle this behavior. - Updates `Interpreter::OutputPrompt()` to check `mPromptEnabled` before emitting the prompt string. --- src/cli/cli.cpp | 18 +++++++++++++++--- src/cli/cli.hpp | 14 ++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 06ad4972d..da3f6f1da 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -79,6 +79,9 @@ Interpreter::Interpreter(Instance *aInstance, otCliOutputCallback aCallback, voi , Utils(aInstance, *this) , mCommandIsPending(false) , mInternalDebugCommand(false) +#if OPENTHREAD_CONFIG_CLI_PROMPT_ENABLE + , mPromptEnabled(true) +#endif , mTimer(*aInstance, HandleTimer, this) #if OPENTHREAD_FTD || OPENTHREAD_MTD #if OPENTHREAD_CONFIG_SNTP_CLIENT_ENABLE @@ -415,6 +418,10 @@ otError Interpreter::SetUserCommands(const otCliCommand *aCommands, uint8_t aLen return error; } +#if OPENTHREAD_CONFIG_CLI_PROMPT_ENABLE +void Interpreter::SetPromptConfig(bool aEnabled) { mPromptEnabled = aEnabled; } +#endif + #if OPENTHREAD_FTD || OPENTHREAD_MTD /** @@ -8427,7 +8434,9 @@ void Interpreter::Initialize(otInstance *aInstance, otCliOutputCallback aCallbac void Interpreter::OutputPrompt(void) { #if OPENTHREAD_CONFIG_CLI_PROMPT_ENABLE - static const char sPrompt[] = "> "; + static const char kPrompt[] = "> "; + + VerifyOrExit(mPromptEnabled); // The `OutputFormat()` below is adding the prompt which is not // part of any command output, so we set the `EmittingCommandOutput` @@ -8435,9 +8444,12 @@ void Interpreter::OutputPrompt(void) // log (under `OPENTHREAD_CONFIG_CLI_LOG_INPUT_OUTPUT_ENABLE`). SetEmittingCommandOutput(false); - OutputFormat("%s", sPrompt); + OutputFormat("%s", kPrompt); SetEmittingCommandOutput(true); -#endif // OPENTHREAD_CONFIG_CLI_PROMPT_ENABLE + +exit: + return; +#endif } void Interpreter::HandleTimer(Timer &aTimer) diff --git a/src/cli/cli.hpp b/src/cli/cli.hpp index c120e2b64..eb6a4c865 100644 --- a/src/cli/cli.hpp +++ b/src/cli/cli.hpp @@ -184,6 +184,17 @@ public: */ otError SetUserCommands(const otCliCommand *aCommands, uint8_t aLength, void *aContext); +#if OPENTHREAD_CONFIG_CLI_PROMPT_ENABLE + /** + * Configures whether or not the CLI interpreter outputs prompt string. + * + * It is enabled by default. + * + * @param[in] aEnabled TRUE to enable outputting prompt, FALSE to disable. + */ + void SetPromptConfig(bool aEnabled); +#endif + protected: static Interpreter *sInterpreter; @@ -353,6 +364,9 @@ private: UserCommandsEntry mUserCommands[kMaxUserCommandEntries]; bool mCommandIsPending; bool mInternalDebugCommand; +#if OPENTHREAD_CONFIG_CLI_PROMPT_ENABLE + bool mPromptEnabled; +#endif TimerMilliContext mTimer;