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