CLI11
C++11 Command Line Interface Parser
Loading...
Searching...
No Matches
ConfigFwd.hpp
1// Copyright (c) 2017-2024, 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// IWYU pragma: private, include "CLI/CLI.hpp"
10
11// [CLI11:public_includes:set]
12#include <algorithm>
13#include <fstream>
14#include <iostream>
15#include <memory>
16#include <string>
17#include <vector>
18// [CLI11:public_includes:end]
19
20#include "Error.hpp"
21#include "StringTools.hpp"
22
23namespace CLI {
24// [CLI11:config_fwd_hpp:verbatim]
25
26class App;
27
29struct ConfigItem {
31 std::vector<std::string> parents{};
32
34 std::string name{};
36 std::vector<std::string> inputs{};
38 bool multiline{false};
40 CLI11_NODISCARD std::string fullname() const {
41 std::vector<std::string> tmp = parents;
42 tmp.emplace_back(name);
43 return detail::join(tmp, ".");
44 (void)multiline; // suppression for cppcheck false positive
45 }
46};
47
49class Config {
50 protected:
51 std::vector<ConfigItem> items{};
52
53 public:
55 virtual std::string to_config(const App *, bool, bool, std::string) const = 0;
56
58 virtual std::vector<ConfigItem> from_config(std::istream &) const = 0;
59
61 CLI11_NODISCARD virtual std::string to_flag(const ConfigItem &item) const {
62 if(item.inputs.size() == 1) {
63 return item.inputs.at(0);
64 }
65 if(item.inputs.empty()) {
66 return "{}";
67 }
68 throw ConversionError::TooManyInputsFlag(item.fullname()); // LCOV_EXCL_LINE
69 }
70
72 CLI11_NODISCARD std::vector<ConfigItem> from_file(const std::string &name) const {
73 std::ifstream input{name};
74 if(!input.good())
75 throw FileError::Missing(name);
76
77 return from_config(input);
78 }
79
81 virtual ~Config() = default;
82};
83
85class ConfigBase : public Config {
86 protected:
88 char commentChar = '#';
90 char arrayStart = '[';
92 char arrayEnd = ']';
94 char arraySeparator = ',';
96 char valueDelimiter = '=';
98 char stringQuote = '"';
100 char literalQuote = '\'';
102 uint8_t maximumLayers{255};
110 int16_t configIndex{-1};
112 std::string configSection{};
113
114 public:
115 std::string
116 to_config(const App * /*app*/, bool default_also, bool write_description, std::string prefix) const override;
117
118 std::vector<ConfigItem> from_config(std::istream &input) const override;
120 ConfigBase *comment(char cchar) {
121 commentChar = cchar;
122 return this;
123 }
125 ConfigBase *arrayBounds(char aStart, char aEnd) {
126 arrayStart = aStart;
127 arrayEnd = aEnd;
128 return this;
129 }
132 arraySeparator = aSep;
133 return this;
134 }
137 valueDelimiter = vSep;
138 return this;
139 }
141 ConfigBase *quoteCharacter(char qString, char literalChar) {
142 stringQuote = qString;
143 literalQuote = literalChar;
144 return this;
145 }
147 ConfigBase *maxLayers(uint8_t layers) {
148 maximumLayers = layers;
149 return this;
150 }
154 return this;
155 }
157 ConfigBase *commentDefaults(bool comDef = true) {
158 commentDefaultsBool = comDef;
159 return this;
160 }
162 std::string &sectionRef() { return configSection; }
164 CLI11_NODISCARD const std::string &section() const { return configSection; }
166 ConfigBase *section(const std::string &sectionName) {
167 configSection = sectionName;
168 return this;
169 }
170
172 int16_t &indexRef() { return configIndex; }
174 CLI11_NODISCARD int16_t index() const { return configIndex; }
176 ConfigBase *index(int16_t sectionIndex) {
177 configIndex = sectionIndex;
178 return this;
179 }
181 ConfigBase *allowDuplicateFields(bool value = true) {
183 return this;
184 }
185};
186
188using ConfigTOML = ConfigBase;
189
191class ConfigINI : public ConfigTOML {
192
193 public:
194 ConfigINI() {
195 commentChar = ';';
196 arrayStart = '\0';
197 arrayEnd = '\0';
198 arraySeparator = ' ';
199 valueDelimiter = '=';
200 }
201};
202// [CLI11:config_fwd_hpp:end]
203} // namespace CLI
Creates a command line program, with very few defaults.
Definition App.hpp:90
This converter works with INI/TOML files; to write INI files use ConfigINI.
Definition ConfigFwd.hpp:85
std::vector< ConfigItem > from_config(std::istream &input) const override
Convert a configuration into an app.
std::string configSection
Specify the configuration section that should be used.
Definition ConfigFwd.hpp:112
ConfigBase * comment(char cchar)
Specify the configuration for comment characters.
Definition ConfigFwd.hpp:120
ConfigBase * arrayDelimiter(char aSep)
Specify the delimiter character for an array.
Definition ConfigFwd.hpp:131
ConfigBase * quoteCharacter(char qString, char literalChar)
Specify the quote characters used around strings and literal strings.
Definition ConfigFwd.hpp:141
char arraySeparator
the character used to separate elements in an array
Definition ConfigFwd.hpp:94
std::string & sectionRef()
get a reference to the configuration section
Definition ConfigFwd.hpp:162
ConfigBase * arrayBounds(char aStart, char aEnd)
Specify the start and end characters for an array.
Definition ConfigFwd.hpp:125
std::string to_config(const App *, bool default_also, bool write_description, std::string prefix) const override
Convert an app into a configuration.
ConfigBase * maxLayers(uint8_t layers)
Specify the maximum number of parents.
Definition ConfigFwd.hpp:147
ConfigBase * allowDuplicateFields(bool value=true)
specify that multiple duplicate arguments should be merged even if not sequential
Definition ConfigFwd.hpp:181
CLI11_NODISCARD const std::string & section() const
get the section
Definition ConfigFwd.hpp:164
ConfigBase * commentDefaults(bool comDef=true)
comment default value options
Definition ConfigFwd.hpp:157
char stringQuote
the character to use around strings
Definition ConfigFwd.hpp:98
ConfigBase * section(const std::string &sectionName)
specify a particular section of the configuration file to use
Definition ConfigFwd.hpp:166
ConfigBase * valueSeparator(char vSep)
Specify the delimiter between a name and value.
Definition ConfigFwd.hpp:136
uint8_t maximumLayers
the maximum number of layers to allow
Definition ConfigFwd.hpp:102
char valueDelimiter
the character used separate the name from the value
Definition ConfigFwd.hpp:96
char arrayStart
the character used to start an array '\0' is a default to not use
Definition ConfigFwd.hpp:90
char parentSeparatorChar
the separator used to separator parent layers
Definition ConfigFwd.hpp:104
bool allowMultipleDuplicateFields
specify the config reader should collapse repeated field names to a single vector
Definition ConfigFwd.hpp:108
CLI11_NODISCARD int16_t index() const
get the section index
Definition ConfigFwd.hpp:174
char literalQuote
the character to use around single characters and literal strings
Definition ConfigFwd.hpp:100
ConfigBase * index(int16_t sectionIndex)
specify a particular index in the section to use (-1) for all sections to use
Definition ConfigFwd.hpp:176
int16_t & indexRef()
get a reference to the configuration index
Definition ConfigFwd.hpp:172
char arrayEnd
the character used to end an array '\0' is a default to not use
Definition ConfigFwd.hpp:92
ConfigBase * parentSeparator(char sep)
Specify the separator to use for parent layers.
Definition ConfigFwd.hpp:152
bool commentDefaultsBool
comment default values
Definition ConfigFwd.hpp:106
int16_t configIndex
Specify the configuration index to use for arrayed sections.
Definition ConfigFwd.hpp:110
char commentChar
the character used for comments
Definition ConfigFwd.hpp:88
This class provides a converter for configuration files.
Definition ConfigFwd.hpp:49
virtual CLI11_NODISCARD std::string to_flag(const ConfigItem &item) const
Get a flag value.
Definition ConfigFwd.hpp:61
virtual std::string to_config(const App *, bool, bool, std::string) const =0
Convert an app into a configuration.
CLI11_NODISCARD std::vector< ConfigItem > from_file(const std::string &name) const
Parse a config file, throw an error (ParseError:ConfigParseError or FileError) on failure.
Definition ConfigFwd.hpp:72
virtual ~Config()=default
Virtual destructor.
virtual std::vector< ConfigItem > from_config(std::istream &) const =0
Convert a configuration into an app.
ConfigINI generates a "standard" INI compliant output.
Definition ConfigFwd.hpp:191
Holds values to load into Options.
Definition ConfigFwd.hpp:29
std::vector< std::string > inputs
Listing of inputs.
Definition ConfigFwd.hpp:36
std::string name
This is the name.
Definition ConfigFwd.hpp:34
CLI11_NODISCARD std::string fullname() const
The list of parents and name joined by ".".
Definition ConfigFwd.hpp:40
bool multiline
indicator if a multiline vector separator was inserted
Definition ConfigFwd.hpp:38
std::vector< std::string > parents
This is the list of parents.
Definition ConfigFwd.hpp:31