2015-05-27 12:08:46 +00:00
|
|
|
// Copyright (c) 2012-2015, The CryptoNote developers, The Bytecoin developers
|
2014-08-13 10:38:35 +00:00
|
|
|
//
|
|
|
|
// This file is part of Bytecoin.
|
|
|
|
//
|
|
|
|
// Bytecoin is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Bytecoin 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 Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with Bytecoin. If not, see <http://www.gnu.org/licenses/>.
|
2014-03-03 22:07:58 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
#include <boost/program_options/parsers.hpp>
|
|
|
|
#include <boost/program_options/options_description.hpp>
|
|
|
|
#include <boost/program_options/variables_map.hpp>
|
|
|
|
|
|
|
|
namespace command_line
|
|
|
|
{
|
|
|
|
template<typename T, bool required = false>
|
|
|
|
struct arg_descriptor;
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct arg_descriptor<T, false>
|
|
|
|
{
|
|
|
|
typedef T value_type;
|
|
|
|
|
|
|
|
const char* name;
|
|
|
|
const char* description;
|
|
|
|
T default_value;
|
|
|
|
bool not_use_default;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct arg_descriptor<std::vector<T>, false>
|
|
|
|
{
|
|
|
|
typedef std::vector<T> value_type;
|
|
|
|
|
|
|
|
const char* name;
|
|
|
|
const char* description;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct arg_descriptor<T, true>
|
|
|
|
{
|
|
|
|
static_assert(!std::is_same<T, bool>::value, "Boolean switch can't be required");
|
|
|
|
|
|
|
|
typedef T value_type;
|
|
|
|
|
|
|
|
const char* name;
|
|
|
|
const char* description;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
boost::program_options::typed_value<T, char>* make_semantic(const arg_descriptor<T, true>& /*arg*/)
|
|
|
|
{
|
|
|
|
return boost::program_options::value<T>()->required();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
boost::program_options::typed_value<T, char>* make_semantic(const arg_descriptor<T, false>& arg)
|
|
|
|
{
|
|
|
|
auto semantic = boost::program_options::value<T>();
|
|
|
|
if (!arg.not_use_default)
|
|
|
|
semantic->default_value(arg.default_value);
|
|
|
|
return semantic;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
boost::program_options::typed_value<T, char>* make_semantic(const arg_descriptor<T, false>& arg, const T& def)
|
|
|
|
{
|
|
|
|
auto semantic = boost::program_options::value<T>();
|
|
|
|
if (!arg.not_use_default)
|
|
|
|
semantic->default_value(def);
|
|
|
|
return semantic;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
boost::program_options::typed_value<std::vector<T>, char>* make_semantic(const arg_descriptor<std::vector<T>, false>& /*arg*/)
|
|
|
|
{
|
|
|
|
auto semantic = boost::program_options::value< std::vector<T> >();
|
|
|
|
semantic->default_value(std::vector<T>(), "");
|
|
|
|
return semantic;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, bool required>
|
|
|
|
void add_arg(boost::program_options::options_description& description, const arg_descriptor<T, required>& arg, bool unique = true)
|
|
|
|
{
|
2015-05-27 12:08:46 +00:00
|
|
|
if (unique && 0 != description.find_nothrow(arg.name, false))
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
2015-05-27 12:08:46 +00:00
|
|
|
std::cerr << "Argument already exists: " << arg.name << std::endl;
|
2014-03-03 22:07:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
description.add_options()(arg.name, make_semantic(arg), arg.description);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void add_arg(boost::program_options::options_description& description, const arg_descriptor<T, false>& arg, const T& def, bool unique = true)
|
|
|
|
{
|
2015-05-27 12:08:46 +00:00
|
|
|
if (unique && 0 != description.find_nothrow(arg.name, false))
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
2015-05-27 12:08:46 +00:00
|
|
|
std::cerr << "Argument already exists: " << arg.name << std::endl;
|
2014-03-03 22:07:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
description.add_options()(arg.name, make_semantic(arg, def), arg.description);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline void add_arg(boost::program_options::options_description& description, const arg_descriptor<bool, false>& arg, bool unique)
|
|
|
|
{
|
2015-05-27 12:08:46 +00:00
|
|
|
if (unique && 0 != description.find_nothrow(arg.name, false))
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
2015-05-27 12:08:46 +00:00
|
|
|
std::cerr << "Argument already exists: " << arg.name << std::endl;
|
2014-03-03 22:07:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
description.add_options()(arg.name, boost::program_options::bool_switch(), arg.description);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename charT>
|
|
|
|
boost::program_options::basic_parsed_options<charT> parse_command_line(int argc, const charT* const argv[],
|
|
|
|
const boost::program_options::options_description& desc, bool allow_unregistered = false)
|
|
|
|
{
|
|
|
|
auto parser = boost::program_options::command_line_parser(argc, argv);
|
|
|
|
parser.options(desc);
|
|
|
|
if (allow_unregistered)
|
|
|
|
{
|
|
|
|
parser.allow_unregistered();
|
|
|
|
}
|
|
|
|
return parser.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename F>
|
|
|
|
bool handle_error_helper(const boost::program_options::options_description& desc, F parser)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return parser();
|
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
|
|
|
std::cerr << "Failed to parse arguments: " << e.what() << std::endl;
|
|
|
|
std::cerr << desc << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
std::cerr << "Failed to parse arguments: unknown exception" << std::endl;
|
|
|
|
std::cerr << desc << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, bool required>
|
|
|
|
bool has_arg(const boost::program_options::variables_map& vm, const arg_descriptor<T, required>& arg)
|
|
|
|
{
|
|
|
|
auto value = vm[arg.name];
|
|
|
|
return !value.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T, bool required>
|
|
|
|
T get_arg(const boost::program_options::variables_map& vm, const arg_descriptor<T, required>& arg)
|
|
|
|
{
|
|
|
|
return vm[arg.name].template as<T>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline bool has_arg<bool, false>(const boost::program_options::variables_map& vm, const arg_descriptor<bool, false>& arg)
|
|
|
|
{
|
|
|
|
return get_arg<bool, false>(vm, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern const arg_descriptor<bool> arg_help;
|
|
|
|
extern const arg_descriptor<bool> arg_version;
|
|
|
|
extern const arg_descriptor<std::string> arg_data_dir;
|
|
|
|
}
|