mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-04-29 20:23:55 +00:00
95 lines
2.4 KiB
C++
95 lines
2.4 KiB
C++
#pragma once
|
|
|
|
// Distributed under the LGPL v2.1 license. See accompanying
|
|
// file LICENSE or https://github.com/henryiii/CLI11 for details.
|
|
|
|
#include <string>
|
|
#include <sstream>
|
|
#include <iomanip>
|
|
#include <locale>
|
|
#include <type_traits>
|
|
#include <algorithm>
|
|
|
|
namespace CLI {
|
|
namespace detail {
|
|
|
|
|
|
/// Simple function to join a string
|
|
template <typename T>
|
|
std::string join(const T& v, std::string delim = ",") {
|
|
std::ostringstream s;
|
|
size_t start = 0;
|
|
for (const auto& i : v) {
|
|
if(start++ > 0)
|
|
s << delim;
|
|
s << i;
|
|
}
|
|
return s.str();
|
|
}
|
|
|
|
// Based on http://stackoverflow.com/questions/25829143/c-trim-whitespace-from-a-string
|
|
|
|
/// Trim whitespace from left of string
|
|
std::string& ltrim(std::string &str) {
|
|
auto it2 = std::find_if( str.begin() , str.end() , [](char ch){ return !std::isspace<char>(ch , std::locale::classic() ) ; } );
|
|
str.erase( str.begin() , it2);
|
|
return str;
|
|
}
|
|
|
|
/// Trim whitespace from right of string
|
|
std::string& rtrim(std::string &str) {
|
|
auto it1 = std::find_if( str.rbegin() , str.rend() , [](char ch){ return !std::isspace<char>(ch , std::locale::classic() ) ; } );
|
|
str.erase( it1.base() , str.end() );
|
|
return str;
|
|
}
|
|
|
|
/// Trim whitespace from string
|
|
std::string& trim(std::string &str) {
|
|
return ltrim(rtrim(str));
|
|
}
|
|
|
|
/// Make a copy of the string and then trim it
|
|
std::string trim_copy(const std::string &str) {
|
|
std::string s = str;
|
|
return ltrim(rtrim(s));
|
|
}
|
|
|
|
/// Print a two part "help" string
|
|
void format_help(std::stringstream &out, std::string name, std::string description, size_t wid) {
|
|
name = " " + name;
|
|
out << std::setw(wid) << std::left << name;
|
|
if(description != "") {
|
|
if(name.length()>=wid)
|
|
out << std::endl << std::setw(wid) << "";
|
|
out << description;
|
|
}
|
|
out << std::endl;
|
|
}
|
|
|
|
/// Verify the first character of an option
|
|
template<typename T>
|
|
bool valid_first_char(T c) {
|
|
return std::isalpha(c) || c=='_';
|
|
}
|
|
|
|
/// Verify following characters of an option
|
|
template<typename T>
|
|
bool valid_later_char(T c) {
|
|
return std::isalnum(c) || c=='_' || c=='.' || c=='-';
|
|
}
|
|
|
|
/// Verify an option name
|
|
inline bool valid_name_string(const std::string &str) {
|
|
if(str.size()<1 || !valid_first_char(str[0]))
|
|
return false;
|
|
for(auto c : str.substr(1))
|
|
if(!valid_later_char(c))
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|