Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
CLI::ConfigBase Class Reference

This converter works with INI/TOML files; to write proper TOML files use ConfigTOML. More...

#include <CLI11.hpp>

Inheritance diagram for CLI::ConfigBase:
Collaboration diagram for CLI::ConfigBase:

Public Member Functions

std::string to_config (const App *, bool default_also, bool write_description, std::string prefix) const override
 Convert an app into a configuration.
 
std::vector< ConfigItemfrom_config (std::istream &input) const override
 Convert a configuration into an app.
 
ConfigBasecomment (char cchar)
 Specify the configuration for comment characters.
 
ConfigBasearrayBounds (char aStart, char aEnd)
 Specify the start and end characters for an array.
 
ConfigBasearrayDelimiter (char aSep)
 Specify the delimiter character for an array.
 
ConfigBasevalueSeparator (char vSep)
 Specify the delimiter between a name and value.
 
- Public Member Functions inherited from CLI::Config
virtual std::string to_flag (const ConfigItem &item) const
 Get a flag value.
 
std::vector< ConfigItemfrom_file (const std::string &name)
 Parse a config file, throw an error (ParseError:ConfigParseError or FileError) on failure.
 
virtual ~Config ()=default
 Virtual destructor.
 

Protected Attributes

char commentChar = ';'
 the character used for comments
 
char arrayStart = '\0'
 the character used to start an array '\0' is a default to not use
 
char arrayEnd = '\0'
 the character used to end an array '\0' is a default to not use
 
char arraySeparator = ' '
 the character used to separate elements in an array
 
char valueDelimiter = '='
 the character used separate the name from the value
 
- Protected Attributes inherited from CLI::Config
std::vector< ConfigItemitems {}
 

Detailed Description

Definition at line 2015 of file CLI11.hpp.

Member Function Documentation

◆ arrayBounds()

ConfigBase * CLI::ConfigBase::arrayBounds ( char aStart,
char aEnd )
inline

Definition at line 2039 of file CLI11.hpp.

2039 {
2040 arrayStart = aStart;
2041 arrayEnd = aEnd;
2042 return this;
2043 }
char arrayStart
the character used to start an array '\0' is a default to not use
Definition CLI11.hpp:2020
char arrayEnd
the character used to end an array '\0' is a default to not use
Definition CLI11.hpp:2022

◆ arrayDelimiter()

ConfigBase * CLI::ConfigBase::arrayDelimiter ( char aSep)
inline

Definition at line 2045 of file CLI11.hpp.

2045 {
2046 arraySeparator = aSep;
2047 return this;
2048 }
char arraySeparator
the character used to separate elements in an array
Definition CLI11.hpp:2024

◆ comment()

ConfigBase * CLI::ConfigBase::comment ( char cchar)
inline

Definition at line 2034 of file CLI11.hpp.

2034 {
2035 commentChar = cchar;
2036 return this;
2037 }
char commentChar
the character used for comments
Definition CLI11.hpp:2018

◆ from_config()

std::vector< ConfigItem > CLI::ConfigBase::from_config ( std::istream & ) const
inlineoverridevirtual

Implements CLI::Config.

Definition at line 7772 of file CLI11.hpp.

7772 {
7773 std::string line;
7774 std::string section = "default";
7775
7776 std::vector<ConfigItem> output;
7777 bool defaultArray = (arrayStart == '\0' || arrayStart == ' ') && arrayStart == arrayEnd;
7778 char aStart = (defaultArray) ? '[' : arrayStart;
7779 char aEnd = (defaultArray) ? ']' : arrayEnd;
7780 char aSep = (defaultArray && arraySeparator == ' ') ? ',' : arraySeparator;
7781
7782 while(getline(input, line)) {
7783 std::vector<std::string> items_buffer;
7784 std::string name;
7785
7786 detail::trim(line);
7787 std::size_t len = line.length();
7788 if(len > 1 && line.front() == '[' && line.back() == ']') {
7789 if(section != "default") {
7790 // insert a section end which is just an empty items_buffer
7791 output.emplace_back();
7792 output.back().parents = detail::generate_parents(section, name);
7793 output.back().name = "--";
7794 }
7795 section = line.substr(1, len - 2);
7796 // deal with double brackets for TOML
7797 if(section.size() > 1 && section.front() == '[' && section.back() == ']') {
7798 section = section.substr(1, section.size() - 2);
7799 }
7800 if(detail::to_lower(section) == "default") {
7801 section = "default";
7802 } else {
7803 detail::checkParentSegments(output, section);
7804 }
7805 continue;
7806 }
7807 if(len == 0) {
7808 continue;
7809 }
7810 // comment lines
7811 if(line.front() == ';' || line.front() == '#' || line.front() == commentChar) {
7812 continue;
7813 }
7814
7815 // Find = in string, split and recombine
7816 auto pos = line.find(valueDelimiter);
7817 if(pos != std::string::npos) {
7818 name = detail::trim_copy(line.substr(0, pos));
7819 std::string item = detail::trim_copy(line.substr(pos + 1));
7820 if(item.size() > 1 && item.front() == aStart && item.back() == aEnd) {
7821 items_buffer = detail::split_up(item.substr(1, item.length() - 2), aSep);
7822 } else if(defaultArray && item.find_first_of(aSep) != std::string::npos) {
7823 items_buffer = detail::split_up(item, aSep);
7824 } else if(defaultArray && item.find_first_of(' ') != std::string::npos) {
7825 items_buffer = detail::split_up(item);
7826 } else {
7827 items_buffer = {item};
7828 }
7829 } else {
7830 name = detail::trim_copy(line);
7831 items_buffer = {"true"};
7832 }
7833 if(name.find('.') == std::string::npos) {
7835 }
7836 // clean up quotes on the items
7837 for(auto &it : items_buffer) {
7839 }
7840
7841 std::vector<std::string> parents = detail::generate_parents(section, name);
7842
7843 if(!output.empty() && name == output.back().name && parents == output.back().parents) {
7844 output.back().inputs.insert(output.back().inputs.end(), items_buffer.begin(), items_buffer.end());
7845 } else {
7846 output.emplace_back();
7847 output.back().parents = std::move(parents);
7848 output.back().name = std::move(name);
7849 output.back().inputs = std::move(items_buffer);
7850 }
7851 }
7852 if(section != "default") {
7853 // insert a section end which is just an empty items_buffer
7854 std::string ename;
7855 output.emplace_back();
7856 output.back().parents = detail::generate_parents(section, ename);
7857 output.back().name = "--";
7858 while(output.back().parents.size() > 1) {
7859 output.push_back(output.back());
7860 output.back().parents.pop_back();
7861 }
7862 }
7863 return output;
7864}
std::string name
char valueDelimiter
the character used separate the name from the value
Definition CLI11.hpp:2026
std::string & remove_quotes(std::string &str)
remove quotes at the front and back of a string either '"' or '\''
Definition CLI11.hpp:279
std::string trim_copy(const std::string &str)
Make a copy of the string and then trim it.
Definition CLI11.hpp:273
std::string & trim(std::string &str)
Trim whitespace from string.
Definition CLI11.hpp:267
void checkParentSegments(std::vector< ConfigItem > &output, const std::string &currentSection)
assuming non default segments do a check on the close and open of the segments in a configItem struct...
Definition CLI11.hpp:7723
std::vector< std::string > generate_parents(const std::string &section, std::string &name)
Definition CLI11.hpp:7698
std::string to_lower(std::string str)
Return a lower case version of a string.
Definition CLI11.hpp:336
size_t len
Here is the call graph for this function:

◆ to_config()

std::string CLI::ConfigBase::to_config ( const App * ,
bool ,
bool ,
std::string  ) const
inlineoverridevirtual

Implements CLI::Config.

Definition at line 7867 of file CLI11.hpp.

7867 {
7868 std::stringstream out;
7869 std::string commentLead;
7870 commentLead.push_back(commentChar);
7871 commentLead.push_back(' ');
7872
7873 std::vector<std::string> groups = app->get_groups();
7874 bool defaultUsed = false;
7875 groups.insert(groups.begin(), std::string("Options"));
7876 if(write_description) {
7877 out << commentLead << app->get_description() << '\n';
7878 }
7879 for(auto &group : groups) {
7880 if(group == "Options" || group.empty()) {
7881 if(defaultUsed) {
7882 continue;
7883 }
7884 defaultUsed = true;
7885 }
7886 if(write_description && group != "Options" && !group.empty()) {
7887 out << '\n' << commentLead << group << " Options\n";
7888 }
7889 for(const Option *opt : app->get_options({})) {
7890
7891 // Only process option with a long-name and configurable
7892 if(!opt->get_lnames().empty() && opt->get_configurable()) {
7893 if(opt->get_group() != group) {
7894 if(!(group == "Options" && opt->get_group().empty())) {
7895 continue;
7896 }
7897 }
7898 std::string name = prefix + opt->get_lnames()[0];
7899 std::string value = detail::ini_join(opt->reduced_results(), arraySeparator, arrayStart, arrayEnd);
7900
7901 if(value.empty() && default_also) {
7902 if(!opt->get_default_str().empty()) {
7903 value = detail::convert_arg_for_ini(opt->get_default_str());
7904 } else if(opt->get_expected_min() == 0) {
7905 value = "false";
7906 }
7907 }
7908
7909 if(!value.empty()) {
7910 if(write_description && opt->has_description()) {
7911 out << '\n';
7912 out << commentLead << detail::fix_newlines(commentLead, opt->get_description()) << '\n';
7913 }
7914 out << name << valueDelimiter << value << '\n';
7915 }
7916 }
7917 }
7918 }
7919 auto subcommands = app->get_subcommands({});
7920 for(const App *subcom : subcommands) {
7921 if(subcom->get_name().empty()) {
7922 if(write_description && !subcom->get_group().empty()) {
7923 out << '\n' << commentLead << subcom->get_group() << " Options\n";
7924 }
7925 out << to_config(subcom, default_also, write_description, prefix);
7926 }
7927 }
7928
7929 for(const App *subcom : subcommands) {
7930 if(!subcom->get_name().empty()) {
7931 if(subcom->get_configurable() && app->got_subcommand(subcom)) {
7932 if(!prefix.empty() || app->get_parent() == nullptr) {
7933 out << '[' << prefix << subcom->get_name() << "]\n";
7934 } else {
7935 std::string subname = app->get_name() + "." + subcom->get_name();
7936 auto p = app->get_parent();
7937 while(p->get_parent() != nullptr) {
7938 subname = p->get_name() + "." + subname;
7939 p = p->get_parent();
7940 }
7941 out << '[' << subname << "]\n";
7942 }
7943 out << to_config(subcom, default_also, write_description, "");
7944 } else {
7945 out << to_config(subcom, default_also, write_description, prefix + subcom->get_name() + ".");
7946 }
7947 }
7948 }
7949
7950 return out.str();
7951}
const mie::Vuint & p
Definition bn.cpp:27
std::string to_config(const App *, bool default_also, bool write_description, std::string prefix) const override
Convert an app into a configuration.
Definition CLI11.hpp:7867
const bpo::variables_map & get_options() const
std::string ini_join(const std::vector< std::string > &args, char sepChar=',', char arrayStart='[', char arrayEnd=']')
Comma separated join, adds quotes if needed.
Definition CLI11.hpp:7677
std::string convert_arg_for_ini(const std::string &arg)
Definition CLI11.hpp:7631
application & app()
#define value
Definition pkcs11.h:157
Here is the call graph for this function:
Here is the caller graph for this function:

◆ valueSeparator()

ConfigBase * CLI::ConfigBase::valueSeparator ( char vSep)
inline

Definition at line 2050 of file CLI11.hpp.

2050 {
2051 valueDelimiter = vSep;
2052 return this;
2053 }

Member Data Documentation

◆ arrayEnd

char CLI::ConfigBase::arrayEnd = '\0'
protected

Definition at line 2022 of file CLI11.hpp.

◆ arraySeparator

char CLI::ConfigBase::arraySeparator = ' '
protected

Definition at line 2024 of file CLI11.hpp.

◆ arrayStart

char CLI::ConfigBase::arrayStart = '\0'
protected

Definition at line 2020 of file CLI11.hpp.

◆ commentChar

char CLI::ConfigBase::commentChar = ';'
protected

Definition at line 2018 of file CLI11.hpp.

◆ valueDelimiter

char CLI::ConfigBase::valueDelimiter = '='
protected

Definition at line 2026 of file CLI11.hpp.


The documentation for this class was generated from the following file: