Skip to content
Snippets Groups Projects
Commit ac873f53 authored by Florian Pose's avatar Florian Pose
Browse files

Command abbreviation.

parent a9c04c26
No related branches found
No related tags found
No related merge requests found
...@@ -47,7 +47,7 @@ void command_alias(void) ...@@ -47,7 +47,7 @@ void command_alias(void)
unsigned int numSlaves, i; unsigned int numSlaves, i;
if (commandArgs.size() != 1) { if (commandArgs.size() != 1) {
err << "'" << command << "' takes exactly one argument!"; err << "'" << commandName << "' takes exactly one argument!";
throw InvalidUsageException(err); throw InvalidUsageException(err);
} }
......
...@@ -21,7 +21,7 @@ enum Verbosity { ...@@ -21,7 +21,7 @@ enum Verbosity {
Verbose Verbose
}; };
extern string command; extern string commandName;
extern int slavePosition; extern int slavePosition;
extern int domainIndex; extern int domainIndex;
extern vector<string> commandArgs; extern vector<string> commandArgs;
......
...@@ -20,7 +20,7 @@ string binaryBaseName; ...@@ -20,7 +20,7 @@ string binaryBaseName;
unsigned int masterIndex = 0; unsigned int masterIndex = 0;
int slavePosition = -1; int slavePosition = -1;
int domainIndex = -1; int domainIndex = -1;
string command; string commandName;
vector<string> commandArgs; vector<string> commandArgs;
Verbosity verbosity = Normal; Verbosity verbosity = Normal;
string dataTypeStr; string dataTypeStr;
...@@ -33,6 +33,7 @@ MasterDevice masterDev; ...@@ -33,6 +33,7 @@ MasterDevice masterDev;
/*****************************************************************************/ /*****************************************************************************/
struct Command { struct Command {
const char *name;
void (*func)(void); void (*func)(void);
const char *helpString; const char *helpString;
...@@ -40,27 +41,20 @@ struct Command { ...@@ -40,27 +41,20 @@ struct Command {
void displayHelp(void) const; void displayHelp(void) const;
}; };
struct CommandAlias {
const char *name;
const Command *command;
};
/*****************************************************************************/ /*****************************************************************************/
#define COMMAND(name) \ #define COMMAND(name) \
void command_##name(void); \ void command_##name(void); \
extern const char *help_##name; \ extern const char *help_##name
const Command cmd_##name = {command_##name, help_##name};
#define INIT_COMMAND(name) {#name, command_##name, help_##name}
COMMAND(alias); COMMAND(alias);
COMMAND(config); COMMAND(config);
const CommandAlias commandAliases[] = { static const Command commands[] = {
{"alias", &cmd_alias}, INIT_COMMAND(alias),
INIT_COMMAND(config),
{"config", &cmd_config},
{"conf", &cmd_config},
{"cf", &cmd_config},
}; };
#if 0 #if 0
...@@ -115,6 +109,7 @@ void printUsage() ...@@ -115,6 +109,7 @@ void printUsage()
<< " slaves Show slaves." << endl << " slaves Show slaves." << endl
<< " state Request slave states." << endl << " state Request slave states." << endl
<< " xml Generate slave information xmls." << endl << " xml Generate slave information xmls." << endl
<< "Commands can be generously abbreviated." << endl
<< "Global options:" << endl << "Global options:" << endl
<< " --master -m <master> Index of the master to use. Default: 0" << " --master -m <master> Index of the master to use. Default: 0"
<< endl << endl
...@@ -241,7 +236,7 @@ void getOptions(int argc, char **argv) ...@@ -241,7 +236,7 @@ void getOptions(int argc, char **argv)
exit(!helpRequested); exit(!helpRequested);
} }
command = argv[optind]; commandName = argv[optind];
while (++optind < argc) while (++optind < argc)
commandArgs.push_back(string(argv[optind])); commandArgs.push_back(string(argv[optind]));
} }
...@@ -271,7 +266,41 @@ int Command::execute() const ...@@ -271,7 +266,41 @@ int Command::execute() const
void Command::displayHelp() const void Command::displayHelp() const
{ {
cerr << binaryBaseName << " " << command << " " << helpString; cerr << binaryBaseName << " " << commandName << " " << helpString;
}
/****************************************************************************/
bool abbrevMatch(const string &abb, const string &full)
{
unsigned int abbIndex;
size_t fullPos = 0;
for (abbIndex = 0; abbIndex < abb.length(); abbIndex++) {
fullPos = full.find(abb[abbIndex], fullPos);
if (fullPos == string::npos)
return false;
}
return true;
}
/****************************************************************************/
list<const Command *> getMatchingCommands(const string &cmdStr)
{
const Command *cmd, *endCmd =
commands + sizeof(commands) / sizeof(Command);
list<const Command *> res;
// find matching commands
for (cmd = commands; cmd < endCmd; cmd++) {
if (abbrevMatch(cmdStr, cmd->name)) {
res.push_back(cmd);
}
}
return res;
} }
/****************************************************************************/ /****************************************************************************/
...@@ -279,28 +308,35 @@ void Command::displayHelp() const ...@@ -279,28 +308,35 @@ void Command::displayHelp() const
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int retval = 0; int retval = 0;
const CommandAlias *alias; list<const Command *> commands;
const CommandAlias *endAlias = list<const Command *>::const_iterator ci;
commandAliases + sizeof(commandAliases) / sizeof(CommandAlias); const Command *cmd;
binaryBaseName = basename(argv[0]); binaryBaseName = basename(argv[0]);
getOptions(argc, argv); getOptions(argc, argv);
// search command alias in alias map commands = getMatchingCommands(commandName);
for (alias = commandAliases; alias < endAlias; alias++) {
if (command == alias->name) if (commands.size()) {
break; if (commands.size() == 1) {
} cmd = commands.front();
if (!helpRequested) {
if (alias < endAlias) { // command alias found masterDev.setIndex(masterIndex);
if (!helpRequested) { retval = cmd->execute();
masterDev.setIndex(masterIndex); } else {
retval = alias->command->execute(); cmd->displayHelp();
}
} else { } else {
alias->command->displayHelp(); cerr << "Ambigous command abbreviation! Matching:" << endl;
for (ci = commands.begin(); ci != commands.end(); ci++) {
cerr << (*ci)->name << endl;
}
cerr << endl;
printUsage();
retval = 1;
} }
} else { // command not found } else {
cerr << "Unknown command " << command << "!" << endl << endl; cerr << "Unknown command " << commandName << "!" << endl << endl;
printUsage(); printUsage();
retval = 1; retval = 1;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment