pkgman: Organize commands by category
This commit is contained in:
@@ -23,11 +23,12 @@ compare_commands_by_name(const Command* a, const Command* b)
|
||||
|
||||
|
||||
Command::Command(const BString& name, const BString& shortUsage,
|
||||
const BString& longUsage)
|
||||
const BString& longUsage, const BString& category)
|
||||
:
|
||||
fName(name),
|
||||
fShortUsage(shortUsage),
|
||||
fLongUsage(longUsage)
|
||||
fLongUsage(longUsage),
|
||||
fCategory(category)
|
||||
{
|
||||
fShortUsage.ReplaceAll("%command%", fName);
|
||||
fLongUsage.ReplaceAll("%command%", fName);
|
||||
@@ -100,6 +101,17 @@ CommandManager::GetCommands(const char* prefix, CommandList& _commands)
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
CommandManager::GetCommandsForCategory(const char* category,
|
||||
CommandList& _commands)
|
||||
{
|
||||
for (int32 i = 0; Command* command = fCommands.ItemAt(i); i++) {
|
||||
if (command->Category() == category)
|
||||
_commands.AddItem(command);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
CommandManager::CommandManager()
|
||||
:
|
||||
fCommands(20, true)
|
||||
|
||||
@@ -17,14 +17,16 @@ class Command {
|
||||
public:
|
||||
Command(const BString& name,
|
||||
const BString& shortUsage,
|
||||
const BString& longUsage);
|
||||
const BString& longUsage,
|
||||
const BString& category);
|
||||
virtual ~Command();
|
||||
|
||||
void Init(const char* programName);
|
||||
|
||||
const BString& Name() const { return fName; }
|
||||
const BString& ShortUsage() const { return fShortUsage; }
|
||||
const BString& LongUsage() const { return fName; }
|
||||
const BString& LongUsage() const { return fLongUsage; }
|
||||
const BString& Category() const { return fCategory; }
|
||||
|
||||
void PrintUsage(bool error) const;
|
||||
void PrintUsageAndExit(bool error) const;
|
||||
@@ -35,6 +37,7 @@ private:
|
||||
BString fName;
|
||||
BString fShortUsage;
|
||||
BString fLongUsage;
|
||||
BString fCategory;
|
||||
};
|
||||
|
||||
|
||||
@@ -52,6 +55,8 @@ public:
|
||||
{ return fCommands; }
|
||||
void GetCommands(const char* prefix,
|
||||
CommandList& _commands);
|
||||
void GetCommandsForCategory(const char* category,
|
||||
CommandList& _commands);
|
||||
|
||||
private:
|
||||
CommandManager();
|
||||
@@ -70,11 +75,11 @@ struct CommandRegistrar {
|
||||
};
|
||||
|
||||
|
||||
#define DEFINE_COMMAND(className, name, shortUsage, longUsage) \
|
||||
#define DEFINE_COMMAND(className, name, shortUsage, longUsage, category) \
|
||||
struct className : Command { \
|
||||
className() \
|
||||
: \
|
||||
Command(name, shortUsage, longUsage) \
|
||||
Command(name, shortUsage, longUsage, category) \
|
||||
{ \
|
||||
} \
|
||||
\
|
||||
|
||||
@@ -38,7 +38,8 @@ static const char* const kLongUsage =
|
||||
"\n";
|
||||
|
||||
|
||||
DEFINE_COMMAND(AddRepoCommand, "add-repo", kShortUsage, kLongUsage)
|
||||
DEFINE_COMMAND(AddRepoCommand, "add-repo", kShortUsage, kLongUsage,
|
||||
kCommandCategoryRepositories)
|
||||
|
||||
|
||||
int
|
||||
|
||||
@@ -36,7 +36,8 @@ static const char* const kLongUsage =
|
||||
"\n";
|
||||
|
||||
|
||||
DEFINE_COMMAND(DropRepoCommand, "drop-repo", kShortUsage, kLongUsage)
|
||||
DEFINE_COMMAND(DropRepoCommand, "drop-repo", kShortUsage, kLongUsage,
|
||||
kCommandCategoryRepositories)
|
||||
|
||||
|
||||
int
|
||||
|
||||
@@ -38,7 +38,8 @@ static const char* const kLongUsage =
|
||||
"\n";
|
||||
|
||||
|
||||
DEFINE_COMMAND(InstallCommand, "install", kShortUsage, kLongUsage)
|
||||
DEFINE_COMMAND(InstallCommand, "install", kShortUsage, kLongUsage,
|
||||
kCommandCategoryPackages)
|
||||
|
||||
|
||||
int
|
||||
|
||||
@@ -42,7 +42,8 @@ static const char* const kLongUsage =
|
||||
"\n";
|
||||
|
||||
|
||||
DEFINE_COMMAND(ListReposCommand, "list-repos", kShortUsage, kLongUsage)
|
||||
DEFINE_COMMAND(ListReposCommand, "list-repos", kShortUsage, kLongUsage,
|
||||
kCommandCategoryRepositories)
|
||||
|
||||
|
||||
int
|
||||
|
||||
@@ -37,7 +37,8 @@ static const char* const kLongUsage =
|
||||
"\n";
|
||||
|
||||
|
||||
DEFINE_COMMAND(RefreshCommand, "refresh", kShortUsage, kLongUsage)
|
||||
DEFINE_COMMAND(RefreshCommand, "refresh", kShortUsage, kLongUsage,
|
||||
kCommandCategoryRepositories)
|
||||
|
||||
|
||||
int
|
||||
|
||||
@@ -55,7 +55,7 @@ static const char* const kLongUsage =
|
||||
|
||||
|
||||
DEFINE_COMMAND(ResolveDependenciesCommand, "resolve-dependencies", kShortUsage,
|
||||
kLongUsage)
|
||||
kLongUsage, kCommandCategoryOther)
|
||||
|
||||
|
||||
static void
|
||||
|
||||
@@ -47,7 +47,8 @@ static const char* const kLongUsage =
|
||||
"\n";
|
||||
|
||||
|
||||
DEFINE_COMMAND(SearchCommand, "search", kShortUsage, kLongUsage)
|
||||
DEFINE_COMMAND(SearchCommand, "search", kShortUsage, kLongUsage,
|
||||
kCommandCategoryPackages)
|
||||
|
||||
|
||||
static int
|
||||
|
||||
@@ -38,7 +38,8 @@ static const char* const kLongUsage =
|
||||
"\n";
|
||||
|
||||
|
||||
DEFINE_COMMAND(UninstallCommand, "uninstall", kShortUsage, kLongUsage)
|
||||
DEFINE_COMMAND(UninstallCommand, "uninstall", kShortUsage, kLongUsage,
|
||||
kCommandCategoryPackages)
|
||||
|
||||
|
||||
int
|
||||
|
||||
@@ -18,27 +18,51 @@ extern const char* __progname;
|
||||
const char* kProgramName = __progname;
|
||||
|
||||
|
||||
const BString kCommandCategoryPackages("packages");
|
||||
const BString kCommandCategoryRepositories("repositories");
|
||||
const BString kCommandCategoryOther("other");
|
||||
|
||||
|
||||
static const char* const kUsage =
|
||||
"Usage: %s <command> <command args>\n"
|
||||
"Manages packages and package repository.\n"
|
||||
"Manages packages and package repositories.\n"
|
||||
"\n"
|
||||
"Commands:\n"
|
||||
"Package management commands:\n"
|
||||
"%s"
|
||||
"Repository management commands:\n"
|
||||
"%s"
|
||||
"Other commands:\n"
|
||||
"%s"
|
||||
"Common Options:\n"
|
||||
" -h, --help - Print this usage info.\n"
|
||||
" -h, --help - Print usage info.\n"
|
||||
;
|
||||
|
||||
|
||||
static BString
|
||||
get_commands_usage_for_category(const char* category)
|
||||
{
|
||||
BString commandsUsage;
|
||||
CommandList commands;
|
||||
CommandManager::Default()->GetCommandsForCategory(category, commands);
|
||||
for (int32 i = 0; Command* command = commands.ItemAt(i); i++)
|
||||
commandsUsage << command->ShortUsage() << '\n';
|
||||
return commandsUsage;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
print_usage_and_exit(bool error)
|
||||
{
|
||||
BString commandsUsage;
|
||||
const CommandList& commands = CommandManager::Default()->Commands();
|
||||
for (int32 i = 0; Command* command = commands.ItemAt(i); i++)
|
||||
commandsUsage << command->ShortUsage() << '\n';
|
||||
BString packageCommandsUsage
|
||||
= get_commands_usage_for_category(kCommandCategoryPackages);
|
||||
BString repositoryCommandsUsage
|
||||
= get_commands_usage_for_category(kCommandCategoryRepositories);
|
||||
BString otherCommandsUsage
|
||||
= get_commands_usage_for_category(kCommandCategoryOther);
|
||||
|
||||
fprintf(error ? stderr : stdout, kUsage, kProgramName,
|
||||
commandsUsage.String());
|
||||
packageCommandsUsage.String(), repositoryCommandsUsage.String(),
|
||||
otherCommandsUsage.String());
|
||||
|
||||
exit(error ? 1 : 0);
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <String.h>
|
||||
|
||||
|
||||
extern const char* kProgramName;
|
||||
|
||||
@@ -36,4 +38,9 @@ do { \
|
||||
void print_usage_and_exit(bool error);
|
||||
|
||||
|
||||
extern const BString kCommandCategoryPackages;
|
||||
extern const BString kCommandCategoryRepositories;
|
||||
extern const BString kCommandCategoryOther;
|
||||
|
||||
|
||||
#endif // PKGMAN_H
|
||||
|
||||
Reference in New Issue
Block a user