You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: add some capabilities to the config parser and a stream parser (#630)
* add some capabilities to the config parser and a stream parser
* style: pre-commit.ci fixes
* add additional tests for the config parser
* additional tests of config sections and indexing
* style: pre-commit.ci fixes
* add initialization for member variables
* warning and error fixes
* add test for `parse_from_stream`
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Copy file name to clipboardExpand all lines: README.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -766,7 +766,7 @@ sub.subcommand = true
766
766
Spaces before and after the name and argument are ignored. Multiple arguments are separated by spaces. One set of quotes will be removed, preserving spaces (the same way the command line works). Boolean options can be `true`, `on`, `1`, `yes`, `enable`; or `false`, `off`, `0`, `no`, `disable` (case insensitive). Sections (and `.` separated names) are treated as subcommands (note: this does not necessarily mean that subcommand was passed, it just sets the "defaults"). You cannot set positional-only arguments. Subcommands can be triggered from configuration files if the `configurable` flag was set on the subcommand. Then the use of `[subcommand]` notation will trigger a subcommand and cause it to act as if it were on the command line.
767
767
768
768
To print a configuration file from the passed
769
-
arguments, use `.config_to_str(default_also=false, write_description=false)`, where `default_also` will also show any defaulted arguments, and `write_description` will include the app and option descriptions. See [Config files](https://cliutils.github.io/CLI11/book/chapters/config.html) for some additional details.
769
+
arguments, use `.config_to_str(default_also=false, write_description=false)`, where `default_also` will also show any defaulted arguments, and `write_description` will include the app and option descriptions. See [Config files](https://cliutils.github.io/CLI11/book/chapters/config.html) for some additional details and customization points.
770
770
771
771
If it is desired that multiple configuration be allowed. Use
will completely ignore any mismatches, extras, or other issues with the config file
47
+
42
48
### Getting the used configuration file name
43
49
44
50
If it is needed to get the configuration file name used this can be obtained via
@@ -118,7 +124,7 @@ if a prefix is needed to print before the options, for example to print a config
118
124
119
125
### Customization of configure file output
120
126
121
-
The default config parser/generator has some customization points that allow variations on the TOML format. The default formatter has a base configuration that matches the TOML format. It defines 5 characters that define how different aspects of the configuration are handled
127
+
The default config parser/generator has some customization points that allow variations on the TOML format. The default formatter has a base configuration that matches the TOML format. It defines 5 characters that define how different aspects of the configuration are handled. You must use `get_config_formatter_base()` to have access to these fields
122
128
123
129
```cpp
124
130
/// the character used for comments
@@ -131,6 +137,18 @@ char arrayEnd = ']';
131
137
char arraySeparator = ',';
132
138
/// the character used separate the name from the value
133
139
char valueDelimiter = '=';
140
+
/// the character to use around strings
141
+
char stringQuote = '"';
142
+
/// the character to use around single characters
143
+
char characterQuote = '\'';
144
+
/// the maximum number of layers to allow
145
+
uint8_t maximumLayers{255};
146
+
/// the separator used to separator parent layers
147
+
char parentSeparatorChar{'.'};
148
+
/// Specify the configuration index to use for arrayed sections
149
+
uint16_t configIndex{0};
150
+
/// Specify the configuration section that should be used
151
+
std::string configSection;
134
152
```
135
153
136
154
These can be modified via setter functions
@@ -139,6 +157,11 @@ These can be modified via setter functions
139
157
*`ConfigBase *arrayBounds(char aStart, char aEnd)`: Specify the start and end characters for an array
140
158
*`ConfigBase *arrayDelimiter(char aSep)`: Specify the delimiter character for an array
141
159
*`ConfigBase *valueSeparator(char vSep)`: Specify the delimiter between a name and value
160
+
*`ConfigBase *quoteCharacter(char qString, char qChar)`:specify the characters to use around strings and single characters
161
+
*`ConfigBase *maxLayers(uint8_t layers)` : specify the maximum number of parent layers to process. This is useful to limit processing for larger config files
162
+
*`ConfigBase *parentSeparator(char sep)` : specify the character to separate parent layers from options
163
+
*`ConfigBase *section(const std::string §ionName)` : specify the section name to use to get the option values, only this section will be processed
164
+
*`ConfigBase *index(uint16_t sectionIndex)` : specify an index section to use for processing if multiple TOML sections of the same name are present `[[section]]`
142
165
143
166
For example, to specify reading a configure file that used `:` to separate name and values:
The open and close brackets must be on a separate line and the comma gets interpreted as an array separator but since no values are after the comma they get ignored as well. This will not support multiple layers or sections or any other moderately complex JSON, but can work if the input file is simple.
217
+
177
218
## Triggering Subcommands
178
219
179
220
Configuration files can be used to trigger subcommands if a subcommand is set to configure. By default configuration file just set the default values of a subcommand. But if the `configure()` option is set on a subcommand then the if the subcommand is utilized via a `[subname]` block in the configuration file it will act as if it were called from the command line. Subsubcommands can be triggered via `[subname.subsubname]`. Using the `[[subname]]` will be as if the subcommand were triggered multiple times from the command line. This functionality can allow the configuration file to act as a scripting file.
180
221
181
222
For custom configuration files this behavior can be triggered by specifying the parent subcommands in the structure and `++` as the name to open a new subcommand scope and `--` to close it. These names trigger the different callbacks of configurable subcommands.
182
223
224
+
## Stream parsing
225
+
226
+
In addition to the regular parse functions a `parse_from_stream(std::istream &input)` is available to directly parse a stream operator. For example to process some arguments in an already open file stream. The stream is fed directly in the config parser so bypasses the normal command line parsing.
227
+
183
228
## Implementation Notes
184
229
185
-
The config file input works with any form of the option given: Long, short, positional, or the environment variable name. When generating a config file it will create a name in following priority.
230
+
The config file input works with any form of the option given: Long, short, positional, or the environment variable name. When generating a config file it will create an option name in following priority.
0 commit comments