Skip to content

Commit

Permalink
style: pre-commit.ci fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pre-commit-ci[bot] committed Jan 4, 2024
1 parent 73c9bbc commit 2baa049
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 56 deletions.
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,12 @@ filters on the key values is performed.
`CLI::FileOnDefaultPath(default_path, false)`. This allows multiple paths to
be chained using multiple transform calls.
- `CLI::EscapedString`: 🚧 can be used to process an escaped string. The processing is equivalent to that used for TOML config files, see [TOML strings](https://toml.io/en/v1.0.0#string). With 2 notable exceptions. `\`` can also be used as a literal string notation, and it also allows binary string notation see [binary strings](TO ADD). The escaped string processing will remove outer quotes if present, `"` will indicate a string with potential escape sequences, `'` and `\`` will indicate a literal string and the quotes removed but no escape sequences will be processed. This is the same escape processing as used in config files.
- `CLI::EscapedString`: 🚧 can be used to process an escaped string. The
processing is equivalent to that used for TOML config files, see
[TOML strings](https://toml.io/en/v1.0.0#string). With 2 notable exceptions.
`\`` can also be used as a literal string notation, and it also allows binary string notation see [binary strings](TO ADD). The escaped string processing will remove outer quotes if present, `"`will indicate a string with potential escape sequences, `'`and`\``
will indicate a literal string and the quotes removed but no escape sequences
will be processed. This is the same escape processing as used in config files.
##### Validator operations
Expand Down Expand Up @@ -875,9 +880,11 @@ through the `add_subcommand` method have the same restrictions as option names.
- `--subcommand1.subsub.f val` (short form nested subcommand option)

The use of dot notation in this form is equivalent `--subcommand.long <args>` =>
`subcommand --long <args> ++`. Nested subcommands also work `sub1.subsub`
would trigger the subsub subcommand in `sub1`. This is equivalent to "sub1
subsub". Quotes around the subcommand names are permitted 🚧 following the TOML standard for such specification. This includes allowing escape sequences. For example `"subcommand".'f'` or `"subcommand.with.dots".arg1 = value`.
`subcommand --long <args> ++`. Nested subcommands also work `sub1.subsub` would
trigger the subsub subcommand in `sub1`. This is equivalent to "sub1 subsub".
Quotes around the subcommand names are permitted 🚧 following the TOML standard
for such specification. This includes allowing escape sequences. For example
`"subcommand".'f'` or `"subcommand.with.dots".arg1 = value`.

#### Subcommand options

Expand Down Expand Up @@ -1211,10 +1218,12 @@ 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. The config reader can read most aspects of TOML files including strings both literal 🚧 and with potential escape sequences 🚧, digit separators 🚧, and multi-line strings 🚧, 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:
default reader can also accept files in INI format as well. The config reader
can read most aspects of TOML files including strings both literal 🚧 and with
potential escape sequences 🚧, digit separators 🚧, and multi-line strings 🚧,
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:

```toml
# Comments are supported, using a #
Expand Down
4 changes: 2 additions & 2 deletions include/CLI/Validators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ class IPV4Validator : public Validator {
IPV4Validator();
};

class EscapedStringTransformer :public Validator {
public:
class EscapedStringTransformer : public Validator {
public:
EscapedStringTransformer();
};

Expand Down
4 changes: 1 addition & 3 deletions include/CLI/impl/Config_inl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,7 @@ inline std::vector<ConfigItem> ConfigBase::from_config(std::istream &input) cons
if(keyChar == '\"') {
try {
item = detail::remove_escaped_characters(item);
}
catch (const std::invalid_argument& ia)
{
} catch(const std::invalid_argument &ia) {
throw CLI::ParseError(ia.what(), CLI::ExitCodes::InvalidError);
}
}
Expand Down
24 changes: 8 additions & 16 deletions include/CLI/impl/Validators_inl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,29 +233,21 @@ CLI11_INLINE IPV4Validator::IPV4Validator() : Validator("IPV4") {
};
}

CLI11_INLINE EscapedStringTransformer::EscapedStringTransformer()
{
CLI11_INLINE EscapedStringTransformer::EscapedStringTransformer() {
func_ = [](std::string &str) {
try {
if (str.size()>1 && (str.front() == '\"' || str.front() == '\'' || str.front() == '`') &&str.front()==str.back())
{
if(str.size() > 1 && (str.front() == '\"' || str.front() == '\'' || str.front() == '`') &&
str.front() == str.back()) {
process_quoted_string(str);
}
else if (str.find_first_of('\\') != std::string::npos)
{
if (detail::is_binary_escaped_string(str))
{
str=detail::extract_binary_string(str);
}
else
{
} else if(str.find_first_of('\\') != std::string::npos) {
if(detail::is_binary_escaped_string(str)) {
str = detail::extract_binary_string(str);
} else {
str = remove_escaped_characters(str);
}
}
return std::string{};
}
catch (const std::invalid_argument& ia)
{
} catch(const std::invalid_argument &ia) {
return std::string(ia.what());
}
};
Expand Down
2 changes: 1 addition & 1 deletion tests/OptionTypeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ TEST_CASE_METHOD(TApp, "IntegerOverFlowInt", "[optiontype]") {

args = {"-a", "262625424225252"};
CHECK_THROWS_AS(run(), CLI::ConversionError);

args = {"-b", "262625424225252"};
CHECK_THROWS_AS(run(), CLI::ConversionError);

Expand Down
50 changes: 24 additions & 26 deletions tests/TransformTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -706,55 +706,53 @@ TEST_CASE_METHOD(TApp, "NumberWithUnitBadInput", "[transform]") {
CHECK_THROWS_AS(run(), CLI::ValidationError);
}

static const std::map<std::string,std::string> validValues =
{ {"test\\u03C0\\u00e9",from_u8string(u8"test\u03C0\u00E9")},
{"test\\u03C0\\u00e9",from_u8string(u8"test\u73C0\u0057")},
{"test\\U0001F600\\u00E9",from_u8string(u8"test\U0001F600\u00E9")},
{R"("this\nis\na\nfour\tline test")","this\nis\na\nfour\tline test"},
{"'B\"(\\x35\\xa7\\x46)\"'",std::string{0x35,static_cast<char>(0xa7),0x46} },
{"B\"(\\x35\\xa7\\x46)\"",std::string{0x35,static_cast<char>(0xa7),0x46} },
{"test\\ntest","test\ntest"},
{"\"test\\ntest","\"test\ntest"},
{R"('this\nis\na\nfour\tline test')",R"(this\nis\na\nfour\tline test)"},
{R"("this\nis\na\nfour\tline test")","this\nis\na\nfour\tline test"},
{R"(`this\nis\na\nfour\tline test`)",R"(this\nis\na\nfour\tline test)"}
};

TEST_CASE_METHOD(TApp,"StringEscapeValid", "[transform]") {
static const std::map<std::string, std::string> validValues = {
{"test\\u03C0\\u00e9", from_u8string(u8"test\u03C0\u00E9")},
{"test\\u03C0\\u00e9", from_u8string(u8"test\u73C0\u0057")},
{"test\\U0001F600\\u00E9", from_u8string(u8"test\U0001F600\u00E9")},
{R"("this\nis\na\nfour\tline test")", "this\nis\na\nfour\tline test"},
{"'B\"(\\x35\\xa7\\x46)\"'", std::string{0x35, static_cast<char>(0xa7), 0x46}},
{"B\"(\\x35\\xa7\\x46)\"", std::string{0x35, static_cast<char>(0xa7), 0x46}},
{"test\\ntest", "test\ntest"},
{"\"test\\ntest", "\"test\ntest"},
{R"('this\nis\na\nfour\tline test')", R"(this\nis\na\nfour\tline test)"},
{R"("this\nis\na\nfour\tline test")", "this\nis\na\nfour\tline test"},
{R"(`this\nis\na\nfour\tline test`)", R"(this\nis\na\nfour\tline test)"}};

TEST_CASE_METHOD(TApp, "StringEscapeValid", "[transform]") {

auto test_data = GENERATE(from_range(validValues));

std::string value{};

app.add_option("-n", value)->transform(CLI::EscapedString);

args={"-n",test_data.first};
args = {"-n", test_data.first};

run();
CHECK(test_data.second == value);

}

static const std::vector<std::string> invalidValues =
{ "test\\U0001M600\\u00E9","test\\U0001E600\\u00M9","test\\U0001E600\\uD8E9",
"test\\U0001E600\\uD8","test\\U0001E60" "test\\qbad"};
static const std::vector<std::string> invalidValues = {"test\\U0001M600\\u00E9",
"test\\U0001E600\\u00M9",
"test\\U0001E600\\uD8E9",
"test\\U0001E600\\uD8",
"test\\U0001E60"
"test\\qbad"};


TEST_CASE_METHOD(TApp,"StringEscapeInvalid", "[transform]") {
TEST_CASE_METHOD(TApp, "StringEscapeInvalid", "[transform]") {

auto test_data = GENERATE(from_range(invalidValues));

std::string value{};

app.add_option("-n", value)->transform(CLI::EscapedString);

args={"-n",test_data};
args = {"-n", test_data};

CHECK_THROWS_AS(run(),CLI::ValidationError);

CHECK_THROWS_AS(run(), CLI::ValidationError);
}


TEST_CASE_METHOD(TApp, "NumberWithUnitIntOverflow", "[transform]") {
std::map<std::string, int> mapping{{"a", 1000000}, {"b", 100}, {"c", 101}};

Expand Down

0 comments on commit 2baa049

Please sign in to comment.