cxxopts/src/cxxopts.hpp

1123 lines
21 KiB
C++
Raw Normal View History

2014-10-02 15:11:50 +04:00
/*
Copyright (c) 2014 Jarryd Beck
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
2014-10-14 10:45:13 +04:00
#ifndef CXX_OPTS_HPP
#define CXX_OPTS_HPP
2014-09-27 11:04:58 +04:00
#include <exception>
2014-10-13 13:52:06 +04:00
#include <iostream>
#include <map>
#include <memory>
2014-10-15 04:38:39 +04:00
#include <regex>
#include <sstream>
#include <string>
#include <vector>
2014-10-13 13:52:06 +04:00
2014-10-27 05:30:22 +03:00
#ifdef CXXOPTS_USE_UNICODE
2014-10-27 07:20:12 +03:00
#include <unicode/unistr.h>
2014-10-27 05:30:22 +03:00
namespace cxxopts
{
typedef icu::UnicodeString String;
inline
String
toLocalString(std::string s)
{
return icu::UnicodeString::fromUTF8(s);
}
2014-10-27 07:20:12 +03:00
class UnicodeStringIterator : public
std::iterator<std::forward_iterator_tag, int32_t>
{
public:
UnicodeStringIterator(const icu::UnicodeString* s, int32_t pos)
: s(s)
, i(pos)
{
}
value_type
operator*() const
{
return s->char32At(i);
}
bool
operator==(const UnicodeStringIterator& rhs) const
{
return s == rhs.s && i == rhs.i;
}
bool
operator!=(const UnicodeStringIterator& rhs) const
{
return !(*this == rhs);
}
UnicodeStringIterator&
operator++()
{
++i;
return *this;
}
UnicodeStringIterator
operator+(int32_t v)
{
return UnicodeStringIterator(s, i + v);
}
private:
const icu::UnicodeString* s;
int32_t i;
};
2014-10-27 08:45:13 +03:00
inline
String&
stringAppend(String&s, String a)
{
return s.append(std::move(a));
}
inline
String&
stringAppend(String& s, int n, UChar32 c)
{
for (int i = 0; i != n; ++i)
{
s.append(c);
}
return s;
}
template <typename Iterator>
String&
stringAppend(String& s, Iterator begin, Iterator end)
{
while (begin != end)
{
s.append(*begin);
++begin;
}
return s;
}
inline
size_t
stringLength(const String& s)
{
return s.length();
}
inline
std::string
toUTF8String(const String& s)
{
std::string result;
s.toUTF8String(result);
return result;
}
2014-10-27 07:20:12 +03:00
}
namespace std
{
cxxopts::UnicodeStringIterator
begin(const icu::UnicodeString& s)
{
return cxxopts::UnicodeStringIterator(&s, 0);
}
cxxopts::UnicodeStringIterator
end(const icu::UnicodeString& s)
{
return cxxopts::UnicodeStringIterator(&s, s.length());
}
2014-10-27 05:30:22 +03:00
}
#else
namespace cxxopts
{
typedef std::string String;
2014-10-27 07:20:12 +03:00
template <typename T>
T
toLocalString(T&& t)
{
return t;
}
2014-10-27 05:30:22 +03:00
inline
2014-10-27 07:20:12 +03:00
size_t
stringLength(const String& s)
2014-10-27 05:30:22 +03:00
{
2014-10-27 07:20:12 +03:00
return s.length();
2014-10-27 05:30:22 +03:00
}
2014-10-27 08:45:13 +03:00
inline
String&
stringAppend(String&s, String a)
{
return s.append(std::move(a));
}
inline
String&
stringAppend(String& s, int n, char c)
{
return s.append(n, c);
}
template <typename Iterator>
String&
stringAppend(String& s, Iterator begin, Iterator end)
{
return s.append(begin, end);
}
template <typename T>
std::string
toUTF8String(T&& t)
{
return std::forward<T>(t);
}
2014-10-27 05:30:22 +03:00
}
#endif
2014-09-18 09:16:51 +04:00
namespace cxxopts
{
2014-09-27 16:04:53 +04:00
class Value
{
public:
virtual void
2014-10-02 10:38:13 +04:00
parse(const std::string& text) const = 0;
2014-09-27 16:04:53 +04:00
virtual bool
has_arg() const = 0;
};
2014-09-28 15:09:21 +04:00
class OptionException : public std::exception
2014-09-27 11:04:58 +04:00
{
public:
2014-09-28 15:09:21 +04:00
OptionException(const std::string& message)
: m_message(message)
2014-09-27 11:04:58 +04:00
{
}
2014-09-28 15:09:21 +04:00
virtual const char*
2014-09-27 11:04:58 +04:00
what() const noexcept
{
return m_message.c_str();
}
private:
std::string m_message;
};
2014-09-28 15:09:21 +04:00
class OptionSpecException : public OptionException
2014-09-27 11:04:58 +04:00
{
public:
2014-09-28 15:09:21 +04:00
OptionSpecException(const std::string& message)
: OptionException(message)
2014-09-27 11:04:58 +04:00
{
}
2014-09-28 15:09:21 +04:00
};
2014-09-27 11:04:58 +04:00
2014-09-28 15:09:21 +04:00
class OptionParseException : public OptionException
{
public:
OptionParseException(const std::string& message)
: OptionException(message)
2014-09-27 11:04:58 +04:00
{
}
2014-09-28 15:09:21 +04:00
};
2014-09-27 11:04:58 +04:00
2014-09-28 15:09:21 +04:00
class option_exists_error : public OptionSpecException
{
public:
option_exists_error(const std::string& option)
: OptionSpecException(u8"Option " + option + u8" already exists")
{
}
};
class invalid_option_format_error : public OptionSpecException
{
public:
invalid_option_format_error(const std::string& format)
: OptionSpecException(u8"Invalid option format " + format + u8"")
{
}
2014-09-27 11:04:58 +04:00
};
2014-09-28 15:41:19 +04:00
class option_not_exists_exception : public OptionParseException
{
public:
option_not_exists_exception(const std::string& option)
: OptionParseException(u8"Option " + option + u8" does not exist")
{
}
};
2014-10-01 11:36:43 +04:00
class missing_argument_exception : public OptionParseException
{
public:
missing_argument_exception(const std::string& option)
: OptionParseException(u8"Option " + option + u8" is missing an argument")
{
}
};
2014-09-28 15:41:19 +04:00
class option_requires_argument_exception : public OptionParseException
{
public:
option_requires_argument_exception(const std::string& option)
: OptionParseException(u8"Option " + option + u8" requires an argument")
{
}
};
2014-10-01 11:36:43 +04:00
class option_not_has_argument_exception : public OptionParseException
{
public:
option_not_has_argument_exception
(
const std::string& option,
2014-10-01 11:36:43 +04:00
const std::string& arg
)
: OptionParseException(
u8"Option " + option + u8" does not take an argument, but argument"
+ arg + " given")
{
}
};
2014-09-29 12:16:05 +04:00
class option_not_present_exception : public OptionParseException
{
public:
option_not_present_exception(const std::string& option)
: OptionParseException(u8"Option " + option + u8" not present")
{
}
};
2014-10-13 13:52:06 +04:00
class argument_incorrect_type : public OptionParseException
{
public:
argument_incorrect_type
(
const std::string& arg
)
: OptionParseException(
u8"Argument " + arg + u8" failed to parse"
)
{
}
};
namespace values
{
template <typename T>
void
parse_value(const std::string& text, T& value)
{
std::istringstream is(text);
if (!(is >> value))
{
std::cerr << "cannot parse empty value" << std::endl;
throw argument_incorrect_type(text);
}
if (!is.eof())
{
throw argument_incorrect_type(text);
}
}
template <typename T>
void
parse_value(const std::string& text, std::vector<T>& value)
{
T v;
parse_value(text, v);
value.push_back(v);
}
2014-10-15 04:38:39 +04:00
inline
2014-10-13 13:52:06 +04:00
void
parse_value(const std::string& text, bool& value)
{
2014-10-14 09:59:25 +04:00
//TODO recognise on, off, yes, no, enable, disable
//so that we can write --long=yes explicitly
2014-10-13 13:52:06 +04:00
value = true;
}
template <typename T>
struct value_has_arg
{
static constexpr bool value = true;
};
template <>
struct value_has_arg<bool>
{
static constexpr bool value = false;
};
template <typename T>
class default_value : public Value
{
public:
default_value()
: m_result(std::make_shared<T>())
, m_store(m_result.get())
{
}
default_value(T* t)
: m_store(t)
{
}
void
parse(const std::string& text) const
{
parse_value(text, *m_store);
}
bool
has_arg() const
{
return value_has_arg<T>::value;
}
const T&
get() const
{
if (m_store == nullptr)
{
return *m_result;
}
else
{
return *m_store;
}
}
private:
std::shared_ptr<T> m_result;
T* m_store;
};
}
template <typename T>
std::shared_ptr<Value>
value()
{
return std::make_shared<values::default_value<T>>();
}
2014-10-14 09:59:25 +04:00
template <typename T>
std::shared_ptr<Value>
value(T& t)
{
return std::make_shared<values::default_value<T>>(&t);
}
2014-09-25 20:19:23 +04:00
class OptionAdder;
2014-09-27 11:04:58 +04:00
class OptionDetails
{
public:
2014-09-27 16:04:53 +04:00
OptionDetails
(
2014-10-27 05:30:22 +03:00
const String& description,
2014-09-27 16:04:53 +04:00
std::shared_ptr<const Value> value
)
2014-09-27 11:04:58 +04:00
: m_desc(description)
2014-10-02 10:38:13 +04:00
, m_value(value)
, m_count(0)
2014-09-27 16:04:53 +04:00
{
}
2014-10-27 05:30:22 +03:00
const String&
2014-09-27 16:04:53 +04:00
description() const
{
return m_desc;
}
bool
has_arg() const
2014-09-27 11:04:58 +04:00
{
2014-10-02 10:38:13 +04:00
return m_value->has_arg();
2014-09-27 11:04:58 +04:00
}
2014-09-28 15:09:21 +04:00
void
2014-10-02 10:38:13 +04:00
parse(const std::string& text)
2014-09-28 15:09:21 +04:00
{
2014-10-02 10:38:13 +04:00
m_value->parse(text);
++m_count;
2014-09-28 15:09:21 +04:00
}
2014-10-02 10:38:13 +04:00
int
count() const
{
return m_count;
}
2014-09-29 12:13:21 +04:00
2014-10-02 10:38:13 +04:00
template <typename T>
const T&
as() const
2014-09-29 12:13:21 +04:00
{
2014-10-02 10:38:13 +04:00
return dynamic_cast<const values::default_value<T>&>(*m_value).get();
2014-09-29 12:13:21 +04:00
}
2014-10-02 10:38:13 +04:00
private:
2014-10-27 05:30:22 +03:00
String m_desc;
2014-10-02 10:38:13 +04:00
std::shared_ptr<const Value> m_value;
int m_count;
2014-09-29 12:13:21 +04:00
};
2014-10-14 09:59:54 +04:00
struct HelpOptionDetails
2014-10-10 10:29:49 +04:00
{
2014-10-10 11:03:18 +04:00
std::string s;
std::string l;
2014-10-27 05:30:22 +03:00
String desc;
2014-10-10 11:03:18 +04:00
bool has_arg;
2014-10-29 04:35:05 +03:00
std::string arg_help;
2014-10-10 10:29:49 +04:00
};
2014-10-14 09:59:54 +04:00
struct HelpGroupDetails
{
std::string name;
std::string description;
std::vector<HelpOptionDetails> options;
};
2014-09-25 20:19:23 +04:00
class Options
{
public:
2014-10-14 06:29:30 +04:00
Options(std::string program, std::string help_string = "")
: m_program(std::move(program))
2014-10-27 08:45:13 +03:00
, m_help_string(toLocalString(std::move(help_string)))
2014-10-14 06:29:30 +04:00
{
}
2014-10-15 04:38:39 +04:00
inline
2014-09-25 20:19:23 +04:00
void
parse(int& argc, char**& argv);
2014-10-15 04:38:39 +04:00
inline
2014-09-25 20:19:23 +04:00
OptionAdder
2014-10-14 03:49:14 +04:00
add_options(std::string group = "");
2014-09-25 20:19:23 +04:00
2014-10-15 04:38:39 +04:00
inline
2014-10-10 10:29:49 +04:00
void
add_option
(
2014-10-14 03:49:14 +04:00
const std::string& group,
const std::string& s,
const std::string& l,
2014-10-27 05:30:22 +03:00
std::string desc,
2014-10-29 04:35:05 +03:00
std::shared_ptr<const Value> value,
std::string arg_help
2014-10-10 10:29:49 +04:00
);
2014-09-29 12:13:21 +04:00
int
count(const std::string& o) const
{
2014-10-02 10:38:13 +04:00
auto iter = m_options.find(o);
if (iter == m_options.end())
2014-09-29 12:13:21 +04:00
{
return 0;
}
2014-10-02 10:38:13 +04:00
return iter->second->count();
2014-09-29 12:13:21 +04:00
}
2014-10-02 10:38:13 +04:00
const OptionDetails&
2014-09-29 12:16:05 +04:00
operator[](const std::string& option) const
{
2014-10-02 10:38:13 +04:00
auto iter = m_options.find(option);
2014-09-29 12:16:05 +04:00
2014-10-02 10:38:13 +04:00
if (iter == m_options.end())
2014-09-29 12:16:05 +04:00
{
throw option_not_present_exception(option);
}
2014-10-02 10:38:13 +04:00
return *iter->second;
2014-09-29 12:16:05 +04:00
}
2014-10-09 04:08:36 +04:00
//parse positional arguments into the given option
2014-10-15 04:38:39 +04:00
inline
2014-10-09 04:08:36 +04:00
void
parse_positional(std::string option);
2014-10-15 04:38:39 +04:00
inline
2014-10-27 08:45:13 +03:00
std::string
2014-10-14 09:59:54 +04:00
help(const std::vector<std::string>& groups = {""}) const;
2014-10-10 10:29:49 +04:00
2014-09-25 20:19:23 +04:00
private:
2014-10-15 04:38:39 +04:00
inline
2014-10-10 10:29:49 +04:00
void
add_one_option
(
const std::string& option,
std::shared_ptr<OptionDetails> details
);
2014-09-25 20:19:23 +04:00
2014-10-15 04:38:39 +04:00
inline
2014-10-09 04:08:36 +04:00
bool
consume_positional(std::string a);
2014-10-15 04:38:39 +04:00
inline
2014-10-09 04:08:36 +04:00
void
add_to_option(const std::string& option, const std::string& arg);
2014-10-15 04:38:39 +04:00
inline
2014-10-01 11:36:43 +04:00
void
parse_option
(
std::shared_ptr<OptionDetails> value,
const std::string& name,
2014-10-01 11:36:43 +04:00
const std::string& arg = ""
);
2014-10-15 04:38:39 +04:00
inline
2014-10-01 11:36:43 +04:00
void
checked_parse_arg
(
int argc,
char* argv[],
int argPos,
std::shared_ptr<OptionDetails> value,
const std::string& name
);
2014-10-15 04:38:39 +04:00
inline
2014-10-27 07:20:12 +03:00
String
2014-10-14 09:59:54 +04:00
help_one_group(const std::string& group) const;
2014-10-14 06:29:30 +04:00
std::string m_program;
2014-10-27 08:45:13 +03:00
String m_help_string;
2014-10-14 06:29:30 +04:00
2014-10-02 10:38:13 +04:00
std::map<std::string, std::shared_ptr<OptionDetails>> m_options;
2014-10-09 04:08:36 +04:00
std::string m_positional;
2014-10-10 10:29:49 +04:00
//mapping from groups to help options
2014-10-14 09:59:54 +04:00
std::map<std::string, HelpGroupDetails> m_help;
2014-09-25 20:19:23 +04:00
};
class OptionAdder
{
public:
2014-10-14 03:49:14 +04:00
OptionAdder(Options& options, std::string group)
: m_options(options), m_group(std::move(group))
2014-09-25 20:19:23 +04:00
{
}
2014-10-15 04:38:39 +04:00
inline
2014-09-25 20:19:23 +04:00
OptionAdder&
operator()
(
const std::string& opts,
2014-09-27 16:04:53 +04:00
const std::string& desc,
std::shared_ptr<const Value> value
2014-10-29 04:35:05 +03:00
= ::cxxopts::value<bool>(),
std::string arg_help = ""
2014-09-25 20:19:23 +04:00
);
private:
Options& m_options;
2014-10-14 03:49:14 +04:00
std::string m_group;
2014-09-25 20:19:23 +04:00
};
2014-10-01 14:56:30 +04:00
2014-09-18 09:16:51 +04:00
}
2014-10-14 10:45:13 +04:00
2014-10-15 04:38:39 +04:00
namespace cxxopts
{
namespace
{
constexpr int OPTION_LONGEST = 30;
constexpr int OPTION_DESC_GAP = 2;
std::basic_regex<char> option_matcher
2014-10-26 21:12:32 +03:00
("--([[:alnum:]][-_[:alnum:]]+)(=(.*))?|-([a-zA-Z]+)");
2014-10-15 04:38:39 +04:00
std::basic_regex<char> option_specifier
2014-10-26 21:12:32 +03:00
("(([a-zA-Z]),)?([a-zA-Z0-9][-_a-zA-Z0-9]+)");
2014-10-15 04:38:39 +04:00
2014-10-27 05:30:22 +03:00
String
2014-10-15 04:38:39 +04:00
format_option
(
const std::string& s,
const std::string& l,
2014-10-29 04:35:05 +03:00
bool has_arg,
const std::string& arg_help
2014-10-15 04:38:39 +04:00
)
{
2014-10-27 07:20:12 +03:00
String result = " ";
2014-10-15 04:38:39 +04:00
if (s.size() > 0)
{
2014-10-27 07:20:12 +03:00
result += "-" + toLocalString(s) + ",";
2014-10-15 04:38:39 +04:00
}
else
{
result += " ";
}
if (l.size() > 0)
{
2014-10-27 07:20:12 +03:00
result += " --" + toLocalString(l);
2014-10-15 04:38:39 +04:00
}
if (has_arg)
{
2014-10-29 04:35:05 +03:00
if (arg_help.size() != 0)
{
result += " " + toLocalString(arg_help);
}
else
{
result += " arg";
}
2014-10-15 04:38:39 +04:00
}
return result;
}
2014-10-27 05:30:22 +03:00
String
2014-10-15 04:38:39 +04:00
format_description
(
2014-10-27 05:30:22 +03:00
const String& text,
2014-10-15 04:38:39 +04:00
int start,
int width
)
{
2014-10-27 05:30:22 +03:00
String result;
2014-10-15 04:38:39 +04:00
2014-10-27 07:20:12 +03:00
auto current = std::begin(text);
2014-10-15 04:38:39 +04:00
auto startLine = current;
auto lastSpace = current;
int size = 0;
2014-10-27 07:20:12 +03:00
while (current != std::end(text))
2014-10-15 04:38:39 +04:00
{
if (*current == ' ')
{
lastSpace = current;
}
if (size > width)
{
if (lastSpace == startLine)
{
2014-10-27 08:45:13 +03:00
stringAppend(result, startLine, current + 1);
stringAppend(result, "\n");
stringAppend(result, start, ' ');
2014-10-15 04:38:39 +04:00
startLine = current + 1;
lastSpace = startLine;
}
else
{
2014-10-27 08:45:13 +03:00
stringAppend(result, startLine, lastSpace);
stringAppend(result, "\n");
stringAppend(result, start, ' ');
2014-10-15 04:38:39 +04:00
startLine = lastSpace + 1;
}
size = 0;
}
else
{
++size;
}
++current;
}
//append whatever is left
2014-10-27 08:45:13 +03:00
stringAppend(result, startLine, current);
2014-10-15 04:38:39 +04:00
return result;
}
}
OptionAdder
Options::add_options(std::string group)
{
return OptionAdder(*this, std::move(group));
}
OptionAdder&
OptionAdder::operator()
(
const std::string& opts,
const std::string& desc,
2014-10-29 04:35:05 +03:00
std::shared_ptr<const Value> value,
std::string arg_help
2014-10-15 04:38:39 +04:00
)
{
std::match_results<const char*> result;
std::regex_match(opts.c_str(), result, option_specifier);
if (result.empty())
{
throw invalid_option_format_error(opts);
}
const auto& s = result[2];
const auto& l = result[3];
2014-10-29 04:35:05 +03:00
m_options.add_option(m_group, s.str(), l.str(), desc, value,
std::move(arg_help));
2014-10-15 04:38:39 +04:00
return *this;
}
void
Options::parse_option
(
std::shared_ptr<OptionDetails> value,
const std::string& name,
const std::string& arg
)
{
value->parse(arg);
}
void
Options::checked_parse_arg
(
int argc,
char* argv[],
int argPos,
std::shared_ptr<OptionDetails> value,
const std::string& name
)
{
if (argPos >= argc)
{
throw missing_argument_exception(name);
}
parse_option(value, name, argv[argPos]);
}
void
Options::add_to_option(const std::string& option, const std::string& arg)
{
auto iter = m_options.find(option);
if (iter == m_options.end())
{
throw option_not_exists_exception(option);
}
parse_option(iter->second, option, arg);
}
bool
Options::consume_positional(std::string a)
{
if (m_positional.size() > 0)
{
add_to_option(m_positional, a);
return true;
}
else
{
return false;
}
}
void
Options::parse_positional(std::string option)
{
m_positional = std::move(option);
}
void
Options::parse(int& argc, char**& argv)
{
int current = 1;
int nextKeep = 1;
while (current != argc)
{
std::match_results<const char*> result;
std::regex_match(argv[current], result, option_matcher);
if (result.empty())
{
//not a flag
//if true is returned here then it was consumed, otherwise it is
//ignored
if (consume_positional(argv[current]))
{
}
else
{
argv[nextKeep] = argv[current];
++nextKeep;
}
//if we return from here then it was parsed successfully, so continue
}
else
{
//short or long option?
if (result[4].length() != 0)
{
const std::string& s = result[4];
for (int i = 0; i != s.size(); ++i)
{
std::string name(1, s[i]);
auto iter = m_options.find(name);
if (iter == m_options.end())
{
throw option_not_exists_exception(name);
}
auto value = iter->second;
//if no argument then just add it
if (!value->has_arg())
{
parse_option(value, name);
}
else
{
//it must be the last argument
if (i + 1 == s.size())
{
checked_parse_arg(argc, argv, current+1, value, name);
++current;
}
else
{
//error
throw option_requires_argument_exception(name);
}
}
}
}
else if (result[1].length() != 0)
{
const std::string& name = result[1];
auto iter = m_options.find(name);
if (iter == m_options.end())
{
throw option_not_exists_exception(name);
}
auto opt = iter->second;
//equals provided for long option?
if (result[3].length() != 0)
{
//parse the option given
//but if it doesn't take an argument, this is an error
if (!opt->has_arg())
{
throw option_not_has_argument_exception(name, result[3]);
}
parse_option(opt, name, result[3]);
}
else
{
if (opt->has_arg())
{
//parse the next argument
checked_parse_arg(argc, argv, current + 1, opt, name);
++current;
}
else
{
//parse with empty argument
parse_option(opt, name);
}
}
}
}
++current;
}
argc = nextKeep;
}
void
Options::add_option
(
const std::string& group,
const std::string& s,
const std::string& l,
2014-10-27 05:30:22 +03:00
std::string desc,
2014-10-29 04:35:05 +03:00
std::shared_ptr<const Value> value,
std::string arg_help
2014-10-15 04:38:39 +04:00
)
{
2014-10-27 05:30:22 +03:00
auto stringDesc = toLocalString(std::move(desc));
auto option = std::make_shared<OptionDetails>(stringDesc, value);
2014-10-15 04:38:39 +04:00
if (s.size() > 0)
{
add_one_option(s, option);
}
if (l.size() > 0)
{
add_one_option(l, option);
}
//add the help details
auto& options = m_help[group];
2014-10-27 05:30:22 +03:00
options.options.
2014-10-29 04:35:05 +03:00
emplace_back(HelpOptionDetails{s, l, stringDesc, value->has_arg(),
std::move(arg_help)}
);
2014-10-15 04:38:39 +04:00
}
void
Options::add_one_option
(
const std::string& option,
std::shared_ptr<OptionDetails> details
)
{
auto in = m_options.emplace(option, details);
if (!in.second)
{
throw option_exists_error(option);
}
}
2014-10-27 07:20:12 +03:00
String
2014-10-15 04:38:39 +04:00
Options::help_one_group(const std::string& g) const
{
2014-10-27 08:45:13 +03:00
typedef std::vector<std::pair<String, String>> OptionHelp;
2014-10-15 04:38:39 +04:00
auto group = m_help.find(g);
if (group == m_help.end())
{
return "";
}
OptionHelp format;
size_t longest = 0;
2014-10-27 07:20:12 +03:00
String result;
2014-10-15 04:38:39 +04:00
if (!g.empty())
{
2014-10-27 08:45:13 +03:00
result += toLocalString(" " + g + " options:\n\n");
2014-10-15 04:38:39 +04:00
}
for (const auto& o : group->second.options)
{
2014-10-29 04:35:05 +03:00
auto s = format_option(o.s, o.l, o.has_arg, o.arg_help);
2014-10-27 07:20:12 +03:00
longest = std::max(longest, stringLength(s));
2014-10-27 08:45:13 +03:00
format.push_back(std::make_pair(s, String()));
2014-10-15 04:38:39 +04:00
}
longest = std::min(longest, static_cast<size_t>(OPTION_LONGEST));
//widest allowed description
int allowed = 76 - longest - OPTION_DESC_GAP;
auto fiter = format.begin();
for (const auto& o : group->second.options)
{
auto d = format_description(o.desc, longest + OPTION_DESC_GAP, allowed);
result += fiter->first;
2014-10-27 07:20:12 +03:00
if (stringLength(fiter->first) > longest)
2014-10-15 04:38:39 +04:00
{
result += "\n";
2014-10-27 08:45:13 +03:00
result += toLocalString(std::string(longest + OPTION_DESC_GAP, ' '));
2014-10-15 04:38:39 +04:00
}
else
{
2014-10-27 08:45:13 +03:00
result += toLocalString(std::string(longest + OPTION_DESC_GAP -
2014-10-27 07:20:12 +03:00
stringLength(fiter->first),
2014-10-27 08:45:13 +03:00
' '));
2014-10-15 04:38:39 +04:00
}
result += d;
result += "\n";
++fiter;
}
return result;
}
2014-10-27 08:45:13 +03:00
std::string
2014-10-15 04:38:39 +04:00
Options::help(const std::vector<std::string>& groups) const
{
2014-10-27 08:45:13 +03:00
String result = "Usage:\n " + toLocalString(m_program) + " [OPTION...]"
2014-10-15 04:38:39 +04:00
+ m_help_string + "\n\n";
2014-10-26 21:18:41 +03:00
for (std::size_t i = 0; i < groups.size(); ++i)
2014-10-15 04:38:39 +04:00
{
2014-10-26 21:18:41 +03:00
result += help_one_group(groups[i]);
if (i < groups.size() - 1)
{
result += "\n";
}
2014-10-15 04:38:39 +04:00
}
2014-10-27 08:45:13 +03:00
return toUTF8String(result);
2014-10-15 04:38:39 +04:00
}
}
2014-10-14 10:45:13 +04:00
#endif //CXX_OPTS_HPP