1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-05-06 07:03:52 +00:00

Rename add_config to set_config

This commit is contained in:
Henry Fredrick Schreiner 2017-11-28 15:59:25 -05:00 committed by Henry Schreiner
parent 7e5f4dae50
commit a6c17a4a9d
4 changed files with 33 additions and 29 deletions

View File

@ -25,7 +25,7 @@
* Added `CLI::ArgumentMismatch` [#56](https://github.com/CLIUtils/CLI11/pull/56) and fixed missing failure if one arg expected [#55](https://github.com/CLIUtils/CLI11/issues/55) * Added `CLI::ArgumentMismatch` [#56](https://github.com/CLIUtils/CLI11/pull/56) and fixed missing failure if one arg expected [#55](https://github.com/CLIUtils/CLI11/issues/55)
* Support for minimum unlimited expected arguments [#56](https://github.com/CLIUtils/CLI11/pull/56) * Support for minimum unlimited expected arguments [#56](https://github.com/CLIUtils/CLI11/pull/56)
* Single internal arg parse function [#56](https://github.com/CLIUtils/CLI11/pull/56) * Single internal arg parse function [#56](https://github.com/CLIUtils/CLI11/pull/56)
* Allow options to be disabled from INI file [#60](https://github.com/CLIUtils/CLI11/pull/60) * Allow options to be disabled from INI file, rename `add_config` to `set_config` [#60](https://github.com/CLIUtils/CLI11/pull/60)
## Version 1.2 ## Version 1.2

View File

@ -238,13 +238,13 @@ There are several options that are supported on the main app and subcommands. Th
## Configuration file ## Configuration file
```cpp ```cpp
app.add_config(option_name, app.set_config(option_name="",
default_file_name="", default_file_name="",
help_string="Read an ini file", help_string="Read an ini file",
required=false) required=false)
``` ```
Adding 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. 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 `ini` format. An example of a file:
```ini ```ini
; Commments are supported, using a ; ; Commments are supported, using a ;

View File

@ -578,8 +578,8 @@ class App {
return opt; return opt;
} }
/// Add a configuration ini file option /// Set a configuration ini file option, or clear it if no name passed
Option *add_config(std::string name = "--config", Option *set_config(std::string name = "",
std::string default_filename = "", std::string default_filename = "",
std::string help = "Read an ini file", std::string help = "Read an ini file",
bool required = false) { bool required = false) {
@ -587,9 +587,14 @@ class App {
// Remove existing config if present // Remove existing config if present
if(config_ptr_ != nullptr) if(config_ptr_ != nullptr)
remove_option(config_ptr_); remove_option(config_ptr_);
// Only add config if option passed
if(name) {
config_name_ = default_filename; config_name_ = default_filename;
config_required_ = required; config_required_ = required;
config_ptr_ = add_option(name, config_name_, help, !default_filename.empty()); config_ptr_ = add_option(name, config_name_, help, !default_filename.empty());
}
return config_ptr_; return config_ptr_;
} }

View File

@ -162,7 +162,7 @@ TEST_F(TApp, IniNotRequired) {
TempFile tmpini{"TestIniTmp.ini"}; TempFile tmpini{"TestIniTmp.ini"};
app.add_config("--config", tmpini); app.set_config("--config", tmpini);
{ {
std::ofstream out{tmpini}; std::ofstream out{tmpini};
@ -200,7 +200,7 @@ TEST_F(TApp, IniNotRequiredNotDefault) {
TempFile tmpini{"TestIniTmp.ini"}; TempFile tmpini{"TestIniTmp.ini"};
TempFile tmpini2{"TestIniTmp2.ini"}; TempFile tmpini2{"TestIniTmp2.ini"};
app.add_config("--config", tmpini); app.set_config("--config", tmpini);
{ {
std::ofstream out{tmpini}; std::ofstream out{tmpini};
@ -237,7 +237,7 @@ TEST_F(TApp, IniNotRequiredNotDefault) {
TEST_F(TApp, IniRequiredNotFound) { TEST_F(TApp, IniRequiredNotFound) {
std::string noini = "TestIniNotExist.ini"; std::string noini = "TestIniNotExist.ini";
app.add_config("--config", noini, "", true); app.set_config("--config", noini, "", true);
EXPECT_THROW(run(), CLI::FileError); EXPECT_THROW(run(), CLI::FileError);
} }
@ -245,7 +245,7 @@ TEST_F(TApp, IniRequiredNotFound) {
TEST_F(TApp, IniNotRequiredPassedNotFound) { TEST_F(TApp, IniNotRequiredPassedNotFound) {
std::string noini = "TestIniNotExist.ini"; std::string noini = "TestIniNotExist.ini";
app.add_config("--config", "", "", false); app.set_config("--config", "", "", false);
args = {"--config", noini}; args = {"--config", noini};
EXPECT_THROW(run(), CLI::FileError); EXPECT_THROW(run(), CLI::FileError);
@ -262,9 +262,9 @@ TEST_F(TApp, IniOverwrite) {
std::string orig = "filename_not_exist.ini"; std::string orig = "filename_not_exist.ini";
std::string next = "TestIniTmp.ini"; std::string next = "TestIniTmp.ini";
app.add_config("--config", orig); app.set_config("--config", orig);
// Make sure this can be overwritten // Make sure this can be overwritten
app.add_config("--conf", next); app.set_config("--conf", next);
int two = 7; int two = 7;
app.add_option("--two", two); app.add_option("--two", two);
@ -277,7 +277,7 @@ TEST_F(TApp, IniRequired) {
TempFile tmpini{"TestIniTmp.ini"}; TempFile tmpini{"TestIniTmp.ini"};
app.add_config("--config", tmpini, "", true); app.set_config("--config", tmpini, "", true);
{ {
std::ofstream out{tmpini}; std::ofstream out{tmpini};
@ -316,7 +316,7 @@ TEST_F(TApp, IniVector) {
TempFile tmpini{"TestIniTmp.ini"}; TempFile tmpini{"TestIniTmp.ini"};
app.add_config("--config", tmpini); app.set_config("--config", tmpini);
{ {
std::ofstream out{tmpini}; std::ofstream out{tmpini};
@ -339,7 +339,7 @@ TEST_F(TApp, IniLayered) {
TempFile tmpini{"TestIniTmp.ini"}; TempFile tmpini{"TestIniTmp.ini"};
app.add_config("--config", tmpini); app.set_config("--config", tmpini);
{ {
std::ofstream out{tmpini}; std::ofstream out{tmpini};
@ -368,7 +368,7 @@ TEST_F(TApp, IniFailure) {
TempFile tmpini{"TestIniTmp.ini"}; TempFile tmpini{"TestIniTmp.ini"};
app.add_config("--config", tmpini); app.set_config("--config", tmpini);
{ {
std::ofstream out{tmpini}; std::ofstream out{tmpini};
@ -383,7 +383,7 @@ TEST_F(TApp, IniConfigurable) {
TempFile tmpini{"TestIniTmp.ini"}; TempFile tmpini{"TestIniTmp.ini"};
app.add_config("--config", tmpini); app.set_config("--config", tmpini);
bool value; bool value;
app.add_flag("--val", value)->configurable(true); app.add_flag("--val", value)->configurable(true);
@ -401,7 +401,7 @@ TEST_F(TApp, IniNotConfigurable) {
TempFile tmpini{"TestIniTmp.ini"}; TempFile tmpini{"TestIniTmp.ini"};
app.add_config("--config", tmpini); app.set_config("--config", tmpini);
bool value; bool value;
app.add_flag("--val", value)->configurable(false); app.add_flag("--val", value)->configurable(false);
@ -419,7 +419,7 @@ TEST_F(TApp, IniSubFailure) {
TempFile tmpini{"TestIniTmp.ini"}; TempFile tmpini{"TestIniTmp.ini"};
app.add_subcommand("other"); app.add_subcommand("other");
app.add_config("--config", tmpini); app.set_config("--config", tmpini);
{ {
std::ofstream out{tmpini}; std::ofstream out{tmpini};
@ -429,12 +429,11 @@ TEST_F(TApp, IniSubFailure) {
EXPECT_THROW(run(), CLI::INIError); EXPECT_THROW(run(), CLI::INIError);
} }
d TEST_F(TApp, IniNoSubFailure) {
TEST_F(TApp, IniNoSubFailure) {
TempFile tmpini{"TestIniTmp.ini"}; TempFile tmpini{"TestIniTmp.ini"};
app.add_config("--config", tmpini); app.set_config("--config", tmpini);
{ {
std::ofstream out{tmpini}; std::ofstream out{tmpini};
@ -450,7 +449,7 @@ TEST_F(TApp, IniFlagConvertFailure) {
TempFile tmpini{"TestIniTmp.ini"}; TempFile tmpini{"TestIniTmp.ini"};
app.add_flag("--flag"); app.add_flag("--flag");
app.add_config("--config", tmpini); app.set_config("--config", tmpini);
{ {
std::ofstream out{tmpini}; std::ofstream out{tmpini};
@ -466,7 +465,7 @@ TEST_F(TApp, IniFlagNumbers) {
bool boo; bool boo;
app.add_flag("--flag", boo); app.add_flag("--flag", boo);
app.add_config("--config", tmpini); app.set_config("--config", tmpini);
{ {
std::ofstream out{tmpini}; std::ofstream out{tmpini};
@ -483,7 +482,7 @@ TEST_F(TApp, IniFlagDual) {
bool boo; bool boo;
app.add_flag("--flag", boo); app.add_flag("--flag", boo);
app.add_config("--config", tmpini); app.set_config("--config", tmpini);
{ {
std::ofstream out{tmpini}; std::ofstream out{tmpini};
@ -502,7 +501,7 @@ TEST_F(TApp, IniFlagText) {
app.add_flag("--flag2", flag2); app.add_flag("--flag2", flag2);
app.add_flag("--flag3", flag3); app.add_flag("--flag3", flag3);
app.add_flag("--flag4", flag4); app.add_flag("--flag4", flag4);
app.add_config("--config", tmpini); app.set_config("--config", tmpini);
{ {
std::ofstream out{tmpini}; std::ofstream out{tmpini};
@ -522,7 +521,7 @@ TEST_F(TApp, IniFlagText) {
TEST_F(TApp, IniFlags) { TEST_F(TApp, IniFlags) {
TempFile tmpini{"TestIniTmp.ini"}; TempFile tmpini{"TestIniTmp.ini"};
app.add_config("--config", tmpini); app.set_config("--config", tmpini);
{ {
std::ofstream out{tmpini}; std::ofstream out{tmpini};