2021-05-20 04:30:31 +02:00
|
|
|
/* This file is part of epubgrep.
|
|
|
|
* Copyright © 2021 tastytea <tastytea@tastytea.de>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "options.hpp"
|
|
|
|
|
2021-05-20 10:30:36 +02:00
|
|
|
#include "fs-compat.hpp"
|
2021-05-20 04:30:31 +02:00
|
|
|
#include "version.hpp"
|
|
|
|
|
2021-05-20 07:07:47 +02:00
|
|
|
#include <boost/locale/message.hpp>
|
2021-05-20 04:30:31 +02:00
|
|
|
#include <boost/program_options/options_description.hpp>
|
|
|
|
#include <boost/program_options/parsers.hpp>
|
2021-05-20 11:25:56 +02:00
|
|
|
#include <boost/program_options/positional_options.hpp>
|
|
|
|
#include <boost/program_options/value_semantic.hpp>
|
2021-05-20 04:30:31 +02:00
|
|
|
#include <boost/program_options/variables_map.hpp>
|
|
|
|
|
2021-05-24 07:50:50 +02:00
|
|
|
#include <cstdint>
|
2021-05-20 09:05:39 +02:00
|
|
|
#include <cstdlib>
|
2021-05-24 05:45:42 +02:00
|
|
|
#include <exception>
|
2021-05-20 09:05:39 +02:00
|
|
|
#include <fstream>
|
2021-05-20 04:30:31 +02:00
|
|
|
#include <iostream>
|
2021-05-20 09:05:39 +02:00
|
|
|
#include <string>
|
2021-05-20 04:30:31 +02:00
|
|
|
|
2021-05-21 01:50:13 +02:00
|
|
|
namespace epubgrep::options
|
2021-05-20 04:30:31 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
namespace po = boost::program_options;
|
2021-05-20 07:07:47 +02:00
|
|
|
|
|
|
|
using boost::locale::translate;
|
2021-05-20 04:30:31 +02:00
|
|
|
using std::cout;
|
|
|
|
|
|
|
|
po::variables_map parse_options(int argc, char *argv[])
|
|
|
|
{
|
2021-05-23 16:23:07 +02:00
|
|
|
po::options_description options_visible(translate("Available options"));
|
2021-05-20 04:30:31 +02:00
|
|
|
// clang-format off
|
2021-05-23 16:23:07 +02:00
|
|
|
options_visible.add_options()
|
2021-05-20 11:51:08 +02:00
|
|
|
("help,h",
|
2021-05-20 11:25:56 +02:00
|
|
|
translate("Display this help and exit.").str().data())
|
2021-05-20 11:51:08 +02:00
|
|
|
("version,V",
|
2021-05-20 11:25:56 +02:00
|
|
|
translate("Display version information and exit.").str().data())
|
2021-05-23 16:23:07 +02:00
|
|
|
("basic-regexp,G",
|
|
|
|
translate("PATTERN is a basic regular expression (default).")
|
|
|
|
.str().data())
|
2021-05-20 11:51:08 +02:00
|
|
|
("extended-regexp,E",
|
2021-05-23 16:23:07 +02:00
|
|
|
translate("PATTERN is an extended regular expression.").str().data())
|
|
|
|
("grep",
|
|
|
|
translate("Use grep-variation of regular expressions with -G and -E.")
|
|
|
|
.str().data())
|
2021-05-20 11:51:08 +02:00
|
|
|
("perl-regexp,P",
|
2021-05-23 16:23:07 +02:00
|
|
|
translate("PATTERN is a Perl regular expression.").str().data())
|
2021-05-20 11:51:08 +02:00
|
|
|
("ignore-case,i",
|
2021-05-20 11:25:56 +02:00
|
|
|
translate("Ignore case distinctions in pattern and data.")
|
2021-05-20 07:07:47 +02:00
|
|
|
.str().data())
|
2021-05-24 05:45:42 +02:00
|
|
|
("regexp,e", po::value<std::vector<std::string>>()
|
|
|
|
->value_name(translate("PATTERN"))->composing()->required(),
|
2021-05-23 16:23:07 +02:00
|
|
|
translate("Use additional PATTERN for matching.").str().data())
|
2021-05-24 07:50:50 +02:00
|
|
|
("raw,a",
|
2021-05-24 16:01:41 +02:00
|
|
|
translate("Do not clean up text before searching.").str().data())
|
2021-05-24 08:14:29 +02:00
|
|
|
("context,C", po::value<std::uint64_t>()
|
2021-05-24 08:23:21 +02:00
|
|
|
->value_name(translate("NUMBER"))->default_value(0),
|
2021-05-25 11:45:25 +02:00
|
|
|
translate("Print NUMBER words of context around matches.")
|
|
|
|
.str().data())
|
|
|
|
("nocolor", translate("Do not color matches.") .str().data())
|
2021-05-20 04:30:31 +02:00
|
|
|
;
|
2021-05-23 16:23:07 +02:00
|
|
|
|
|
|
|
po::options_description options_hidden("Hidden options");
|
|
|
|
options_hidden.add_options()
|
|
|
|
("input-file", po::value<std::vector<std::string>>()
|
|
|
|
->value_name("FILE"), "Input file to search.")
|
|
|
|
;
|
2021-05-20 04:30:31 +02:00
|
|
|
// clang-format on
|
2021-05-23 16:23:07 +02:00
|
|
|
po::options_description options_all("Allowed options");
|
|
|
|
options_all.add(options_visible).add(options_hidden);
|
2021-05-20 11:25:56 +02:00
|
|
|
|
|
|
|
po::positional_options_description positional_desc;
|
2021-05-20 11:43:35 +02:00
|
|
|
positional_desc.add("regexp", 1).add("input-file", -1);
|
2021-05-20 11:25:56 +02:00
|
|
|
|
2021-05-20 04:30:31 +02:00
|
|
|
po::variables_map vm;
|
2021-05-20 11:25:56 +02:00
|
|
|
po::store(po::command_line_parser(argc, argv)
|
2021-05-23 16:23:07 +02:00
|
|
|
.options(options_all)
|
2021-05-20 11:25:56 +02:00
|
|
|
.positional(positional_desc)
|
|
|
|
.run(),
|
|
|
|
vm);
|
2021-05-20 09:05:39 +02:00
|
|
|
|
|
|
|
std::ifstream configfile(get_config_path());
|
2021-05-23 16:23:07 +02:00
|
|
|
po::store(po::parse_config_file(configfile, options_visible, true), vm);
|
2021-05-20 09:05:39 +02:00
|
|
|
configfile.close();
|
|
|
|
|
2021-05-24 05:45:42 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
po::notify(vm);
|
|
|
|
}
|
|
|
|
catch (const boost::program_options::required_option &e)
|
|
|
|
{
|
2021-05-25 07:12:13 +02:00
|
|
|
if (vm.count("help") + vm.count("version") == 0)
|
|
|
|
{
|
|
|
|
cout << options_visible;
|
|
|
|
std::rethrow_exception(std::current_exception());
|
|
|
|
}
|
2021-05-24 05:45:42 +02:00
|
|
|
}
|
2021-05-20 04:30:31 +02:00
|
|
|
|
|
|
|
if (vm.count("help") != 0)
|
|
|
|
{
|
2021-05-20 11:25:56 +02:00
|
|
|
cout << translate("Usage: epubgrep [OPTION]… PATTERN [FILE]…\n");
|
2021-05-23 16:23:07 +02:00
|
|
|
cout << options_visible;
|
2021-05-24 06:03:32 +02:00
|
|
|
cout << translate("\nYou can access the full manual "
|
|
|
|
"with `man epubgrep`.\n");
|
2021-05-20 04:30:31 +02:00
|
|
|
}
|
|
|
|
else if (vm.count("version") != 0)
|
|
|
|
{
|
|
|
|
cout << "epubgrep " << epubgrep::version << '\n';
|
2021-05-20 07:07:47 +02:00
|
|
|
cout << translate(
|
|
|
|
"Copyright © 2021 tastytea <tastytea@tastytea.de>\n"
|
|
|
|
"License AGPL-3.0-only <https://gnu.org/licenses/agpl.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");
|
2021-05-20 04:30:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return vm;
|
|
|
|
}
|
|
|
|
|
2021-05-20 10:20:19 +02:00
|
|
|
fs::path get_config_path()
|
2021-05-20 09:05:39 +02:00
|
|
|
{
|
2021-05-21 07:10:46 +02:00
|
|
|
const auto get_env{[](const std::string &name)
|
|
|
|
{
|
|
|
|
const char *env = std::getenv(name.c_str());
|
|
|
|
if (env != nullptr)
|
|
|
|
{
|
|
|
|
return env;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}};
|
2021-05-20 09:05:39 +02:00
|
|
|
|
2021-05-20 10:20:19 +02:00
|
|
|
fs::path path{get_env("XDG_CONFIG_HOME")};
|
2021-05-20 09:05:39 +02:00
|
|
|
if (path.empty())
|
|
|
|
{
|
|
|
|
path = get_env("HOME");
|
|
|
|
if (!path.empty())
|
|
|
|
{
|
2021-05-20 10:20:19 +02:00
|
|
|
path /= ".config";
|
2021-05-20 09:05:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!path.empty())
|
|
|
|
{
|
2021-05-20 10:20:19 +02:00
|
|
|
return path /= "epubgrep.conf";
|
2021-05-20 09:05:39 +02:00
|
|
|
}
|
|
|
|
|
2021-05-20 10:20:19 +02:00
|
|
|
return "epubgrep.conf";
|
2021-05-20 09:05:39 +02:00
|
|
|
}
|
|
|
|
|
2021-05-21 01:50:13 +02:00
|
|
|
} // namespace epubgrep::options
|