2019-05-12 00:02:31 +02:00
|
|
|
/* This file is part of remwharead.
|
|
|
|
* Copyright © 2019 tastytea <tastytea@tastytea.de>
|
|
|
|
*
|
|
|
|
* This program 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, version 3.
|
|
|
|
*
|
|
|
|
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <exception>
|
|
|
|
#include <popl.hpp>
|
|
|
|
#include "version.hpp"
|
|
|
|
#include "parse_options.hpp"
|
|
|
|
|
|
|
|
using std::cout;
|
|
|
|
using std::cerr;
|
|
|
|
using std::endl;
|
2019-05-12 20:19:00 +02:00
|
|
|
|
|
|
|
options::options()
|
|
|
|
{}
|
|
|
|
|
|
|
|
options::options(const uint8_t &status)
|
|
|
|
: status_code(status)
|
|
|
|
{}
|
2019-05-12 00:02:31 +02:00
|
|
|
|
2019-05-12 20:19:00 +02:00
|
|
|
const options parse_options(const int argc, const char *argv[])
|
2019-05-12 00:02:31 +02:00
|
|
|
{
|
|
|
|
string tags;
|
|
|
|
string format;
|
|
|
|
string span;
|
|
|
|
options opts;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
popl::OptionParser op("Available options");
|
|
|
|
op.add<popl::Value<string>>
|
2019-05-16 08:36:35 +02:00
|
|
|
("t", "tags", "Add tags to URI, delimited by commas.", "", &tags);
|
2019-07-21 17:15:55 +02:00
|
|
|
auto option_export = op.add<popl::Value<string>>
|
2019-07-21 16:37:36 +02:00
|
|
|
("e", "export", "Export to format.", "simple", &format);
|
2019-05-12 00:02:31 +02:00
|
|
|
op.add<popl::Value<string>>
|
2019-05-22 09:43:58 +02:00
|
|
|
("f", "file", "Save output to file.", "", &opts.file);
|
2019-05-12 00:02:31 +02:00
|
|
|
op.add<popl::Value<string>>
|
2019-05-22 09:51:58 +02:00
|
|
|
("T", "time-span",
|
|
|
|
"Only export entries between YYYY-MM-DD,YYYY-MM-DD.", "", &span);
|
2019-05-19 09:42:52 +02:00
|
|
|
op.add<popl::Value<string>>
|
|
|
|
("s", "search-tags",
|
2019-05-19 11:50:41 +02:00
|
|
|
"Search in tags. Format: tag1 AND tag2 OR tag3.",
|
2019-05-22 09:43:58 +02:00
|
|
|
"", &opts.search_tags);
|
2019-05-19 10:55:45 +02:00
|
|
|
op.add<popl::Value<string>>
|
2019-05-22 09:51:58 +02:00
|
|
|
("S", "search-all",
|
2019-05-19 11:50:41 +02:00
|
|
|
"Search in tags, title, description and full text.",
|
2019-05-22 09:43:58 +02:00
|
|
|
"", &opts.search_all);
|
|
|
|
auto option_noarchive = op.add<popl::Switch>
|
|
|
|
("N", "no-archive", "Do not archive URI.");
|
2019-05-12 00:02:31 +02:00
|
|
|
auto option_help = op.add<popl::Switch>
|
|
|
|
("h", "help", "Show this help message.");
|
|
|
|
auto option_version = op.add<popl::Switch>
|
|
|
|
("V", "version", "Print version, copyright and license.");
|
|
|
|
op.parse(argc, argv);
|
|
|
|
|
|
|
|
if (option_help->is_set())
|
|
|
|
{
|
2019-05-22 09:51:58 +02:00
|
|
|
cout << "Usage: " << argv[0] << " [-t tags] [-N] URI\n"
|
2019-05-13 02:32:27 +02:00
|
|
|
<< " " << argv[0]
|
2019-07-21 17:15:55 +02:00
|
|
|
<< " -e format [-f file] [-T start,end] "
|
2019-05-22 09:51:58 +02:00
|
|
|
<< "[[-s|-S] expression]\n";
|
2019-05-12 00:02:31 +02:00
|
|
|
cout << op;
|
2019-05-12 20:19:00 +02:00
|
|
|
return options(0);
|
2019-05-12 00:02:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (option_version->is_set())
|
|
|
|
{
|
|
|
|
cout << "remwharead " << global::version << endl <<
|
|
|
|
"Copyright (C) 2019 tastytea <tastytea@tastytea.de>\n"
|
|
|
|
"License GPLv3: GNU GPL version 3 "
|
|
|
|
"<https://www.gnu.org/licenses/gpl-3.0.html>.\n"
|
|
|
|
"This program comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
|
|
|
|
"and you are welcome to redistribute it under certain conditions.\n";
|
2019-05-12 20:19:00 +02:00
|
|
|
return options(0);
|
2019-05-12 00:02:31 +02:00
|
|
|
}
|
|
|
|
|
2019-05-22 09:43:58 +02:00
|
|
|
if (option_noarchive->is_set())
|
|
|
|
{
|
|
|
|
opts.archive = false;
|
|
|
|
}
|
|
|
|
|
2019-05-12 00:02:31 +02:00
|
|
|
if (!tags.empty())
|
|
|
|
{
|
|
|
|
size_t pos_end = 0;
|
|
|
|
size_t pos_start = 0;
|
|
|
|
while (pos_end != std::string::npos)
|
|
|
|
{
|
|
|
|
pos_end = tags.find(',', pos_start);
|
2019-05-22 15:13:10 +02:00
|
|
|
string buffer = tags.substr(pos_start, pos_end - pos_start);
|
|
|
|
while (*buffer.begin() == ' ') // Remove leading spaces.
|
|
|
|
{
|
|
|
|
buffer.erase(buffer.begin());
|
|
|
|
}
|
|
|
|
while (*buffer.rbegin() == ' ') // Remove trailing spaces.
|
|
|
|
{
|
|
|
|
buffer.erase(buffer.end() - 1);
|
|
|
|
}
|
|
|
|
opts.tags.push_back(buffer);
|
2019-05-12 00:02:31 +02:00
|
|
|
pos_start = pos_end + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-21 16:37:36 +02:00
|
|
|
if (option_export->is_set())
|
2019-05-12 00:02:31 +02:00
|
|
|
{
|
|
|
|
if (format == "csv")
|
|
|
|
{
|
|
|
|
opts.format = export_format::csv;
|
|
|
|
}
|
2019-05-16 08:37:50 +02:00
|
|
|
else if (format == "asciidoc" || format == "adoc")
|
2019-05-12 00:02:31 +02:00
|
|
|
{
|
|
|
|
opts.format = export_format::asciidoc;
|
|
|
|
}
|
2019-05-26 15:47:06 +02:00
|
|
|
else if (format == "bookmarks")
|
|
|
|
{
|
|
|
|
opts.format = export_format::bookmarks;
|
|
|
|
}
|
2019-07-21 16:37:36 +02:00
|
|
|
else if (format == "simple")
|
|
|
|
{
|
|
|
|
opts.format = export_format::simple;
|
|
|
|
}
|
2019-05-12 00:02:31 +02:00
|
|
|
else
|
|
|
|
{
|
2019-05-13 02:33:43 +02:00
|
|
|
opts.format = export_format::undefined;
|
2019-05-26 15:47:06 +02:00
|
|
|
cerr << "Error: Export format must be "
|
2019-07-21 16:37:36 +02:00
|
|
|
<< "csv, asciidoc, bookmarks or simple.\n";
|
2019-05-12 20:19:00 +02:00
|
|
|
return options(1);
|
2019-05-12 00:02:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!span.empty())
|
|
|
|
{
|
|
|
|
size_t pos = span.find(',');
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
|
|
|
opts.span =
|
|
|
|
{
|
|
|
|
string_to_timepoint(span.substr(0, pos)),
|
|
|
|
string_to_timepoint(span.substr(pos + 1))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cerr << "Error: Time span must be in format: "
|
2019-05-13 02:32:27 +02:00
|
|
|
"YYYY-MM-DD,YYYY-MM-DD.\n";
|
2019-05-12 20:19:00 +02:00
|
|
|
return options(1);
|
2019-05-12 00:02:31 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-13 02:33:43 +02:00
|
|
|
|
|
|
|
if (op.non_option_args().size() > 0)
|
|
|
|
{
|
2019-05-16 08:36:35 +02:00
|
|
|
opts.uri = op.non_option_args().front();
|
2019-05-13 02:33:43 +02:00
|
|
|
}
|
|
|
|
|
2019-05-16 08:36:35 +02:00
|
|
|
if (opts.uri == "" && opts.format == export_format::undefined)
|
2019-05-13 02:33:43 +02:00
|
|
|
{
|
2019-05-16 08:36:35 +02:00
|
|
|
cerr << "Error: You have to specify either URI or --export.\n";
|
2019-05-13 02:33:43 +02:00
|
|
|
return options(1);
|
|
|
|
}
|
2019-05-12 00:02:31 +02:00
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
2019-05-16 00:04:49 +02:00
|
|
|
cerr << "Error in " << __func__ << ": " << e.what() << endl;
|
2019-05-12 20:19:00 +02:00
|
|
|
return options(1);
|
2019-05-12 00:02:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return opts;
|
|
|
|
}
|