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"
|
|
|
|
|
|
|
|
#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>
|
|
|
|
#include <boost/program_options/variables_map.hpp>
|
|
|
|
|
2021-05-20 09:05:39 +02:00
|
|
|
#include <cstdlib>
|
|
|
|
#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
|
|
|
|
|
|
|
namespace epubgrep
|
|
|
|
{
|
|
|
|
|
|
|
|
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-20 07:07:47 +02:00
|
|
|
po::options_description desc(translate("Available options"));
|
2021-05-20 04:30:31 +02:00
|
|
|
// clang-format off
|
|
|
|
desc.add_options()
|
2021-05-20 07:07:47 +02:00
|
|
|
("help",
|
|
|
|
translate("Display this help and exit").str().data())
|
|
|
|
("version",
|
|
|
|
translate("Display version information and exit").str().data())
|
|
|
|
("extended-regexp",
|
|
|
|
translate("PATTERNS are extended regular expressions").str().data())
|
|
|
|
("perl-regexp",
|
|
|
|
translate("PATTERNS are Perl regular expressions").str().data())
|
|
|
|
("ignore-case",
|
|
|
|
translate("Ignore case distinctions in patterns and data")
|
|
|
|
.str().data())
|
2021-05-20 04:30:31 +02:00
|
|
|
;
|
|
|
|
// clang-format on
|
|
|
|
po::variables_map vm;
|
|
|
|
po::store(po::parse_command_line(argc, argv, desc), vm);
|
2021-05-20 09:05:39 +02:00
|
|
|
|
|
|
|
std::ifstream configfile(get_config_path());
|
|
|
|
po::store(po::parse_config_file(configfile, desc, true), vm);
|
|
|
|
configfile.close();
|
|
|
|
|
2021-05-20 04:30:31 +02:00
|
|
|
po::notify(vm);
|
|
|
|
|
|
|
|
if (vm.count("help") != 0)
|
|
|
|
{
|
2021-05-20 07:07:47 +02:00
|
|
|
cout << translate("Usage: epubgrep [OPTION]… PATTERNS [FILE]…\n");
|
|
|
|
cout << desc;
|
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 09:05:39 +02:00
|
|
|
std::string get_config_path()
|
|
|
|
{
|
|
|
|
auto get_env = [](std::string const &name)
|
|
|
|
{
|
|
|
|
const char *env = std::getenv(name.c_str());
|
|
|
|
if (env != nullptr)
|
|
|
|
{
|
|
|
|
return env;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string path{get_env("XDG_CONFIG_HOME")};
|
|
|
|
if (path.empty())
|
|
|
|
{
|
|
|
|
path = get_env("HOME");
|
|
|
|
if (!path.empty())
|
|
|
|
{
|
|
|
|
path += "/.config";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!path.empty())
|
|
|
|
{
|
|
|
|
path += "/";
|
|
|
|
}
|
|
|
|
|
|
|
|
return path += "epubgrep.conf";
|
|
|
|
}
|
|
|
|
|
2021-05-20 04:30:31 +02:00
|
|
|
} // namespace epubgrep
|