From 4ef22255b0bde317889bfb4abf58f26d2b9c1304 Mon Sep 17 00:00:00 2001 From: Nicholas O'Connor Date: Wed, 22 Mar 2017 19:34:50 -0700 Subject: [PATCH] the humble beginnings of a command system --- src/Makefile.am | 2 +- src/cellar.cpp | 31 +++++++------------------------ src/commands.cpp | 19 +++++++++++++++++++ src/commands.hpp | 20 ++++++++++++++++++++ 4 files changed, 47 insertions(+), 25 deletions(-) create mode 100644 src/commands.cpp create mode 100644 src/commands.hpp diff --git a/src/Makefile.am b/src/Makefile.am index 860f0f0..569b6cc 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,4 +1,4 @@ bin_PROGRAMS = cellar cellar_CPPFLAGS = $(BOOST_CPPFLAGS) cellar_LDFLAGS = $(BOOST_LDFLAGS) $(BOOST_SYSTEM_LIB) $(BOOST_FILESYSTEM_LIB) -cellar_SOURCES = cellar.cpp fs.cpp bottles.cpp +cellar_SOURCES = cellar.cpp fs.cpp bottles.cpp commands.cpp diff --git a/src/cellar.cpp b/src/cellar.cpp index bd03324..2aa373a 100644 --- a/src/cellar.cpp +++ b/src/cellar.cpp @@ -6,34 +6,17 @@ #include "json.hpp" #include "bottles.hpp" +#include "commands.hpp" using namespace std; using namespace cellar; using json = nlohmann::json; int main(int argc, char* argv[]) { - map bottles = bottles::get_bottles(); - for (auto& item : bottles) { - cout << item.first << "- "; - bottles::Bottle current = item.second; - - switch (current.type) { - case bottles::bottle_anonymous: - cout << "anonymous wine bottle"; - break; - case bottles::bottle_labelled: - cout << current.config["name"]; - break; - case bottles::bottle_symlink: - cout << "symlink to "; - cout << current.canonical_path; - break; - default: - cout << "broken or unsupported wine bottle"; - } - - cout << endl; - } - - return 0; + vector commands = commands::list_commands(); + cout << "cellar - bottle management tool for WINE connoisseurs" << std::endl; + for (string item : commands) { + cout << item << " has loaded" << endl; + } + return 0; } diff --git a/src/commands.cpp b/src/commands.cpp new file mode 100644 index 0000000..dc04315 --- /dev/null +++ b/src/commands.cpp @@ -0,0 +1,19 @@ +#include "commands.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) { + result.push_back(item.first); + } + return result; +} diff --git a/src/commands.hpp b/src/commands.hpp new file mode 100644 index 0000000..29a7b55 --- /dev/null +++ b/src/commands.hpp @@ -0,0 +1,20 @@ +#ifndef __COMMANDS_HPP +#define __COMMANDS_HPP +#pragma once + +#include +#include +#include + +using namespace std; +namespace cellar { + namespace commands { + typedef void (*CommandFunction)(int, char*[]); + extern map command_map; + + bool add_command(string, CommandFunction); + vector list_commands(); + } +} + +#endif // __COMMANDS_HPP