CLI11  2.1.2
ConfigFwd.hpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2021, University of Cincinnati, developed by Henry Schreiner
2 // under NSF AWARD 1414736 and by the respective contributors.
3 // All rights reserved.
4 //
5 // SPDX-License-Identifier: BSD-3-Clause
6 
7 #pragma once
8 
9 // [CLI11:public_includes:set]
10 #include <algorithm>
11 #include <fstream>
12 #include <iostream>
13 #include <string>
14 #include <vector>
15 // [CLI11:public_includes:end]
16 
17 #include "Error.hpp"
18 #include "StringTools.hpp"
19 
20 namespace CLI {
21 // [CLI11:config_fwd_hpp:verbatim]
22 
23 class App;
24 
26 struct ConfigItem {
28  std::vector<std::string> parents{};
29 
31  std::string name{};
32 
34  std::vector<std::string> inputs{};
35 
37  std::string fullname() const {
38  std::vector<std::string> tmp = parents;
39  tmp.emplace_back(name);
40  return detail::join(tmp, ".");
41  }
42 };
43 
45 class Config {
46  protected:
47  std::vector<ConfigItem> items{};
48 
49  public:
51  virtual std::string to_config(const App *, bool, bool, std::string) const = 0;
52 
54  virtual std::vector<ConfigItem> from_config(std::istream &) const = 0;
55 
57  virtual std::string to_flag(const ConfigItem &item) const {
58  if(item.inputs.size() == 1) {
59  return item.inputs.at(0);
60  }
61  throw ConversionError::TooManyInputsFlag(item.fullname());
62  }
63 
65  std::vector<ConfigItem> from_file(const std::string &name) {
66  std::ifstream input{name};
67  if(!input.good())
68  throw FileError::Missing(name);
69 
70  return from_config(input);
71  }
72 
74  virtual ~Config() = default;
75 };
76 
78 class ConfigBase : public Config {
79  protected:
81  char commentChar = '#';
83  char arrayStart = '[';
85  char arrayEnd = ']';
87  char arraySeparator = ',';
89  char valueDelimiter = '=';
91  char stringQuote = '"';
93  char characterQuote = '\'';
95  uint8_t maximumLayers{255};
99  int16_t configIndex{-1};
101  std::string configSection{};
102 
103  public:
104  std::string
105  to_config(const App * /*app*/, bool default_also, bool write_description, std::string prefix) const override;
106 
107  std::vector<ConfigItem> from_config(std::istream &input) const override;
109  ConfigBase *comment(char cchar) {
110  commentChar = cchar;
111  return this;
112  }
114  ConfigBase *arrayBounds(char aStart, char aEnd) {
115  arrayStart = aStart;
116  arrayEnd = aEnd;
117  return this;
118  }
121  arraySeparator = aSep;
122  return this;
123  }
126  valueDelimiter = vSep;
127  return this;
128  }
130  ConfigBase *quoteCharacter(char qString, char qChar) {
131  stringQuote = qString;
132  characterQuote = qChar;
133  return this;
134  }
136  ConfigBase *maxLayers(uint8_t layers) {
137  maximumLayers = layers;
138  return this;
139  }
142  parentSeparatorChar = sep;
143  return this;
144  }
146  std::string &sectionRef() { return configSection; }
148  const std::string &section() const { return configSection; }
150  ConfigBase *section(const std::string &sectionName) {
151  configSection = sectionName;
152  return this;
153  }
154 
156  int16_t &indexRef() { return configIndex; }
158  int16_t index() const { return configIndex; }
160  ConfigBase *index(int16_t sectionIndex) {
161  configIndex = sectionIndex;
162  return this;
163  }
164 };
165 
168 
170 class ConfigINI : public ConfigTOML {
171 
172  public:
174  commentChar = ';';
175  arrayStart = '\0';
176  arrayEnd = '\0';
177  arraySeparator = ' ';
178  valueDelimiter = '=';
179  }
180 };
181 // [CLI11:config_fwd_hpp:end]
182 } // namespace CLI
Creates a command line program, with very few defaults.
Definition: App.hpp:69
This converter works with INI/TOML files; to write INI files use ConfigINI.
Definition: ConfigFwd.hpp:78
std::vector< ConfigItem > from_config(std::istream &input) const override
Convert a configuration into an app.
Definition: Config.hpp:172
std::string configSection
Specify the configuration section that should be used.
Definition: ConfigFwd.hpp:101
ConfigBase * quoteCharacter(char qString, char qChar)
Specify the quote characters used around strings and characters.
Definition: ConfigFwd.hpp:130
char arraySeparator
the character used to separate elements in an array
Definition: ConfigFwd.hpp:87
ConfigBase * section(const std::string &sectionName)
specify a particular section of the configuration file to use
Definition: ConfigFwd.hpp:150
ConfigBase * arrayBounds(char aStart, char aEnd)
Specify the start and end characters for an array.
Definition: ConfigFwd.hpp:114
ConfigBase * comment(char cchar)
Specify the configuration for comment characters.
Definition: ConfigFwd.hpp:109
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:305
int16_t index() const
get the section index
Definition: ConfigFwd.hpp:158
std::string & sectionRef()
get a reference to the configuration section
Definition: ConfigFwd.hpp:146
ConfigBase * valueSeparator(char vSep)
Specify the delimiter between a name and value.
Definition: ConfigFwd.hpp:125
ConfigBase * index(int16_t sectionIndex)
specify a particular index in the section to use (-1) for all sections to use
Definition: ConfigFwd.hpp:160
char characterQuote
the character to use around single characters
Definition: ConfigFwd.hpp:93
const std::string & section() const
get the section
Definition: ConfigFwd.hpp:148
ConfigBase * parentSeparator(char sep)
Specify the separator to use for parent layers.
Definition: ConfigFwd.hpp:141
int16_t & indexRef()
get a reference to the configuration index
Definition: ConfigFwd.hpp:156
char stringQuote
the character to use around strings
Definition: ConfigFwd.hpp:91
uint8_t maximumLayers
the maximum number of layers to allow
Definition: ConfigFwd.hpp:95
char valueDelimiter
the character used separate the name from the value
Definition: ConfigFwd.hpp:89
char arrayStart
the character used to start an array '\0' is a default to not use
Definition: ConfigFwd.hpp:83
ConfigBase * maxLayers(uint8_t layers)
Specify the maximum number of parents.
Definition: ConfigFwd.hpp:136
char parentSeparatorChar
the separator used to separator parent layers
Definition: ConfigFwd.hpp:97
ConfigBase * arrayDelimiter(char aSep)
Specify the delimiter character for an array.
Definition: ConfigFwd.hpp:120
char arrayEnd
the character used to end an array '\0' is a default to not use
Definition: ConfigFwd.hpp:85
int16_t configIndex
Specify the configuration index to use for arrayed sections.
Definition: ConfigFwd.hpp:99
char commentChar
the character used for comments
Definition: ConfigFwd.hpp:81
ConfigINI generates a "standard" INI compliant output.
Definition: ConfigFwd.hpp:170
ConfigINI()
Definition: ConfigFwd.hpp:173
This class provides a converter for configuration files.
Definition: ConfigFwd.hpp:45
std::vector< ConfigItem > items
Definition: ConfigFwd.hpp:47
virtual std::string to_config(const App *, bool, bool, std::string) const =0
Convert an app into a configuration.
virtual std::vector< ConfigItem > from_config(std::istream &) const =0
Convert a configuration into an app.
virtual ~Config()=default
Virtual destructor.
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:65
virtual std::string to_flag(const ConfigItem &item) const
Get a flag value.
Definition: ConfigFwd.hpp:57
std::string join(const T &v, std::string delim=",")
Simple function to join a string.
Definition: StringTools.hpp:63
Definition: App.hpp:34
Holds values to load into Options.
Definition: ConfigFwd.hpp:26
std::vector< std::string > inputs
Listing of inputs.
Definition: ConfigFwd.hpp:34
std::string name
This is the name.
Definition: ConfigFwd.hpp:31
std::vector< std::string > parents
This is the list of parents.
Definition: ConfigFwd.hpp:28
std::string fullname() const
The list of parents and name joined by ".".
Definition: ConfigFwd.hpp:37