This commit is contained in:
Jarryd Beck 2014-10-27 16:45:13 +11:00
parent 9d1a4dfcdc
commit 9509ca94f2
3 changed files with 102 additions and 19 deletions

View File

@ -23,5 +23,9 @@ project(cxxopts)
set(VERSION "0.0.1") set(VERSION "0.0.1")
find_package(PkgConfig)
pkg_check_modules(ICU REQUIRED icu-uc)
add_subdirectory(src) add_subdirectory(src)

View File

@ -21,5 +21,6 @@
add_executable(example example.cpp) add_executable(example example.cpp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall")
target_link_libraries(example ${ICU_LIBRARIES})
install(FILES cxxopts.hpp DESTINATION include) install(FILES cxxopts.hpp DESTINATION include)

View File

@ -94,6 +94,55 @@ namespace cxxopts
const icu::UnicodeString* s; const icu::UnicodeString* s;
int32_t i; int32_t i;
}; };
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;
}
} }
namespace std namespace std
@ -130,6 +179,35 @@ namespace cxxopts
{ {
return s.length(); return s.length();
} }
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);
}
} }
#endif #endif
@ -452,7 +530,7 @@ namespace cxxopts
Options(std::string program, std::string help_string = "") Options(std::string program, std::string help_string = "")
: m_program(std::move(program)) : m_program(std::move(program))
, m_help_string(std::move(help_string)) , m_help_string(toLocalString(std::move(help_string)))
{ {
} }
@ -506,7 +584,7 @@ namespace cxxopts
parse_positional(std::string option); parse_positional(std::string option);
inline inline
String std::string
help(const std::vector<std::string>& groups = {""}) const; help(const std::vector<std::string>& groups = {""}) const;
private: private:
@ -552,7 +630,7 @@ namespace cxxopts
help_one_group(const std::string& group) const; help_one_group(const std::string& group) const;
std::string m_program; std::string m_program;
std::string m_help_string; String m_help_string;
std::map<std::string, std::shared_ptr<OptionDetails>> m_options; std::map<std::string, std::shared_ptr<OptionDetails>> m_options;
std::string m_positional; std::string m_positional;
@ -661,17 +739,17 @@ namespace cxxopts
{ {
if (lastSpace == startLine) if (lastSpace == startLine)
{ {
result.append(startLine, current + 1); stringAppend(result, startLine, current + 1);
result.append("\n"); stringAppend(result, "\n");
result.append(start, ' '); stringAppend(result, start, ' ');
startLine = current + 1; startLine = current + 1;
lastSpace = startLine; lastSpace = startLine;
} }
else else
{ {
result.append(startLine, lastSpace); stringAppend(result, startLine, lastSpace);
result.append("\n"); stringAppend(result, "\n");
result.append(start, ' '); stringAppend(result, start, ' ');
startLine = lastSpace + 1; startLine = lastSpace + 1;
} }
size = 0; size = 0;
@ -685,7 +763,7 @@ namespace cxxopts
} }
//append whatever is left //append whatever is left
result.append(startLine, current); stringAppend(result, startLine, current);
return result; return result;
} }
@ -949,7 +1027,7 @@ Options::add_one_option
String String
Options::help_one_group(const std::string& g) const Options::help_one_group(const std::string& g) const
{ {
typedef std::vector<std::pair<std::string, std::string>> OptionHelp; typedef std::vector<std::pair<String, String>> OptionHelp;
auto group = m_help.find(g); auto group = m_help.find(g);
if (group == m_help.end()) if (group == m_help.end())
@ -965,14 +1043,14 @@ Options::help_one_group(const std::string& g) const
if (!g.empty()) if (!g.empty())
{ {
result += " " + g + " options:\n\n"; result += toLocalString(" " + g + " options:\n\n");
} }
for (const auto& o : group->second.options) for (const auto& o : group->second.options)
{ {
auto s = format_option(o.s, o.l, o.has_arg); auto s = format_option(o.s, o.l, o.has_arg);
longest = std::max(longest, stringLength(s)); longest = std::max(longest, stringLength(s));
format.push_back(std::make_pair(s, std::string())); format.push_back(std::make_pair(s, String()));
} }
longest = std::min(longest, static_cast<size_t>(OPTION_LONGEST)); longest = std::min(longest, static_cast<size_t>(OPTION_LONGEST));
@ -989,13 +1067,13 @@ Options::help_one_group(const std::string& g) const
if (stringLength(fiter->first) > longest) if (stringLength(fiter->first) > longest)
{ {
result += "\n"; result += "\n";
result += std::string(longest + OPTION_DESC_GAP, ' '); result += toLocalString(std::string(longest + OPTION_DESC_GAP, ' '));
} }
else else
{ {
result += std::string(longest + OPTION_DESC_GAP - result += toLocalString(std::string(longest + OPTION_DESC_GAP -
stringLength(fiter->first), stringLength(fiter->first),
' '); ' '));
} }
result += d; result += d;
result += "\n"; result += "\n";
@ -1006,10 +1084,10 @@ Options::help_one_group(const std::string& g) const
return result; return result;
} }
String std::string
Options::help(const std::vector<std::string>& groups) const Options::help(const std::vector<std::string>& groups) const
{ {
std::string result = "Usage:\n " + m_program + " [OPTION...]" String result = "Usage:\n " + toLocalString(m_program) + " [OPTION...]"
+ m_help_string + "\n\n"; + m_help_string + "\n\n";
for (std::size_t i = 0; i < groups.size(); ++i) for (std::size_t i = 0; i < groups.size(); ++i)
@ -1021,7 +1099,7 @@ Options::help(const std::vector<std::string>& groups) const
} }
} }
return result; return toUTF8String(result);
} }
} }