2023-10-10 21:01:09 +00:00
|
|
|
///
|
|
|
|
/// \file Commander.h
|
2023-10-11 00:24:29 +00:00
|
|
|
/// \author K. Isom <kyle@imap.cc>
|
|
|
|
/// \date 2023-10-10
|
2023-10-10 21:01:09 +00:00
|
|
|
/// \brief Subprogram tooling.
|
2023-10-11 00:24:29 +00:00
|
|
|
///
|
|
|
|
/// Commander is tooling for creating subcommand interfaces for command-line
|
|
|
|
/// programs. For an example, see phonebook.cc.
|
|
|
|
///
|
|
|
|
/// The basic idea is to enable writing programs of the form
|
|
|
|
/// ```
|
|
|
|
/// $ some_tool subcommand args...
|
|
|
|
/// ```
|
|
|
|
///
|
2023-10-10 21:01:09 +00:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef KLIB_COMMANDER_H
|
|
|
|
#define KLIB_COMMANDER_H
|
|
|
|
|
|
|
|
namespace klib {
|
|
|
|
|
|
|
|
|
2023-10-11 00:24:29 +00:00
|
|
|
/// CommanderFunc describes a function that can be run in Commander.
|
|
|
|
///
|
|
|
|
/// It expects an argument count and a list of arguments.
|
2023-10-10 21:01:09 +00:00
|
|
|
typedef std::function<bool(int, char **)> CommanderFunc;
|
|
|
|
|
|
|
|
|
2023-10-11 00:24:29 +00:00
|
|
|
/// Subcommands are the individual commands for the program. A Subcommand
|
|
|
|
/// will check that it has enough arguments before running its function.
|
2023-10-10 21:01:09 +00:00
|
|
|
class Subcommand {
|
|
|
|
public:
|
2023-10-11 00:24:29 +00:00
|
|
|
/// Status describes the results of running a Subcommand.
|
2023-10-10 21:01:09 +00:00
|
|
|
enum class Status : uint8_t {
|
2023-10-11 00:24:29 +00:00
|
|
|
/// The subcommand executed correctly.
|
2023-10-10 21:01:09 +00:00
|
|
|
OK = 0,
|
2023-10-11 00:24:29 +00:00
|
|
|
/// Not enough arguments were supplied to the subcommand.
|
2023-10-10 21:01:09 +00:00
|
|
|
NotEnoughArgs = 1,
|
2023-10-11 00:24:29 +00:00
|
|
|
/// The subcommand failed to run correctly.
|
2023-10-10 21:01:09 +00:00
|
|
|
Failed = 2,
|
2023-10-11 00:24:29 +00:00
|
|
|
/// The subcommand hasn't been registered in a Commander
|
|
|
|
/// instance.
|
2023-10-10 21:01:09 +00:00
|
|
|
CommandNotRegistered = 3,
|
|
|
|
};
|
|
|
|
|
2023-10-11 00:24:29 +00:00
|
|
|
/// A Subcommand is initialized with a name, the number of arguments
|
|
|
|
/// it requires, and a function to run.
|
|
|
|
///
|
|
|
|
/// \param name The subcommand name; this is the name that will select this command.
|
|
|
|
/// \param argc The minimum number of arguments required by this subcommand.
|
|
|
|
/// \param func A valid CommanderFunc.
|
2023-10-10 21:01:09 +00:00
|
|
|
Subcommand(std::string name, int argc, CommanderFunc func)
|
|
|
|
: fn(func), args(argc), command(name)
|
|
|
|
{}
|
|
|
|
|
2023-10-11 00:24:29 +00:00
|
|
|
/// Name returns the name of this subcommand.
|
2023-10-10 21:01:09 +00:00
|
|
|
std::string Name() { return this->command; }
|
2023-10-11 00:24:29 +00:00
|
|
|
|
|
|
|
/// Run attempts to run the CommanderFunc for this subcommand.
|
|
|
|
///
|
|
|
|
/// \param argc The number of arguments supplied.
|
|
|
|
/// \param argv The argument list.
|
|
|
|
/// \return A Status type indicating the status of running the command.
|
2023-10-10 21:01:09 +00:00
|
|
|
Status Run(int argc, char **argv);
|
|
|
|
private:
|
|
|
|
CommanderFunc fn;
|
|
|
|
int args;
|
|
|
|
std::string command;
|
|
|
|
};
|
|
|
|
|
2023-10-11 00:24:29 +00:00
|
|
|
/// Commander collects subcommands and can run the apppropriate one.
|
|
|
|
///
|
|
|
|
/// For example:
|
|
|
|
/// ```
|
|
|
|
/// auto command = string(argv[optind++]);
|
|
|
|
/// Commander commander;
|
|
|
|
///
|
|
|
|
/// commander.Register(Subcommand("list", 0, listFiles));
|
|
|
|
/// commander.Register(Subcommand("new", 1, newPhonebook));
|
|
|
|
/// commander.Register(Subcommand("del", 1, delKey));
|
|
|
|
/// commander.Register(Subcommand("has", 1, hasKey));
|
|
|
|
/// commander.Register(Subcommand("get", 1, getKey));
|
|
|
|
/// commander.Register(Subcommand("put", 2, putKey));
|
|
|
|
///
|
|
|
|
/// auto result = commander.Run(command, argc-optind, argv+optind);
|
|
|
|
/// ```
|
2023-10-10 21:01:09 +00:00
|
|
|
///
|
|
|
|
class Commander {
|
|
|
|
public:
|
2023-10-11 00:24:29 +00:00
|
|
|
/// A Commander is initialized empty.
|
2023-10-10 21:01:09 +00:00
|
|
|
Commander();
|
|
|
|
|
2023-10-11 00:24:29 +00:00
|
|
|
/// Register adds the subcommand. It will be copied into the Commander.
|
2023-10-10 21:01:09 +00:00
|
|
|
bool Register(Subcommand scmd);
|
2023-10-11 00:24:29 +00:00
|
|
|
|
|
|
|
/// Try to run a subcommand registered with this Commander.
|
2023-10-10 21:01:09 +00:00
|
|
|
Subcommand::Status Run(std::string command, int argc, char **argv);
|
|
|
|
private:
|
|
|
|
std::map<std::string, Subcommand *> cmap;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // klib
|
|
|
|
|
|
|
|
|
|
|
|
#endif //KLIB_COMMANDER_H
|