Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set the default configuration file output as TOML #435

Merged
merged 2 commits into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -679,39 +679,39 @@ app.set_config(option_name="",
required=false)
```

If this is called with no arguments, it will remove the configuration file option (like `set_help_flag`). Setting a configuration option is special. If it is present, it will be read along with the normal command line arguments. The file will be read if it exists, and does not throw an error unless `required` is `true`. Configuration files are in `ini` format by default, The reader can also accept many files in [TOML] format 🆕. (other formats can be added by an adept user, some variations are available through customization points in the default formatter). An example of a file:
If this is called with no arguments, it will remove the configuration file option (like `set_help_flag`). Setting a configuration option is special. If it is present, it will be read along with the normal command line arguments. The file will be read if it exists, and does not throw an error unless `required` is `true`. Configuration files are in [TOML] format by default 🚧, though the default reader can also accept files in INI format as well 🆕. It should be noted that CLI11 does not contain a full TOML parser but can read strings from most TOML file and run them through the CLI11 parser. Other formats can be added by an adept user, some variations are available through customization points in the default formatter. An example of a TOML file 🆕:

```ini
; Comments are supported, using a ;
; The default section is [default], case insensitive
# Comments are supported, using a #
# The default section is [default], case insensitive

value = 1
str = "A string"
vector = 1 2 3
str_vector = "one" "two" "and three"
vector = [1,2,3]
str_vector = ["one","two","and three"]

; Sections map to subcommands
# Sections map to subcommands
[subcommand]
in_subcommand = Wow
sub.subcommand = true
```
or equivalently in TOML 🆕
```toml
# Comments are supported, using a #
# The default section is [default], case insensitive
or equivalently in INI format
```ini
; Comments are supported, using a ;
; The default section is [default], case insensitive

value = 1
str = "A string"
vector = [1,2,3]
str_vector = ["one","two","and three"]
vector = 1 2 3
str_vector = "one" "two" "and three"

# Sections map to subcommands
; Sections map to subcommands
[subcommand]
in_subcommand = Wow
sub.subcommand = true
```

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 config files if the `configurable` flag was set on the subcommand. Then use `[subcommand]` notation will trigger a subcommand and cause it to act as if it were on the command line.
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.

To print a configuration file from the passed
arguments, use `.config_to_str(default_also=false, prefix="", write_description=false)`, where `default_also` will also show any defaulted arguments, `prefix` will add a prefix, and `write_description` will include option descriptions. See [Config files](https://cliutils.github.io/CLI11/book/chapters/config.html) for some additional details.
Expand Down Expand Up @@ -744,7 +744,7 @@ The App class was designed allow toolkits to subclass it, to provide preset defa
but before run behavior, while
still giving the user freedom to `callback` on the main app.

The most important parse function is `parse(std::vector<std::string>)`, which takes a reversed list of arguments (so that `pop_back` processes the args in the correct order). `get_help_ptr` and `get_config_ptr` give you access to the help/config option pointers. The standard `parse` manually sets the name from the first argument, so it should not be in this vector. You can also use `parse(string, bool)` to split up and parse a string; the optional bool should be set to true if you are
The most important parse function is `parse(std::vector<std::string>)`, which takes a reversed list of arguments (so that `pop_back` processes the args in the correct order). `get_help_ptr` and `get_config_ptr` give you access to the help/config option pointers. The standard `parse` manually sets the name from the first argument, so it should not be in this vector. You can also use `parse(string, bool)` to split up and parse a string; the optional boolean should be set to true if you are
including the program name in the string, and false otherwise.

Also, in a related note, the `App` you get a pointer to is stored in the parent `App` in a `shared_ptr`s (similar to `Option`s) and are deleted when the main `App` goes out of scope unless the object has another owner.
Expand Down
44 changes: 22 additions & 22 deletions book/chapters/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,39 +41,39 @@ If it is needed to get the configuration file name used this can be obtained via

## Configure file format

Here is an example configuration file, in INI format:
Here is an example configuration file, in [TOML](https://github.com/toml-lang/toml) format:

```ini
; Comments are supported, using a ;
; The default section is [default], case insensitive
# Comments are supported, using a #
# The default section is [default], case insensitive

value = 1
str = "A string"
vector = 1 2 3
vector = [1,2,3]

; Section map to subcommands
# Section map to subcommands
[subcommand]
in_subcommand = Wow
sub.subcommand = true
[subcommand.sub]
subcommand = true # could also be give as sub.subcommand=true
```

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`, `y`, `t`, `+`, `yes`, `enable`; or `false`, `off`, `0`, `no`, `n`, `f`, `-`, `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". If a subcommand is set to `configurable` then passing the subcommand using `[sub]` in a configuration file will trigger the subcommand.)

CLI11 also supports configuration file in [TOML](https://github.com/toml-lang/toml) format.
CLI11 also supports configuration file in INI format.

```toml
# Comments are supported, using a #
# The default section is [default], case insensitive
```ini
; Comments are supported, using a ;
; The default section is [default], case insensitive

value = 1
str = "A string"
vector = [1,2,3]
vector = 1 2 3

# Section map to subcommands
; Section map to subcommands
[subcommand]
in_subcommand = Wow
[subcommand.sub]
subcommand = true # could also be give as sub.subcommand=true
sub.subcommand = true
```

The main differences are in vector notation and comment character. Note: CLI11 is not a full TOML parser as it just reads values as strings. It is possible (but not recommended) to mix notation.
Expand All @@ -83,16 +83,16 @@ The main differences are in vector notation and comment character. Note: CLI11
To print a configuration file from the passed arguments, use `.config_to_str(default_also=false, prefix="", write_description=false)`, where `default_also` will also show any defaulted arguments, `prefix` will add a prefix, and `write_description` will include option descriptions.

### Customization of configure file output
The default config parser/generator has some customization points that allow variations on the INI format. The default formatter has a base configuration that matches the INI format. It defines 5 characters that define how different aspects of the configuration are handled
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
```cpp
/// the character used for comments
char commentChar = ';';
char commentChar = '#';
/// the character used to start an array '\0' is a default to not use
char arrayStart = '\0';
char arrayStart = '[';
/// the character used to end an array '\0' is a default to not use
char arrayEnd = '\0';
char arrayEnd = ']';
/// the character used to separate elements in an array
char arraySeparator = ' ';
char arraySeparator = ',';
/// the character used separate the name from the value
char valueDelimiter = '=';
```
Expand All @@ -111,11 +111,11 @@ auto config_base=app.get_config_formatter_base();
config_base->valueSeparator(':');
```

The default configuration file will read TOML files, but will write out files in the INI format. To specify outputting TOML formatted files use
The default configuration file will read INI files, but will write out files in the TOML format. To specify outputting INI formatted files use
```cpp
app.config_formatter(std::make_shared<CLI::ConfigTOML>());
app.config_formatter(std::make_shared<CLI::ConfigINI>());
```
which makes use of a predefined modification of the ConfigBase class which INI also uses.
which makes use of a predefined modification of the ConfigBase class which TOML also uses.

## Custom formats

Expand Down
2 changes: 1 addition & 1 deletion include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class App {
Option *config_ptr_{nullptr};

/// This is the formatter for help printing. Default provided. INHERITABLE (same pointer)
std::shared_ptr<Config> config_formatter_{new ConfigINI()};
std::shared_ptr<Config> config_formatter_{new ConfigTOML()};

///@}

Expand Down
13 changes: 7 additions & 6 deletions include/CLI/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,11 @@ inline std::vector<ConfigItem> ConfigBase::from_config(std::istream &input) cons
std::string section = "default";

std::vector<ConfigItem> output;
bool defaultArray = (arrayStart == '\0' || arrayStart == ' ') && arrayStart == arrayEnd;
char aStart = (defaultArray) ? '[' : arrayStart;
char aEnd = (defaultArray) ? ']' : arrayEnd;
char aSep = (defaultArray && arraySeparator == ' ') ? ',' : arraySeparator;
bool isDefaultArray = (arrayStart == '[' && arrayEnd == ']' && arraySeparator == ',');
bool isINIArray = (arrayStart == '\0' || arrayStart == ' ') && arrayStart == arrayEnd;
char aStart = (isINIArray) ? '[' : arrayStart;
char aEnd = (isINIArray) ? ']' : arrayEnd;
char aSep = (isINIArray && arraySeparator == ' ') ? ',' : arraySeparator;

while(getline(input, line)) {
std::vector<std::string> items_buffer;
Expand Down Expand Up @@ -212,9 +213,9 @@ inline std::vector<ConfigItem> ConfigBase::from_config(std::istream &input) cons
std::string item = detail::trim_copy(line.substr(pos + 1));
if(item.size() > 1 && item.front() == aStart && item.back() == aEnd) {
items_buffer = detail::split_up(item.substr(1, item.length() - 2), aSep);
} else if(defaultArray && item.find_first_of(aSep) != std::string::npos) {
} else if((isDefaultArray || isINIArray) && item.find_first_of(aSep) != std::string::npos) {
items_buffer = detail::split_up(item, aSep);
} else if(defaultArray && item.find_first_of(' ') != std::string::npos) {
} else if((isDefaultArray || isINIArray) && item.find_first_of(' ') != std::string::npos) {
items_buffer = detail::split_up(item);
} else {
items_buffer = {item};
Expand Down
28 changes: 14 additions & 14 deletions include/CLI/ConfigFwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@ class Config {
virtual ~Config() = default;
};

/// This converter works with INI/TOML files; to write proper TOML files use ConfigTOML
/// This converter works with INI/TOML files; to write INI files use ConfigINI
class ConfigBase : public Config {
protected:
/// the character used for comments
char commentChar = ';';
char commentChar = '#';
/// the character used to start an array '\0' is a default to not use
char arrayStart = '\0';
char arrayStart = '[';
/// the character used to end an array '\0' is a default to not use
char arrayEnd = '\0';
char arrayEnd = ']';
/// the character used to separate elements in an array
char arraySeparator = ' ';
char arraySeparator = ',';
/// the character used separate the name from the value
char valueDelimiter = '=';

Expand Down Expand Up @@ -113,18 +113,18 @@ class ConfigBase : public Config {
}
};

/// the default Config is the INI file format
using ConfigINI = ConfigBase;
/// the default Config is the TOML file format
using ConfigTOML = ConfigBase;

/// ConfigTOML generates a TOML compliant output
class ConfigTOML : public ConfigINI {
/// ConfigINI generates a "standard" INI compliant output
class ConfigINI : public ConfigTOML {

public:
ConfigTOML() {
commentChar = '#';
arrayStart = '[';
arrayEnd = ']';
arraySeparator = ',';
ConfigINI() {
commentChar = ';';
arrayStart = '\0';
arrayEnd = '\0';
arraySeparator = ' ';
valueDelimiter = '=';
}
};
Expand Down
Loading