Added support for command details

This commit is contained in:
Alexei KADIR 2024-02-16 17:19:56 +01:00
parent 2c0dd8d9ab
commit d925445876
3 changed files with 27 additions and 19 deletions

View File

@ -6,23 +6,23 @@
#include <string.h>
Command COMMANDS[] = {
{"help", "[command]", "Shows help for all or a specific command.", CMD_HELP},
{"version", "", "Shows the version of the program.", CMD_VERSION},
{NULL},
{"add-entry", "<entry> [--root|-r <size>] [--backing|-b <backing>]", "Adds a new entry to the pool", CMD_ADD_ENTRY},
{"remove-entry", "<entry>", "Removes an entry from the pool", CMD_REMOVE_ENTRY},
{"list-entries", "", "Lists all entries in the pool", CMD_LIST_ENTRIES},
{"clear-entries", "", "Clears all entries from the pool", CMD_CLEAR_ENTRIES},
{NULL},
{"add-backing", "<entry>", "Adds a backing disk to the pool from the given entry", CMD_ADD_BACKING},
{"remove-backing", "<backing>", "Removes a backing disk from the pool", CMD_REMOVE_BACKING},
{"list-backings", "[--tree|-t]", "Lists all backing disks in the pool", CMD_LIST_BACKINGS},
{"clear-backings", "", "Clears all backing disks from the pool", CMD_CLEAR_BACKINGS},
{NULL},
{"start", "<entry> [--no-pci] [--vnc <port> <password>] [--iso <iso>]", "Starts the sandbox with the given entry", CMD_START},
{"power", "", "Sends a power signal to the sandbox", CMD_POWER},
{"stop", "[--force|-f] [--timeout|-t <timeout>]", "Stops the sandbox", CMD_STOP},
{"status", "", "Shows the status of the sandbox", CMD_STATUS},
{CMD_HELP, "help", "[command]", "Shows help for all or a specific command.", "TODO"},
{CMD_VERSION, "version", "", "Shows the version of the program.", "TODO"},
{CMD_SEPARATOR},
{CMD_ADD_ENTRY, "add-entry", "<entry> [--root|-r <size>] [--backing|-b <backing>]", "Adds a new entry to the pool", "TODO"},
{CMD_REMOVE_ENTRY, "remove-entry", "<entry>", "Removes an entry from the pool", "TODO"},
{CMD_LIST_ENTRIES, "list-entries", "", "Lists all entries in the pool", "TODO"},
{CMD_CLEAR_ENTRIES, "clear-entries", "", "Clears all entries from the pool", "TODO"},
{CMD_SEPARATOR},
{CMD_ADD_BACKING, "add-backing", "<entry>", "Adds a backing disk to the pool from the given entry", "TODO"},
{CMD_REMOVE_BACKING, "remove-backing", "<backing>", "Removes a backing disk from the pool", "TODO"},
{CMD_LIST_BACKINGS, "list-backings", "[--tree|-t]", "Lists all backing disks in the pool", "TODO"},
{CMD_CLEAR_BACKINGS, "clear-backings", "", "Clears all backing disks from the pool", "TODO"},
{CMD_SEPARATOR},
{CMD_START, "start", "<entry> [--no-pci] [--vnc <port> <password>] [--iso <iso>]", "Starts the sandbox with the given entry", "TODO"},
{CMD_POWER, "power", "", "Sends a power signal to the sandbox", "TODO"},
{CMD_STOP, "stop", "[--force|-f] [--timeout|-t <timeout>]", "Stops the sandbox", "TODO"},
{CMD_STATUS, "status", "", "Shows the status of the sandbox", "TODO"},
};
Command* find_command(const char* name) {
@ -54,7 +54,7 @@ void show_help() {
printf("Usage: sandbox <command> [args]\n\n");
printf("Commands:\n");
for (size_t i = 0; i < sizeof(COMMANDS) / sizeof(COMMANDS[0]); i++) {
if (COMMANDS[i].name == NULL) {
if (COMMANDS[i].id == CMD_SEPARATOR) {
printf("\n");
continue;
}
@ -69,6 +69,11 @@ void show_help() {
void show_command_help(const Command* command) {
printf("Usage: sandbox %s %s\n", command->name, command->args);
printf(" %s\n", command->description);
if (command->details != NULL) {
printf("\n");
printf("%s\n", command->details);
}
}
int cmd_help(int argc, char** argv) {

View File

@ -4,6 +4,7 @@
typedef enum {
CMD_UNKNOWN,
CMD_SEPARATOR,
CMD_HELP,
CMD_VERSION,
@ -25,10 +26,11 @@ typedef enum {
} CommandID;
typedef struct {
CommandID id;
const char* name;
const char* args;
const char* description;
CommandID id;
const char* details;
} Command;
extern Command COMMANDS[];

View File

@ -25,6 +25,7 @@ int main(int argc, char* argv[]) {
return cmd_help(argc - 2, argv + 2);
case CMD_VERSION:
return cmd_version(argc - 2, argv + 2);
default:
log_message(LOG_ERROR, "Command '%s' is not implemented.", command->name);
break;