diff options
author | Eelco Dolstra <edolstra@gmail.com> | 2018-11-22 15:59:52 +0100 |
---|---|---|
committer | Eelco Dolstra <edolstra@gmail.com> | 2019-12-05 20:10:35 +0100 |
commit | f1b5c76c1a5f69b795bf0b5c1afb1853f81225ef (patch) | |
tree | c8819a32d9a8f7a3c889d373d08a32b92443bb8b /src/nix | |
parent | 092af3c82692cb346dfac0dcb1bafe46703682ca (diff) |
MultiCommand: Simplify construction
(cherry picked from commit 15a16e5c05d547ec07170df2392263e5e891447b)
Diffstat (limited to 'src/nix')
-rw-r--r-- | src/nix/command.cc | 8 | ||||
-rw-r--r-- | src/nix/command.hh | 8 | ||||
-rw-r--r-- | src/nix/main.cc | 7 |
3 files changed, 15 insertions, 8 deletions
diff --git a/src/nix/command.cc b/src/nix/command.cc index 724f03e5d..a40a113db 100644 --- a/src/nix/command.cc +++ b/src/nix/command.cc @@ -5,7 +5,7 @@ namespace nix { -Commands * RegisterCommand::commands = 0; +std::vector<ref<Command>> * RegisterCommand::commands = 0; void Command::printHelp(const string & programName, std::ostream & out) { @@ -22,9 +22,11 @@ void Command::printHelp(const string & programName, std::ostream & out) } } -MultiCommand::MultiCommand(const Commands & _commands) - : commands(_commands) +MultiCommand::MultiCommand(const std::vector<ref<Command>> & _commands) { + for (auto & command : _commands) + commands.emplace(command->name(), command); + expectedArgs.push_back(ExpectedArg{"command", 1, true, [=](std::vector<std::string> ss) { assert(!command); auto i = commands.find(ss[0]); diff --git a/src/nix/command.hh b/src/nix/command.hh index 2db22dba2..be56f8992 100644 --- a/src/nix/command.hh +++ b/src/nix/command.hh @@ -182,7 +182,7 @@ public: 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; @@ -194,12 +194,12 @@ public: /* A helper class for registering commands globally. */ struct RegisterCommand { - static Commands * commands; + static std::vector<ref<Command>> * commands; RegisterCommand(ref<Command> command) { - if (!commands) commands = new Commands; - commands->emplace(command->name(), command); + if (!commands) commands = new std::vector<ref<Command>>; + commands->push_back(command); } }; diff --git a/src/nix/main.cc b/src/nix/main.cc index 1c9d909d8..d5cba7fb9 100644 --- a/src/nix/main.cc +++ b/src/nix/main.cc @@ -104,10 +104,15 @@ struct NixArgs : virtual MultiCommand, virtual MixCommonArgs "--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() { printHelp(programName, std::cout); - std::cout << "\nNote: this program is EXPERIMENTAL and subject to change.\n"; throw Exit(); } }; |