1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-30 11:43:15 +02:00

Move Command and MultiCommand to libutil

(cherry picked from commit f70434b1fb)
This commit is contained in:
Eelco Dolstra 2018-11-22 16:03:31 +01:00
parent f1b5c76c1a
commit f964f428fe
5 changed files with 116 additions and 117 deletions

View file

@ -188,6 +188,48 @@ public:
friend class MultiCommand;
};
/* A command is an argument parser that can be executed by calling its
run() method. */
struct Command : virtual Args
{
virtual ~Command() { }
virtual std::string name() = 0;
virtual void prepare() { };
virtual void run() = 0;
struct Example
{
std::string description;
std::string command;
};
typedef std::list<Example> Examples;
virtual Examples examples() { return Examples(); }
void printHelp(const string & programName, std::ostream & out) override;
};
typedef std::map<std::string, ref<Command>> Commands;
/* An argument parser that supports multiple subcommands,
i.e. <command> <subcommand>. */
class MultiCommand : virtual Args
{
public:
Commands commands;
std::shared_ptr<Command> command;
MultiCommand(const std::vector<ref<Command>> & commands);
void printHelp(const string & programName, std::ostream & out) override;
bool processFlag(Strings::iterator & pos, Strings::iterator end) override;
bool processArgs(const Strings & args, bool finish) override;
};
Strings argvToStrings(int argc, char * * argv);
/* Helper function for rendering argument labels. */