From 6afc16fe5bda18daccccfd687b291eb67fefd934 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 5 May 2016 19:10:46 +0200 Subject: [PATCH] Accept a comma-separated list of tags as the first parameter in tag --- src/tag/CMakeLists.txt | 1 + src/tag/commandline.cpp | 6 +++-- src/tag/main.cpp | 12 +++++++++- src/tag/split_tags.cpp | 50 +++++++++++++++++++++++++++++++++++++++++ src/tag/split_tags.hpp | 30 +++++++++++++++++++++++++ 5 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 src/tag/split_tags.cpp create mode 100644 src/tag/split_tags.hpp diff --git a/src/tag/CMakeLists.txt b/src/tag/CMakeLists.txt index 9107d4e..7f088ea 100644 --- a/src/tag/CMakeLists.txt +++ b/src/tag/CMakeLists.txt @@ -3,6 +3,7 @@ project(${bare_name}-tag CXX) add_executable(${PROJECT_NAME} main.cpp commandline.cpp + split_tags.cpp ) target_include_directories(${PROJECT_NAME} diff --git a/src/tag/commandline.cpp b/src/tag/commandline.cpp index 84431b8..9ed6634 100644 --- a/src/tag/commandline.cpp +++ b/src/tag/commandline.cpp @@ -34,6 +34,7 @@ namespace din { po::options_description positional_options("Positional options"); positional_options.add_options() + ("tags", po::value(), "comma-separated tag list") ("ids", po::value>(), "pos_option description") ; @@ -41,7 +42,7 @@ namespace din { po::options_description all("Available options"); po::positional_options_description pd; all.add(desc).add(positional_options).add(set_options); - pd.add("ids", -1);//.add("pos_option2", 1); + pd.add("tags", 1).add("ids", -1); try { po::store(po::command_line_parser(parArgc, parArgv).options(all).positional(pd).run(), parVarMap); } @@ -51,7 +52,8 @@ namespace din { po::notify(parVarMap); - if (dinlib::manage_common_commandline(std::cout, ACTION_NAME, "[options...] ids...", parVarMap, {std::cref(desc), std::cref(set_options)})) { + const char* const help_text = "[options...] tag[,tag2...] ids..."; + if (dinlib::manage_common_commandline(std::cout, ACTION_NAME, help_text, parVarMap, {std::cref(desc), std::cref(set_options)})) { return true; } diff --git a/src/tag/main.cpp b/src/tag/main.cpp index aafc48d..9b446e2 100644 --- a/src/tag/main.cpp +++ b/src/tag/main.cpp @@ -16,10 +16,13 @@ */ #include "commandline.hpp" -#include "tag_postgres.hpp" +#include "split_tags.hpp" #include #include +namespace { +} //unnamed namespace + int main (int parArgc, char* parArgv[]) { using boost::program_options::variables_map; @@ -43,5 +46,12 @@ int main (int parArgc, char* parArgv[]) { for (auto id : ids) { std::cout << id << '\n'; } + + const auto master_tags_string = vm["tags"].as(); + std::vector tags = din::split_tags(master_tags_string); + + for (auto tag : tags) { + std::cout << '"' << tag << "\"\n"; + } return 0; } diff --git a/src/tag/split_tags.cpp b/src/tag/split_tags.cpp new file mode 100644 index 0000000..2a6ea19 --- /dev/null +++ b/src/tag/split_tags.cpp @@ -0,0 +1,50 @@ +/* Copyright 2015, 2016, Michele Santullo + * This file is part of "dindexer". + * + * "dindexer" is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * "dindexer" is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with "dindexer". If not, see . + */ + +#include "split_tags.hpp" +#include +#include +#include +#include +#include +#include + +namespace din { + std::vector split_tags (const std::string& parCommaSeparatedList) { + using OutRange = boost::iterator_range; + using boost::token_finder; + using boost::adaptors::transformed; + using boost::adaptors::filtered; + using boost::string_ref; + using boost::iter_split; + using boost::trim_copy; + + std::vector out_range; + + //See: + //https://stackoverflow.com/questions/27999941/how-to-use-boostsplit-with-booststring-ref-in-boost-1-55 + //http://www.boost.org/doc/libs/1_60_0/doc/html/boost/algorithm/iter_split.html + //http://www.boost.org/doc/libs/1_60_0/doc/html/boost/algorithm/token_finder.html + //https://stackoverflow.com/questions/20781090/difference-between-boostsplit-vs-boostiter-split + return boost::copy_range>( + iter_split(out_range, parCommaSeparatedList, token_finder([](char c){return ','==c;})) | + transformed([](const OutRange& r){return trim_copy(r);}) | + transformed([](const OutRange& r){return string_ref(&*r.begin(), r.size());}) | + filtered([](const string_ref& r){return not r.empty();}) + ); + } +} //namespace din diff --git a/src/tag/split_tags.hpp b/src/tag/split_tags.hpp new file mode 100644 index 0000000..15a2695 --- /dev/null +++ b/src/tag/split_tags.hpp @@ -0,0 +1,30 @@ +/* Copyright 2015, 2016, Michele Santullo + * This file is part of "dindexer". + * + * "dindexer" is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * "dindexer" is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with "dindexer". If not, see . + */ + +#ifndef id913CE9D2F60745349F39F2C82455973E +#define id913CE9D2F60745349F39F2C82455973E + +#include +#include +#include +#include "helpers/compatibility.h" + +namespace din { + std::vector split_tags ( const std::string& parCommaSeparatedList ) a_pure; +} //namespace din + +#endif