From a6c17a4a9df4ffc21ca6d6263d9db5acbd517a34 Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Tue, 28 Nov 2017 15:59:25 -0500 Subject: [PATCH] Rename add_config to set_config --- CHANGELOG.md | 2 +- README.md | 4 ++-- include/CLI/App.hpp | 15 ++++++++++----- tests/IniTest.cpp | 41 ++++++++++++++++++++--------------------- 4 files changed, 33 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6616b779..529e156d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) * 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) -* 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 diff --git a/README.md b/README.md index 034e61b8..bc6f5933 100644 --- a/README.md +++ b/README.md @@ -238,13 +238,13 @@ There are several options that are supported on the main app and subcommands. Th ## Configuration file ```cpp -app.add_config(option_name, +app.set_config(option_name="", default_file_name="", help_string="Read an ini file", 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 ; Commments are supported, using a ; diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index 04cb2794..a3b62249 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -578,8 +578,8 @@ class App { return opt; } - /// Add a configuration ini file option - Option *add_config(std::string name = "--config", + /// Set a configuration ini file option, or clear it if no name passed + Option *set_config(std::string name = "", std::string default_filename = "", std::string help = "Read an ini file", bool required = false) { @@ -587,9 +587,14 @@ class App { // Remove existing config if present if(config_ptr_ != nullptr) remove_option(config_ptr_); - config_name_ = default_filename; - config_required_ = required; - config_ptr_ = add_option(name, config_name_, help, !default_filename.empty()); + + // Only add config if option passed + if(name) { + config_name_ = default_filename; + config_required_ = required; + config_ptr_ = add_option(name, config_name_, help, !default_filename.empty()); + } + return config_ptr_; } diff --git a/tests/IniTest.cpp b/tests/IniTest.cpp index 94eeb24c..c94420a3 100644 --- a/tests/IniTest.cpp +++ b/tests/IniTest.cpp @@ -162,7 +162,7 @@ TEST_F(TApp, IniNotRequired) { TempFile tmpini{"TestIniTmp.ini"}; - app.add_config("--config", tmpini); + app.set_config("--config", tmpini); { std::ofstream out{tmpini}; @@ -200,7 +200,7 @@ TEST_F(TApp, IniNotRequiredNotDefault) { TempFile tmpini{"TestIniTmp.ini"}; TempFile tmpini2{"TestIniTmp2.ini"}; - app.add_config("--config", tmpini); + app.set_config("--config", tmpini); { std::ofstream out{tmpini}; @@ -237,7 +237,7 @@ TEST_F(TApp, IniNotRequiredNotDefault) { TEST_F(TApp, IniRequiredNotFound) { std::string noini = "TestIniNotExist.ini"; - app.add_config("--config", noini, "", true); + app.set_config("--config", noini, "", true); EXPECT_THROW(run(), CLI::FileError); } @@ -245,7 +245,7 @@ TEST_F(TApp, IniRequiredNotFound) { TEST_F(TApp, IniNotRequiredPassedNotFound) { std::string noini = "TestIniNotExist.ini"; - app.add_config("--config", "", "", false); + app.set_config("--config", "", "", false); args = {"--config", noini}; EXPECT_THROW(run(), CLI::FileError); @@ -262,9 +262,9 @@ TEST_F(TApp, IniOverwrite) { std::string orig = "filename_not_exist.ini"; std::string next = "TestIniTmp.ini"; - app.add_config("--config", orig); + app.set_config("--config", orig); // Make sure this can be overwritten - app.add_config("--conf", next); + app.set_config("--conf", next); int two = 7; app.add_option("--two", two); @@ -277,7 +277,7 @@ TEST_F(TApp, IniRequired) { TempFile tmpini{"TestIniTmp.ini"}; - app.add_config("--config", tmpini, "", true); + app.set_config("--config", tmpini, "", true); { std::ofstream out{tmpini}; @@ -316,7 +316,7 @@ TEST_F(TApp, IniVector) { TempFile tmpini{"TestIniTmp.ini"}; - app.add_config("--config", tmpini); + app.set_config("--config", tmpini); { std::ofstream out{tmpini}; @@ -339,7 +339,7 @@ TEST_F(TApp, IniLayered) { TempFile tmpini{"TestIniTmp.ini"}; - app.add_config("--config", tmpini); + app.set_config("--config", tmpini); { std::ofstream out{tmpini}; @@ -368,7 +368,7 @@ TEST_F(TApp, IniFailure) { TempFile tmpini{"TestIniTmp.ini"}; - app.add_config("--config", tmpini); + app.set_config("--config", tmpini); { std::ofstream out{tmpini}; @@ -383,7 +383,7 @@ TEST_F(TApp, IniConfigurable) { TempFile tmpini{"TestIniTmp.ini"}; - app.add_config("--config", tmpini); + app.set_config("--config", tmpini); bool value; app.add_flag("--val", value)->configurable(true); @@ -401,7 +401,7 @@ TEST_F(TApp, IniNotConfigurable) { TempFile tmpini{"TestIniTmp.ini"}; - app.add_config("--config", tmpini); + app.set_config("--config", tmpini); bool value; app.add_flag("--val", value)->configurable(false); @@ -419,7 +419,7 @@ TEST_F(TApp, IniSubFailure) { TempFile tmpini{"TestIniTmp.ini"}; app.add_subcommand("other"); - app.add_config("--config", tmpini); + app.set_config("--config", tmpini); { std::ofstream out{tmpini}; @@ -429,12 +429,11 @@ TEST_F(TApp, IniSubFailure) { EXPECT_THROW(run(), CLI::INIError); } - -TEST_F(TApp, IniNoSubFailure) { +d TEST_F(TApp, IniNoSubFailure) { TempFile tmpini{"TestIniTmp.ini"}; - app.add_config("--config", tmpini); + app.set_config("--config", tmpini); { std::ofstream out{tmpini}; @@ -450,7 +449,7 @@ TEST_F(TApp, IniFlagConvertFailure) { TempFile tmpini{"TestIniTmp.ini"}; app.add_flag("--flag"); - app.add_config("--config", tmpini); + app.set_config("--config", tmpini); { std::ofstream out{tmpini}; @@ -466,7 +465,7 @@ TEST_F(TApp, IniFlagNumbers) { bool boo; app.add_flag("--flag", boo); - app.add_config("--config", tmpini); + app.set_config("--config", tmpini); { std::ofstream out{tmpini}; @@ -483,7 +482,7 @@ TEST_F(TApp, IniFlagDual) { bool boo; app.add_flag("--flag", boo); - app.add_config("--config", tmpini); + app.set_config("--config", tmpini); { std::ofstream out{tmpini}; @@ -502,7 +501,7 @@ TEST_F(TApp, IniFlagText) { app.add_flag("--flag2", flag2); app.add_flag("--flag3", flag3); app.add_flag("--flag4", flag4); - app.add_config("--config", tmpini); + app.set_config("--config", tmpini); { std::ofstream out{tmpini}; @@ -522,7 +521,7 @@ TEST_F(TApp, IniFlagText) { TEST_F(TApp, IniFlags) { TempFile tmpini{"TestIniTmp.ini"}; - app.add_config("--config", tmpini); + app.set_config("--config", tmpini); { std::ofstream out{tmpini};