1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-29 20:23:55 +00:00
CLI11/include/CLI/Config.hpp
Philip Top 418b7175f5 Type size refactor (#325)
* add expanded type_size specification

* add some more checks for type_size_max

* continued work on getting type sizes more flexible

* make some more tweaks to option to split up validate and reduce sections

* git rid of exceptions on the type_size functions exceptions,  allow any number to be entered for the min and max and don't make a distinction between flags and other types.

* add expected count

* add the allow extra args flag in an option

* start working in allow_extra_args

* write some stuff in the book,  and continue working on the failing test cases

* fix a few more of the helpers tests

* a few more test cases running

* all tests pass, fixing calls in ini files

* get vector<pair> working and all tests passing

* change callback to use reference to remove allocation and copy operation

* add support and test for vector<vector<X>>

* change Validators_ to validators_ for consistency

* fix linux warnings and errors by reording some templates and adding some typename keywords

* add support for std::vector<X> as the cross conversion type so optional<std::vector<X>> is supported using the full template of add_option.

* a few more test cases to take care of some coverage gaps

* add missing parenthesis

* add some more tests for coverage gaps

* add test for flag like option

* add transform test for `as<X>` function and make it pass through the defaults

* add a few more tests and have vector default string interpreted correctly.

* add test for defaulted integer,  and route default string for defaulted value which would otherwise be empty

* some code cleanup and comments and few more test coverage gap tests

* add more tests and fix a few bugs on the type size and different code paths

* remove path in results by fixing the clear of options so they go back to parsing state.

* get coverage back to 100%

* clang_tidy, and codacy fixes

* reorder the lexical_conversion definitions

* update some formatting

* update whitespace on book chapter
2019-11-10 05:36:16 +10:30

74 lines
2.4 KiB
C++

#pragma once
// Distributed under the 3-Clause BSD License. See accompanying
// file LICENSE or https://github.com/CLIUtils/CLI11 for details.
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include "CLI/App.hpp"
#include "CLI/ConfigFwd.hpp"
#include "CLI/StringTools.hpp"
namespace CLI {
inline std::string
ConfigINI::to_config(const App *app, bool default_also, bool write_description, std::string prefix) const {
std::stringstream out;
for(const Option *opt : app->get_options({})) {
// Only process option with a long-name and configurable
if(!opt->get_lnames().empty() && opt->get_configurable()) {
std::string name = prefix + opt->get_lnames()[0];
std::string value;
// Non-flags
if(opt->get_expected_min() != 0) {
// If the option was found on command line
if(opt->count() > 0)
value = detail::ini_join(opt->results());
// If the option has a default and is requested by optional argument
else if(default_also && !opt->get_default_str().empty())
value = opt->get_default_str();
// Flag, one passed
} else if(opt->count() == 1) {
value = "true";
// Flag, multiple passed
} else if(opt->count() > 1) {
value = std::to_string(opt->count());
// Flag, not present
} else if(opt->count() == 0 && default_also) {
value = "false";
}
if(!value.empty()) {
if(write_description && opt->has_description()) {
if(static_cast<int>(out.tellp()) != 0) {
out << std::endl;
}
out << "; " << detail::fix_newlines("; ", opt->get_description()) << std::endl;
}
// Don't try to quote anything that is not size 1
if(opt->get_items_expected_max() != 1)
out << name << "=" << value << std::endl;
else
out << name << "=" << detail::add_quotes_if_needed(value) << std::endl;
}
}
}
for(const App *subcom : app->get_subcommands({}))
out << to_config(subcom, default_also, write_description, prefix + subcom->get_name() + ".");
return out.str();
}
} // namespace CLI