CLI11  1.9.0
ConfigFwd.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 // Distributed under the 3-Clause BSD License. See accompanying
4 // file LICENSE or https://github.com/CLIUtils/CLI11 for details.
5 
6 #include <algorithm>
7 #include <fstream>
8 #include <iostream>
9 #include <string>
10 
11 #include "CLI/Error.hpp"
12 #include "CLI/StringTools.hpp"
13 
14 namespace CLI {
15 
16 class App;
17 
19 struct ConfigItem {
21  std::vector<std::string> parents{};
22 
24  std::string name{};
25 
27  std::vector<std::string> inputs{};
28 
30  std::string fullname() const {
31  std::vector<std::string> tmp = parents;
32  tmp.emplace_back(name);
33  return detail::join(tmp, ".");
34  }
35 };
36 
38 class Config {
39  protected:
40  std::vector<ConfigItem> items{};
41 
42  public:
44  virtual std::string to_config(const App *, bool, bool, std::string) const = 0;
45 
47  virtual std::vector<ConfigItem> from_config(std::istream &) const = 0;
48 
50  virtual std::string to_flag(const ConfigItem &item) const {
51  if(item.inputs.size() == 1) {
52  return item.inputs.at(0);
53  }
54  throw ConversionError::TooManyInputsFlag(item.fullname());
55  }
56 
58  std::vector<ConfigItem> from_file(const std::string &name) {
59  std::ifstream input{name};
60  if(!input.good())
61  throw FileError::Missing(name);
62 
63  return from_config(input);
64  }
65 
67  virtual ~Config() = default;
68 };
69 
71 class ConfigBase : public Config {
72  protected:
74  char commentChar = ';';
76  char arrayStart = '\0';
78  char arrayEnd = '\0';
80  char arraySeparator = ' ';
82  char valueDelimiter = '=';
83 
84  public:
85  std::string
86  to_config(const App * /*app*/, bool default_also, bool write_description, std::string prefix) const override;
87 
88  std::vector<ConfigItem> from_config(std::istream &input) const override;
90  ConfigBase *comment(char cchar) {
91  commentChar = cchar;
92  return this;
93  }
95  ConfigBase *arrayBounds(char aStart, char aEnd) {
96  arrayStart = aStart;
97  arrayEnd = aEnd;
98  return this;
99  }
102  arraySeparator = aSep;
103  return this;
104  }
107  valueDelimiter = vSep;
108  return this;
109  }
110 };
111 
114 
116 class ConfigTOML : public ConfigINI {
117 
118  public:
120  commentChar = '#';
121  arrayStart = '[';
122  arrayEnd = ']';
123  arraySeparator = ',';
124  valueDelimiter = '=';
125  }
126 };
127 } // namespace CLI
CLI::ConfigBase::arrayEnd
char arrayEnd
the character used to end an array '\0' is a default to not use
Definition: ConfigFwd.hpp:78
CLI::ConfigItem::name
std::string name
This is the name.
Definition: ConfigFwd.hpp:24
CLI::Config::to_flag
virtual std::string to_flag(const ConfigItem &item) const
Get a flag value.
Definition: ConfigFwd.hpp:50
Error.hpp
CLI::ConfigBase::comment
ConfigBase * comment(char cchar)
Specify the configuration for comment characters.
Definition: ConfigFwd.hpp:90
CLI::detail::join
std::string join(const T &v, std::string delim=",")
Simple function to join a string.
Definition: StringTools.hpp:56
CLI::ConfigItem::fullname
std::string fullname() const
The list of parents and name joined by ".".
Definition: ConfigFwd.hpp:30
CLI::Config::~Config
virtual ~Config()=default
Virtual destructor.
CLI::ConfigBase::arrayDelimiter
ConfigBase * arrayDelimiter(char aSep)
Specify the delimiter character for an array.
Definition: ConfigFwd.hpp:101
CLI::ConfigBase
This converter works with INI/TOML files; to write proper TOML files use ConfigTOML.
Definition: ConfigFwd.hpp:71
CLI::ConfigBase::arrayStart
char arrayStart
the character used to start an array '\0' is a default to not use
Definition: ConfigFwd.hpp:76
CLI
Definition: App.hpp:27
CLI::ConfigBase::arrayBounds
ConfigBase * arrayBounds(char aStart, char aEnd)
Specify the start and end characters for an array.
Definition: ConfigFwd.hpp:95
CLI::ConfigItem::inputs
std::vector< std::string > inputs
Listing of inputs.
Definition: ConfigFwd.hpp:27
CLI::ConfigBase::valueDelimiter
char valueDelimiter
the character used separate the name from the value
Definition: ConfigFwd.hpp:82
CLI::ConfigBase::to_config
std::string to_config(const App *, bool default_also, bool write_description, std::string prefix) const override
Convert an app into a configuration.
Definition: Config.hpp:255
CLI::ConfigBase::commentChar
char commentChar
the character used for comments
Definition: ConfigFwd.hpp:74
CLI::Config::from_file
std::vector< ConfigItem > from_file(const std::string &name)
Parse a config file, throw an error (ParseError:ConfigParseError or FileError) on failure.
Definition: ConfigFwd.hpp:58
StringTools.hpp
CLI::App
Creates a command line program, with very few defaults.
Definition: App.hpp:61
CLI::Config::to_config
virtual std::string to_config(const App *, bool, bool, std::string) const =0
Convert an app into a configuration.
CLI::ConfigBase::arraySeparator
char arraySeparator
the character used to separate elements in an array
Definition: ConfigFwd.hpp:80
CLI::Config
This class provides a converter for configuration files.
Definition: ConfigFwd.hpp:38
CLI::ConfigBase::valueSeparator
ConfigBase * valueSeparator(char vSep)
Specify the delimiter between a name and value.
Definition: ConfigFwd.hpp:106
CLI::ConfigTOML
ConfigTOML generates a TOML compliant output.
Definition: ConfigFwd.hpp:116
CLI::Config::from_config
virtual std::vector< ConfigItem > from_config(std::istream &) const =0
Convert a configuration into an app.
CLI::Config::items
std::vector< ConfigItem > items
Definition: ConfigFwd.hpp:40
CLI::ConfigTOML::ConfigTOML
ConfigTOML()
Definition: ConfigFwd.hpp:119
CLI::ConfigBase::from_config
std::vector< ConfigItem > from_config(std::istream &input) const override
Convert a configuration into an app.
Definition: Config.hpp:160
CLI::ConfigItem
Holds values to load into Options.
Definition: ConfigFwd.hpp:19
CLI::ConfigItem::parents
std::vector< std::string > parents
This is the list of parents.
Definition: ConfigFwd.hpp:21