diff --git a/configure.ac b/configure.ac index ee8ffda..6c8a7d6 100644 --- a/configure.ac +++ b/configure.ac @@ -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 diff --git a/src/cellar.cpp b/src/cellar.cpp index 0a50c2f..464a333 100644 --- a/src/cellar.cpp +++ b/src/cellar.cpp @@ -1,11 +1,15 @@ #include #include +#include #include #include +#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 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; - } - return 0; +} + +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 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; + } } diff --git a/src/cellar.hpp b/src/cellar.hpp new file mode 100644 index 0000000..1b7774f --- /dev/null +++ b/src/cellar.hpp @@ -0,0 +1,9 @@ +#ifndef __CELLAR_HPP +#define __CELLAR_HPP +#pragma once + +namespace cellar { + void print_header(); +} + +#endif // __CELLAR_HPP diff --git a/src/commands.cpp b/src/commands.cpp index dc04315..89da87d 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -1,15 +1,13 @@ +#include + #include "commands.hpp" +#include "cellar.hpp" using namespace std; using namespace cellar::commands; map cellar::commands::command_map; -bool cellar::commands::add_command(string name, CommandFunction func) { - command_map[name] = func; - return true; -} - vector cellar::commands::list_commands() { vector result; for (auto& item : command_map) { @@ -17,3 +15,24 @@ vector cellar::commands::list_commands() { } return result; } + +void help_command(int argc, char** argv) { + vector 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; diff --git a/src/commands.hpp b/src/commands.hpp index 29a7b55..2bc7e3c 100644 --- a/src/commands.hpp +++ b/src/commands.hpp @@ -12,7 +12,7 @@ namespace cellar { typedef void (*CommandFunction)(int, char*[]); extern map command_map; - bool add_command(string, CommandFunction); + void add_command(string, CommandFunction); vector list_commands(); } } diff --git a/src/version.cpp.cog b/src/version.cpp.cog index 34cc24d..d4bc012 100644 --- a/src/version.cpp.cog +++ b/src/version.cpp.cog @@ -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;