Skip to content

Commit 276dc68

Browse files
committed
Hide irrelevant commands in cvd help by default
Only show relevant commands and selector options when no devices are running. All commands are shown if devices are running, with device-specific commands displayed first. Output without a running device: ================================ ``` Cuttlefish Virtual Device (CVD) CLI. usage: cvd <selector/driver options> <command> <args> Driver Options: -help Print this message -verbosity=<LEVEL> Adjust Cvd verbosity level. LEVEL is Android log severity. (Required: cvd >= v1.3) Commands (cvd help <command> for more information): cache - Manage the files cached by cvd create - Create a Cuttlefish virtual device or environment fetch, fetch_cvd - Retrieve build artifacts based on branch and target names help - Used to display help information for other commands lint - error checks the input virtual device json config file load - Loads the given JSON configuration file and launches devices based on the options provided login - Acquire credentials reset - Used to stop devices, optionally clean up instance files, and shut down the deprecated cvd server process version - Prints version of cvd client and cvd server ``` Output with a running device: ============================= ``` Cuttlefish Virtual Device (CVD) CLI. usage: cvd <selector/driver options> <command> <args> Selector Options: -group_name <name> Specify the name of the instance group created or selected. -instance_name <name> Selects the device of the given name to perform the commands for. -instance_name <names> Takes the names of the devices to create within an instance group. The 'names' is comma-separated. Device-Specific Commands (cvd help <command> for more information): display - Enables hotplug/unplug of displays from running cuttlefish virtual devices env - Enumerate + Query APIs for all gRPC services made available by this virtual device instance fleet - lists active devices with relevant information clear - Clears the instance database, stopping any running instances first. bugreport, host_bugreport, cvd_host_bugreport - Run cvd bugreport --help for command description stop, stop_cvd - Stop all instances in a group powerbtn - Trigger power button event on the device powerwash - Reset device to first boot state restart - Restart device remove, rm - Remove devices and artifacts from the system. screen_recording - Record screen contents suspend, resume, snapshot_take - Suspend/resume the cuttlefish device, or take snapshot of the device start, launch_cvd - Start a Cuttlefish virtual device or environment status, cvd_status - Query status of a single instance group. Use `cvd fleet` for all devices Driver Options: -help Print this message -verbosity=<LEVEL> Adjust Cvd verbosity level. LEVEL is Android log severity. (Required: cvd >= v1.3) Commands (cvd help <command> for more information): cache - Manage the files cached by cvd create - Create a Cuttlefish virtual device or environment fetch, fetch_cvd - Retrieve build artifacts based on branch and target names help - Used to display help information for other commands lint - error checks the input virtual device json config file load - Loads the given JSON configuration file and launches devices based on the options provided login - Acquire credentials reset - Used to stop devices, optionally clean up instance files, and shut down the deprecated cvd server process version - Prints version of cvd client and cvd server ``` Bug: b/504758273 Assisted-by: Gemini:Next
1 parent a1730e9 commit 276dc68

20 files changed

Lines changed: 86 additions & 15 deletions

base/cvd/cuttlefish/host/commands/cvd/cli/commands/bugreport.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class CvdBugreportCommandHandler : public CvdCommandHandler {
9898
cvd_common::Args CmdList() const override;
9999
Result<std::string> SummaryHelp() const override;
100100
bool ShouldInterceptHelp() const override;
101+
bool RequiresRunningDevice() const override;
101102
Result<std::string> DetailedHelp(std::vector<std::string>&) const override;
102103

103104
private:
@@ -187,6 +188,7 @@ Result<std::string> CvdBugreportCommandHandler::SummaryHelp() const {
187188
}
188189

189190
bool CvdBugreportCommandHandler::ShouldInterceptHelp() const { return false; }
191+
bool CvdBugreportCommandHandler::RequiresRunningDevice() const { return true; }
190192

191193
Result<std::string> CvdBugreportCommandHandler::DetailedHelp(
192194
std::vector<std::string>& arguments) const {

base/cvd/cuttlefish/host/commands/cvd/cli/commands/clear.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class CvdClearCommandHandler : public CvdCommandHandler {
4141
cvd_common::Args CmdList() const override { return {kClearCmd}; }
4242
Result<std::string> SummaryHelp() const override { return kSummaryHelpText; }
4343
bool ShouldInterceptHelp() const override { return true; }
44+
bool RequiresRunningDevice() const override { return true; }
4445
Result<std::string> DetailedHelp(std::vector<std::string>&) const override;
4546

4647
private:

base/cvd/cuttlefish/host/commands/cvd/cli/commands/command_handler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ Result<bool> CvdCommandHandler::CanHandle(const CommandRequest& request) const {
2626
return Contains(CmdList(), request.Subcommand());
2727
}
2828

29+
bool CvdCommandHandler::RequiresRunningDevice() const { return false; }
30+
2931
} // namespace cuttlefish

base/cvd/cuttlefish/host/commands/cvd/cli/commands/command_handler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class CvdCommandHandler {
3535
virtual cvd_common::Args CmdList() const = 0;
3636
// used for command help text
3737
virtual Result<std::string> SummaryHelp() const = 0;
38+
virtual bool RequiresRunningDevice() const;
3839
virtual bool ShouldInterceptHelp() const = 0;
3940
virtual Result<std::string> DetailedHelp(std::vector<std::string>&) const = 0;
4041
};

base/cvd/cuttlefish/host/commands/cvd/cli/commands/display.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ class CvdDisplayCommandHandler : public CvdCommandHandler {
9191

9292
bool ShouldInterceptHelp() const override { return true; }
9393

94+
bool RequiresRunningDevice() const override { return true; }
95+
9496
Result<std::string> DetailedHelp(std::vector<std::string>&) const override {
9597
return kDetailedHelpText;
9698
}

base/cvd/cuttlefish/host/commands/cvd/cli/commands/env.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ class CvdEnvCommandHandler : public CvdCommandHandler {
8989

9090
bool ShouldInterceptHelp() const override { return true; }
9191

92+
bool RequiresRunningDevice() const override { return true; }
93+
9294
Result<std::string> DetailedHelp(std::vector<std::string>&) const override {
9395
return kDetailedHelpText;
9496
}

base/cvd/cuttlefish/host/commands/cvd/cli/commands/fleet.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class CvdFleetCommandHandler : public CvdCommandHandler {
5252

5353
bool ShouldInterceptHelp() const override { return true; }
5454

55+
bool RequiresRunningDevice() const override { return true; }
56+
5557
Result<std::string> DetailedHelp(std::vector<std::string>&) const override {
5658
return kHelpMessage;
5759
}

base/cvd/cuttlefish/host/commands/cvd/cli/commands/help.cpp

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
#include <vector>
2525

2626
#include "absl/strings/str_join.h"
27+
#include "cuttlefish/common/libs/utils/contains.h"
2728
#include "absl/log/check.h"
2829

2930
#include "cuttlefish/host/commands/cvd/cli/command_request.h"
3031
#include "cuttlefish/host/commands/cvd/cli/commands/command_handler.h"
3132
#include "cuttlefish/host/commands/cvd/cli/request_context.h"
3233
#include "cuttlefish/host/commands/cvd/cli/types.h"
34+
#include "cuttlefish/host/commands/cvd/instances/instance_manager.h"
3335
#include "cuttlefish/result/result.h"
3436

3537
namespace cuttlefish {
@@ -38,15 +40,19 @@ namespace {
3840
constexpr char kHelpIntroText[] = R"(Cuttlefish Virtual Device (CVD) CLI.
3941
4042
usage: cvd <selector/driver options> <command> <args>
43+
)";
4144

45+
constexpr char kSelectorOptionsText[] = R"(
4246
Selector Options:
4347
-group_name <name> Specify the name of the instance group created
4448
or selected.
4549
-instance_name <name> Selects the device of the given name to perform the
4650
commands for.
4751
-instance_name <names> Takes the names of the devices to create within an
4852
instance group. The 'names' is comma-separated.
53+
)";
4954

55+
constexpr char kDriverOptionsText[] = R"(
5056
Driver Options:
5157
-help Print this message
5258
-verbosity=<LEVEL> Adjust Cvd verbosity level. LEVEL is Android log
@@ -66,15 +72,20 @@ Example usage:
6672
cvd help <command> - displays more detailed help for the specific command
6773
)";
6874

69-
constexpr char kIgnorableHandlerCommand[] = "experimental";
75+
const std::vector<std::string> kIgnoredCommandLists = {
76+
"experimental",
77+
"server-kill, kill-server, restart-server",
78+
};
7079

7180
} // namespace
7281

7382
class CvdHelpHandler : public CvdCommandHandler {
7483
public:
7584
CvdHelpHandler(
76-
const std::vector<std::unique_ptr<CvdCommandHandler>>& request_handlers)
77-
: request_handlers_(request_handlers) {}
85+
const std::vector<std::unique_ptr<CvdCommandHandler>>& request_handlers,
86+
InstanceManager& instance_manager)
87+
: request_handlers_(request_handlers),
88+
instance_manager_(instance_manager) {}
7889

7990
Result<void> Handle(const CommandRequest& request) override {
8091
CF_EXPECT(CanHandle(request));
@@ -109,20 +120,45 @@ class CvdHelpHandler : public CvdCommandHandler {
109120

110121
Result<std::string> TopLevelHelp() {
111122
std::stringstream help_message;
112-
help_message << kHelpIntroText << std::endl;
123+
help_message << kHelpIntroText;
124+
bool devices_running = CF_EXPECT(instance_manager_.HasInstanceGroups());
125+
if (devices_running) {
126+
help_message << kSelectorOptionsText;
127+
}
128+
129+
if (devices_running) {
130+
help_message << "\nDevice-Specific Commands (cvd help <command> for more information):\n";
131+
for (const auto& handler : request_handlers_) {
132+
if (handler->RequiresRunningDevice()) {
133+
CF_EXPECT(PrintHandler(help_message, *handler));
134+
}
135+
}
136+
}
137+
138+
help_message << kDriverOptionsText << std::endl;
139+
113140
for (const auto& handler : request_handlers_) {
114-
std::string command_list = absl::StrJoin(handler->CmdList(), ", ");
115-
// exclude commands without any command list values as not intended for
116-
// use by users or sub-subcommands
117-
if (!command_list.empty() && command_list != kIgnorableHandlerCommand) {
118-
help_message << "\t" << command_list << " - ";
119-
help_message << CF_EXPECT(handler->SummaryHelp()) << std::endl
120-
<< std::endl;
141+
if (!handler->RequiresRunningDevice()) {
142+
CF_EXPECT(PrintHandler(help_message, *handler));
121143
}
122144
}
145+
123146
return help_message.str();
124147
}
125148

149+
Result<void> PrintHandler(
150+
std::stringstream& help_message,
151+
const CvdCommandHandler& handler) const {
152+
std::string command_list = absl::StrJoin(handler.CmdList(), ", ");
153+
if (!command_list.empty() &&
154+
!Contains(kIgnoredCommandLists, command_list)) {
155+
help_message << "\t" << command_list << " - ";
156+
help_message << CF_EXPECT(handler.SummaryHelp()) << std::endl
157+
<< std::endl;
158+
}
159+
return {};
160+
}
161+
126162
Result<std::string> SubCommandHelp(std::vector<std::string>& args) {
127163
CF_EXPECT(
128164
!args.empty(),
@@ -136,11 +172,14 @@ class CvdHelpHandler : public CvdCommandHandler {
136172
}
137173

138174
const std::vector<std::unique_ptr<CvdCommandHandler>>& request_handlers_;
175+
InstanceManager& instance_manager_;
139176
};
140177

141178
std::unique_ptr<CvdCommandHandler> NewCvdHelpHandler(
142-
const std::vector<std::unique_ptr<CvdCommandHandler>>& server_handlers) {
143-
return std::unique_ptr<CvdCommandHandler>(new CvdHelpHandler(server_handlers));
179+
const std::vector<std::unique_ptr<CvdCommandHandler>>& server_handlers,
180+
InstanceManager& instance_manager) {
181+
return std::unique_ptr<CvdCommandHandler>(
182+
new CvdHelpHandler(server_handlers, instance_manager));
144183
}
145184

146185
} // namespace cuttlefish

base/cvd/cuttlefish/host/commands/cvd/cli/commands/help.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
#include "cuttlefish/host/commands/cvd/cli/commands/command_handler.h"
2323

2424
namespace cuttlefish {
25+
class InstanceManager;
2526

2627
std::unique_ptr<CvdCommandHandler> NewCvdHelpHandler(
27-
const std::vector<std::unique_ptr<CvdCommandHandler>>& server_handlers);
28+
const std::vector<std::unique_ptr<CvdCommandHandler>>& server_handlers,
29+
InstanceManager& instance_manager);
2830
}

base/cvd/cuttlefish/host/commands/cvd/cli/commands/noop.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class CvdNoopHandler : public CvdCommandHandler {
5050

5151
bool ShouldInterceptHelp() const override { return true; }
5252

53+
bool RequiresRunningDevice() const override { return true; }
54+
5355
Result<std::string> DetailedHelp(std::vector<std::string>&) const override {
5456
return "DEPRECATED: This command is a no-op";
5557
}

0 commit comments

Comments
 (0)