133 lines
3.9 KiB
C++
133 lines
3.9 KiB
C++
/* 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 "fs-compat.hpp"
|
|
#include "version.hpp"
|
|
|
|
#include <boost/locale/message.hpp>
|
|
#include <boost/program_options/options_description.hpp>
|
|
#include <boost/program_options/parsers.hpp>
|
|
#include <boost/program_options/positional_options.hpp>
|
|
#include <boost/program_options/value_semantic.hpp>
|
|
#include <boost/program_options/variables_map.hpp>
|
|
|
|
#include <cstdlib>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <string>
|
|
|
|
namespace epubgrep::options
|
|
{
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
using boost::locale::translate;
|
|
using std::cout;
|
|
|
|
po::variables_map parse_options(int argc, char *argv[])
|
|
{
|
|
po::options_description cmdline_desc(translate("Available options"));
|
|
// clang-format off
|
|
cmdline_desc.add_options()
|
|
("help,h",
|
|
translate("Display this help and exit.").str().data())
|
|
("version,V",
|
|
translate("Display version information and exit.").str().data())
|
|
("extended-regexp,E",
|
|
translate("PATTERN is extended regular expression.").str().data())
|
|
("perl-regexp,P",
|
|
translate("PATTERN is Perl regular expression.").str().data())
|
|
("ignore-case,i",
|
|
translate("Ignore case distinctions in pattern and data.")
|
|
.str().data())
|
|
("regexp,e", po::value<std::string>()->value_name(translate("PATTERN")),
|
|
translate("Use PATTERN for matching.").str().data())
|
|
("input-file,I", po::value<std::vector<std::string>>()
|
|
->value_name(translate("FILE")),
|
|
translate("Input file. Can be used more than once.").str().data())
|
|
;
|
|
// clang-format on
|
|
|
|
po::positional_options_description positional_desc;
|
|
positional_desc.add("regexp", 1).add("input-file", -1);
|
|
|
|
po::variables_map vm;
|
|
po::store(po::command_line_parser(argc, argv)
|
|
.options(cmdline_desc)
|
|
.positional(positional_desc)
|
|
.run(),
|
|
vm);
|
|
|
|
std::ifstream configfile(get_config_path());
|
|
po::store(po::parse_config_file(configfile, cmdline_desc, true), vm);
|
|
configfile.close();
|
|
|
|
po::notify(vm);
|
|
|
|
if (vm.count("help") != 0)
|
|
{
|
|
cout << translate("Usage: epubgrep [OPTION]… PATTERN [FILE]…\n");
|
|
cout << cmdline_desc;
|
|
}
|
|
else if (vm.count("version") != 0)
|
|
{
|
|
cout << "epubgrep " << epubgrep::version << '\n';
|
|
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");
|
|
}
|
|
|
|
return vm;
|
|
}
|
|
|
|
fs::path get_config_path()
|
|
{
|
|
auto get_env = [](std::string const &name)
|
|
{
|
|
const char *env = std::getenv(name.c_str());
|
|
if (env != nullptr)
|
|
{
|
|
return env;
|
|
}
|
|
|
|
return "";
|
|
};
|
|
|
|
fs::path path{get_env("XDG_CONFIG_HOME")};
|
|
if (path.empty())
|
|
{
|
|
path = get_env("HOME");
|
|
if (!path.empty())
|
|
{
|
|
path /= ".config";
|
|
}
|
|
}
|
|
if (!path.empty())
|
|
{
|
|
return path /= "epubgrep.conf";
|
|
}
|
|
|
|
return "epubgrep.conf";
|
|
}
|
|
|
|
} // namespace epubgrep::options
|