2017-03-23 19:09:36 -07:00
|
|
|
// vim: filetype=cpp
|
2017-03-12 17:13:07 -07:00
|
|
|
#include <iostream>
|
2017-03-22 19:02:14 -07:00
|
|
|
#include <map>
|
2017-03-22 23:23:15 -07:00
|
|
|
#include <sstream>
|
2017-03-12 17:13:07 -07:00
|
|
|
#include <string>
|
2017-03-12 22:33:42 -07:00
|
|
|
#include <vector>
|
2017-03-12 17:13:07 -07:00
|
|
|
|
2017-03-22 23:23:15 -07:00
|
|
|
#include "config.h"
|
|
|
|
#include "tclap/CmdLine.h"
|
2017-03-12 23:10:04 -07:00
|
|
|
#include "json.hpp"
|
2017-03-12 17:13:07 -07:00
|
|
|
|
2017-03-13 19:03:07 -07:00
|
|
|
#include "bottles.hpp"
|
2017-03-22 23:23:15 -07:00
|
|
|
#include "cellar.hpp"
|
2017-03-22 19:34:50 -07:00
|
|
|
#include "commands.hpp"
|
2017-03-23 18:51:11 -07:00
|
|
|
#include "output.hpp"
|
2017-03-22 20:42:08 -07:00
|
|
|
#include "version.hpp"
|
2017-03-12 17:13:07 -07:00
|
|
|
|
2017-03-23 21:20:26 -07:00
|
|
|
/*[[[cog
|
|
|
|
import cog
|
|
|
|
|
|
|
|
with open("src/modules.txt") as modules:
|
|
|
|
for module in modules:
|
|
|
|
cog.outl("#include \"internal/{0}.hpp\"".format(module.strip()))
|
|
|
|
]]]*/
|
|
|
|
//[[[end]]]
|
|
|
|
|
2017-03-12 22:33:42 -07:00
|
|
|
using namespace std;
|
2017-03-22 19:02:14 -07:00
|
|
|
using namespace cellar;
|
2017-03-12 23:10:04 -07:00
|
|
|
using json = nlohmann::json;
|
2017-03-12 17:13:07 -07:00
|
|
|
|
2017-03-24 20:01:25 -07:00
|
|
|
bool cellar::verbose;
|
|
|
|
|
2017-03-22 23:23:15 -07:00
|
|
|
void cellar::print_header() {
|
2017-03-23 18:51:11 -07:00
|
|
|
output::statement("cellar - bottle management tool for WINE connoisseurs");
|
|
|
|
output::statement(version::short_version());
|
2017-03-22 23:23:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
2017-03-24 19:30:58 -07:00
|
|
|
output::detect_colors();
|
2017-03-22 23:23:15 -07:00
|
|
|
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);
|
2017-03-24 20:01:25 -07:00
|
|
|
|
|
|
|
TCLAP::SwitchArg verbosearg("v", "verbose", "Enables extra output");
|
|
|
|
cmdparse.add(verbosearg);
|
2017-03-23 15:25:56 -07:00
|
|
|
|
2017-03-22 23:23:15 -07:00
|
|
|
TCLAP::UnlabeledValueArg<string> command("command", "Specific command to run.", true, "help", "command");
|
|
|
|
cmdparse.add(command);
|
|
|
|
|
2017-03-23 15:25:56 -07:00
|
|
|
TCLAP::UnlabeledMultiArg<string> subargs("subargs", "Sub-arguments", false, "", "subarg");
|
|
|
|
cmdparse.add(subargs);
|
|
|
|
|
2017-03-22 23:23:15 -07:00
|
|
|
cmdparse.parse(argc, argv);
|
|
|
|
|
2017-03-24 20:01:25 -07:00
|
|
|
verbose = verbosearg.getValue();
|
|
|
|
|
2017-03-24 17:30:49 -07:00
|
|
|
// hardcoded because it's special
|
2017-03-24 20:01:25 -07:00
|
|
|
vector<string> core_cmdnames; // mostly for verbose output
|
2017-03-24 17:30:49 -07:00
|
|
|
for (auto item : commands::core_commands()) {
|
|
|
|
commands::command_map[item.first] = item.second;
|
2017-03-24 20:01:25 -07:00
|
|
|
core_cmdnames.push_back(item.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (verbose) { // handling it here for efficiency
|
|
|
|
stringstream commandstring;
|
|
|
|
commandstring << "loading from core: ";
|
|
|
|
for (string item : core_cmdnames) {
|
|
|
|
commandstring << item << " ";
|
|
|
|
}
|
|
|
|
output::statement(commandstring.str(), true);
|
2017-03-24 17:30:49 -07:00
|
|
|
}
|
2017-03-24 20:01:25 -07:00
|
|
|
// as above, but cogged from src/modules.txt
|
|
|
|
// BULLSHIT: trying to use str.format on this string causes bizarre compiler errors
|
2017-03-23 19:09:36 -07:00
|
|
|
/*[[[cog
|
|
|
|
import cog
|
|
|
|
|
|
|
|
with open("src/modules.txt") as modules:
|
|
|
|
for module in modules:
|
|
|
|
cog.out("""
|
2017-03-24 20:01:25 -07:00
|
|
|
vector<string> """ + module.strip() + """_cmdnames;
|
2017-03-23 19:09:36 -07:00
|
|
|
for (auto item : commands::""" + module.strip() + """_commands()) {
|
|
|
|
commands::command_map[item.first] = item.second;
|
2017-03-24 20:01:25 -07:00
|
|
|
""" + module.strip() + """_cmdnames.push_back(item.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (verbose) {
|
|
|
|
stringstream commandstring;
|
|
|
|
commandstring << "loading from """ + module.strip() + """: ";
|
|
|
|
for (string item : """ + module.strip() + """_cmdnames) {
|
|
|
|
commandstring << item << " ";
|
|
|
|
}
|
|
|
|
output::statement(commandstring.str(), true);
|
2017-03-23 19:09:36 -07:00
|
|
|
}
|
|
|
|
""", dedent=True, trimblanklines=True)
|
|
|
|
]]]*/
|
|
|
|
//[[[end]]]
|
2017-03-23 12:06:29 -07:00
|
|
|
|
2017-03-22 23:23:15 -07:00
|
|
|
string usercmd = command.getValue();
|
|
|
|
if (commands::command_map.count(usercmd) > 0) {
|
2017-03-23 17:23:59 -07:00
|
|
|
vector<string> subargv;
|
|
|
|
stringstream sstr;
|
|
|
|
sstr << "cellar ";
|
|
|
|
sstr << usercmd;
|
|
|
|
subargv.push_back(sstr.str());
|
|
|
|
for (auto item : subargs.getValue()) {
|
|
|
|
subargv.push_back(item);
|
|
|
|
}
|
2017-03-23 15:25:56 -07:00
|
|
|
commands::command_map[usercmd](subargv.size(), subargv);
|
2017-03-22 23:23:15 -07:00
|
|
|
} else {
|
2017-03-23 18:51:11 -07:00
|
|
|
stringstream errstr;
|
|
|
|
|
|
|
|
errstr << "invalid command: " << usercmd;
|
|
|
|
output::error(errstr.str());
|
2017-03-22 23:23:15 -07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
} catch (TCLAP::ArgException &exc) {
|
|
|
|
cerr << "Invalid argument. (" << exc.argId() << ": " << exc.error() << ")" << endl;
|
|
|
|
return 1;
|
2017-03-22 19:34:50 -07:00
|
|
|
}
|
2017-03-12 15:43:11 -07:00
|
|
|
}
|