help and version commands, tclap dependency introduced here

This commit is contained in:
Nicholas O'Connor 2017-03-22 23:23:15 -07:00
parent 207599eab1
commit 7f5e6088cf
6 changed files with 73 additions and 14 deletions

View File

@ -16,6 +16,9 @@ if test "$COG" == "not found" ; then
AC_SUBST(COG)
fi
AX_CXX_HAVE_SSTREAM
PKG_CHECK_MODULES([TCLAP], [tclap >= 1.2.1])
AX_BOOST_BASE([1.63], , [AC_MSG_ERROR([boost 1.63 required])])
AX_BOOST_SYSTEM
AX_BOOST_FILESYSTEM

View File

@ -1,11 +1,15 @@
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include "config.h"
#include "tclap/CmdLine.h"
#include "json.hpp"
#include "bottles.hpp"
#include "cellar.hpp"
#include "commands.hpp"
#include "version.hpp"
@ -13,12 +17,36 @@ using namespace std;
using namespace cellar;
using json = nlohmann::json;
int main(int argc, char* argv[]) {
vector<string> commands = commands::list_commands();
void cellar::print_header() {
cout << "cellar - bottle management tool for WINE connoisseurs" << std::endl;
cout << version::short_version() << std::endl;
for (string item : commands) {
cout << item << " has loaded" << endl;
}
int main(int argc, char* argv[]) {
if (argc == 1) {
print_header();
cout << "\n(try \"cellar help\" if you're confused)" << endl;
return 0;
}
try {
const string desc = "bottle management tool for WINE connoisseurs";
const string versionstr = version::short_version();
TCLAP::CmdLine cmdparse(desc, ' ', versionstr, false);
TCLAP::UnlabeledValueArg<string> command("command", "Specific command to run.", true, "help", "command");
cmdparse.add(command);
cmdparse.parse(argc, argv);
string usercmd = command.getValue();
if (commands::command_map.count(usercmd) > 0) {
commands::command_map[usercmd](argc, argv);
} else {
cerr << "invalid command: " << usercmd << endl;
return 1;
}
return 0;
} catch (TCLAP::ArgException &exc) {
cerr << "Invalid argument. (" << exc.argId() << ": " << exc.error() << ")" << endl;
return 1;
}
}

9
src/cellar.hpp Normal file
View File

@ -0,0 +1,9 @@
#ifndef __CELLAR_HPP
#define __CELLAR_HPP
#pragma once
namespace cellar {
void print_header();
}
#endif // __CELLAR_HPP

View File

@ -1,15 +1,13 @@
#include <iostream>
#include "commands.hpp"
#include "cellar.hpp"
using namespace std;
using namespace cellar::commands;
map<string, CommandFunction> cellar::commands::command_map;
bool cellar::commands::add_command(string name, CommandFunction func) {
command_map[name] = func;
return true;
}
vector<string> cellar::commands::list_commands() {
vector<string> result;
for (auto& item : command_map) {
@ -17,3 +15,24 @@ vector<string> cellar::commands::list_commands() {
}
return result;
}
void help_command(int argc, char** argv) {
vector<string> commands = list_commands();
cellar::print_header();
cout << "You have these commands:\n" << endl;
int num_columns = 4;
int cur_column = 1;
for (string command : commands) {
cout << "\t" << command;
if (cur_column == num_columns) {
cout << endl;
cur_column = 1;
} else {
cur_column++;
}
}
cout << endl;
}
CommandFunction helpcmd = command_map["help"] = &help_command;

View File

@ -12,7 +12,7 @@ namespace cellar {
typedef void (*CommandFunction)(int, char*[]);
extern map<string, CommandFunction> command_map;
bool add_command(string, CommandFunction);
void add_command(string, CommandFunction);
vector<string> list_commands();
}
}

View File

@ -33,7 +33,7 @@ string cellar::version::short_version() {
outstring = "exception raised when trying to read git data at precompile time"
raise
cog.outl("return \"{0}\";".format(outstring))
cog.outl("return string(\"{0}\");".format(outstring))
]]]*/
//[[[end]]]
}
@ -41,4 +41,4 @@ string cellar::version::short_version() {
void print_version(int argc, char** argv) {
cout << short_version() << endl;
}
bool _ = cellar::commands::add_command("version", &print_version);
cellar::commands::CommandFunction versioncmd = cellar::commands::command_map["version"] = &print_version;