description implementation! version already has one

This commit is contained in:
Nicholas O'Connor 2017-03-24 23:37:49 -07:00
parent a5a8fb284c
commit de77c55df7
4 changed files with 54 additions and 13 deletions

View File

@ -77,8 +77,18 @@ endfunction(cellar_library)
cellar_library(bottles) cellar_library(bottles)
cellar_library(launch) cellar_library(launch)
add_executable(cellar ${src}/cellar.cpp ${src}/commands.cpp ${src}/fs.cpp file(GLOB coresources RELATIVE "${CMAKE_SOURCE_DIR}"
${src}/version.cpp ${src}/output.cpp ${src}/help/help.cpp) "${CMAKE_SOURCE_DIR}/src/*.cpp")
file(GLOB corecoggedsources RELATIVE "${CMAKE_SOURCE_DIR}"
"${CMAKE_SOURCE_DIR}/src/*.cpp.cog")
foreach(corecog ${corecoggedsources})
string(REGEX REPLACE ".cog\$" "" this "${corecog}")
set(coresources ${coresources} ${this})
endforeach(corecog)
get_sources("src/help/*.cpp" helpsources)
add_executable(cellar ${coresources} ${helpsources})
target_link_libraries(cellar ${cellar_LIBRARIES} ${Boost_LIBRARIES}) target_link_libraries(cellar ${cellar_LIBRARIES} ${Boost_LIBRARIES})
add_dependencies(cellar cog) # effectively redundant but a couple of the bin's add_dependencies(cellar cog) # effectively redundant but a couple of the bin's
# files are cogged, so this is mostly for people # files are cogged, so this is mostly for people

View File

@ -9,6 +9,7 @@ using namespace std;
namespace cellar { namespace cellar {
namespace help { namespace help {
extern void set_description(string,string); extern void set_description(string,string);
extern string get_description(string);
} }
} }

26
src/help/descriptions.cpp Normal file
View File

@ -0,0 +1,26 @@
#include <map>
#include <string>
#include "help.hpp"
map<string,string> description_map;
void cellar::help::set_description(string command, string description) {
if (description.empty()) {
if (description_map.find(command) == description_map.end()) { // not found
return; // no description no cry
} else {
description_map.erase(command);
}
} else { // setting description
description_map[command] = description;
}
}
string cellar::help::get_description(string command) {
if (description_map.find(command) == description_map.end()) { // not found
return "";
} else {
return description_map.at(command);
}
}

View File

@ -1,28 +1,32 @@
#include <iostream>
#include <sstream>
#include <string> #include <string>
#include <vector> #include <vector>
#include "cellar.hpp" #include "cellar.hpp"
#include "internal/core.hpp" #include "internal/core.hpp"
#include "help.cpp" #include "help.hpp"
#include "output.hpp"
using namespace std; using namespace std;
using namespace cellar;
void cellar::commands::help_command(int argc, vector<string> argv) { void cellar::commands::help_command(int argc, vector<string> argv) {
vector<string> commands = list_commands(); vector<string> commands = list_commands();
cellar::print_header(); cellar::print_header();
cout << "You have these commands:\n" << endl; cout << endl; // TODO: cellar::output function for code clarity
int num_columns = 4; output::statement("You have these commands:");
int cur_column = 1; stringstream sstr;
for (string command : commands) { for (string command : commands) {
cout << "\t" << command; sstr << "\t" << command;
if (cur_column == num_columns) {
cout << endl; string desc = help::get_description(command);
cur_column = 1; if (!desc.empty()) {
} else { sstr << " - " << desc;
cur_column++;
} }
output::statement(sstr.str());
sstr.str("");
} }
cout << endl;
} }