forked from lix-project/lix
MultiCommand: Simplify construction
This commit is contained in:
parent
aa0e2a2e70
commit
15a16e5c05
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
Commands * RegisterCommand::commands = 0;
|
std::vector<ref<Command>> * RegisterCommand::commands = 0;
|
||||||
|
|
||||||
void Command::printHelp(const string & programName, std::ostream & out)
|
void Command::printHelp(const string & programName, std::ostream & out)
|
||||||
{
|
{
|
||||||
|
@ -21,9 +21,11 @@ void Command::printHelp(const string & programName, std::ostream & out)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MultiCommand::MultiCommand(const Commands & _commands)
|
MultiCommand::MultiCommand(const std::vector<ref<Command>> & _commands)
|
||||||
: commands(_commands)
|
|
||||||
{
|
{
|
||||||
|
for (auto & command : _commands)
|
||||||
|
commands.emplace(command->name(), command);
|
||||||
|
|
||||||
expectedArgs.push_back(ExpectedArg{"command", 1, true, [=](std::vector<std::string> ss) {
|
expectedArgs.push_back(ExpectedArg{"command", 1, true, [=](std::vector<std::string> ss) {
|
||||||
assert(!command);
|
assert(!command);
|
||||||
auto i = commands.find(ss[0]);
|
auto i = commands.find(ss[0]);
|
||||||
|
|
|
@ -173,7 +173,7 @@ public:
|
||||||
|
|
||||||
std::shared_ptr<Command> command;
|
std::shared_ptr<Command> command;
|
||||||
|
|
||||||
MultiCommand(const Commands & commands);
|
MultiCommand(const std::vector<ref<Command>> & commands);
|
||||||
|
|
||||||
void printHelp(const string & programName, std::ostream & out) override;
|
void printHelp(const string & programName, std::ostream & out) override;
|
||||||
|
|
||||||
|
@ -185,12 +185,12 @@ public:
|
||||||
/* A helper class for registering commands globally. */
|
/* A helper class for registering commands globally. */
|
||||||
struct RegisterCommand
|
struct RegisterCommand
|
||||||
{
|
{
|
||||||
static Commands * commands;
|
static std::vector<ref<Command>> * commands;
|
||||||
|
|
||||||
RegisterCommand(ref<Command> command)
|
RegisterCommand(ref<Command> command)
|
||||||
{
|
{
|
||||||
if (!commands) commands = new Commands;
|
if (!commands) commands = new std::vector<ref<Command>>;
|
||||||
commands->emplace(command->name(), command);
|
commands->push_back(command);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -57,10 +57,15 @@ struct NixArgs : virtual MultiCommand, virtual MixCommonArgs
|
||||||
"--help-config' for a list of configuration settings.\n";
|
"--help-config' for a list of configuration settings.\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void printHelp(const string & programName, std::ostream & out)
|
||||||
|
{
|
||||||
|
MultiCommand::printHelp(programName, out);
|
||||||
|
std::cout << "\nNote: this program is EXPERIMENTAL and subject to change.\n";
|
||||||
|
}
|
||||||
|
|
||||||
void showHelpAndExit()
|
void showHelpAndExit()
|
||||||
{
|
{
|
||||||
printHelp(programName, std::cout);
|
printHelp(programName, std::cout);
|
||||||
std::cout << "\nNote: this program is EXPERIMENTAL and subject to change.\n";
|
|
||||||
throw Exit();
|
throw Exit();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue