Skip to content

Commit 5a03ee5

Browse files
phlptppre-commit-ci[bot]henryiii
authored
Allow non standard option names like -option (#1078)
This has been bounced around for a couple years now #474 and a few others have expressed desire to work with non-standard option names. We have been somewhat resistant to that but I think it can be done now. This PR adds a modifier `allow_non_standard_option_names()` It is purposely long, it is purposely off by default. But what it does is allow option names with a single `-` to act like a short option name. With this modifier enabled no single letter short option names are allowed to start with the same letter as a non-standard names. For example `-s` and `-single` would not be allowed. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Henry Schreiner <[email protected]>
1 parent 7bc90c7 commit 5a03ee5

33 files changed

+241
-48
lines changed

CPPLINT.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ linelength=120 # As in .clang-format
33

44
# Unused filters
55
filter=-build/c++11 # Reports e.g. chrono and thread, which overlap with Chromium's API. Not applicable to general C++ projects.
6+
filter=-build/c++17 # google only restrictions not relevant
67
filter=-build/include_order # Requires unusual include order that encourages creating not self-contained headers
78
filter=-build/include_subdir # Prevents including files in current directory for whatever reason
89
filter=-readability/nolint # Conflicts with clang-tidy
@@ -13,3 +14,4 @@ filter=-runtime/string # Requires not using static const strings which makes th
1314
filter=-whitespace/blank_line # Unnecessarily strict with blank lines that otherwise help with readability
1415
filter=-whitespace/indent # Requires strange 3-space indent of private/protected/public markers
1516
filter=-whitespace/parens,-whitespace/braces # Conflict with clang-format
17+
filter=-whitespace/newline # handled by clang-format

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,6 @@ installation fuss.
158158
There are some other possible "features" that are intentionally not supported by
159159
this library:
160160

161-
- Non-standard variations on syntax, like `-long` options. This is non-standard
162-
and should be avoided, so that is enforced by this library.
163161
- Completion of partial options, such as Python's `argparse` supplies for
164162
incomplete arguments. It's better not to guess. Most third party command line
165163
parsers for python actually reimplement command line parsing rather than using
@@ -904,6 +902,14 @@ option_groups. These are:
904902
the form of `/s /long /file:file_name.ext` This option does not change how
905903
options are specified in the `add_option` calls or the ability to process
906904
options in the form of `-s --long --file=file_name.ext`.
905+
- `.allow_non_standard_option_names()`:🚧 Allow specification of single `-` long
906+
form option names. This is not recommended but is available to enable
907+
reworking of existing interfaces. If this modifier is enabled on an app or
908+
subcommand, options or flags can be specified like normal but instead of
909+
throwing an exception, long form single dash option names will be allowed. It
910+
is not allowed to have a single character short option starting with the same
911+
character as a single dash long form name; for example, `-s` and `-single` are
912+
not allowed in the same application.
907913
- `.fallthrough()`: Allow extra unmatched options and positionals to "fall
908914
through" and be matched on a parent option. Subcommands by default are allowed
909915
to "fall through" as in they will first attempt to match on the current

azure-pipelines.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
- job: CppLint
2323
pool:
2424
vmImage: "ubuntu-latest"
25-
container: sharaku/cpplint:latest
25+
container: helics/buildenv:cpplint
2626
steps:
2727
- bash: cpplint --counting=detailed --recursive examples include/CLI tests
2828
displayName: Checking against google style guide

examples/custom_parse.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <CLI/CLI.hpp>
1010
#include <iostream>
1111
#include <sstream>
12+
#include <string>
1213

1314
// example file to demonstrate a custom lexical cast function
1415

examples/formatter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <CLI/CLI.hpp>
88
#include <iostream>
99
#include <memory>
10+
#include <string>
1011

1112
class MyFormatter : public CLI::Formatter {
1213
public:

examples/inter_argument_order.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <CLI/CLI.hpp>
88
#include <algorithm>
99
#include <iostream>
10+
#include <string>
1011
#include <tuple>
1112
#include <vector>
1213

include/CLI/App.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ class App {
260260
/// This is potentially useful as a modifier subcommand
261261
bool silent_{false};
262262

263+
/// indicator that the subcommand should allow non-standard option arguments, such as -single_dash_flag
264+
bool allow_non_standard_options_{false};
265+
263266
/// Counts the number of times this command/subcommand was parsed
264267
std::uint32_t parsed_{0U};
265268

@@ -392,6 +395,12 @@ class App {
392395
return this;
393396
}
394397

398+
/// allow non standard option names
399+
App *allow_non_standard_option_names(bool allowed = true) {
400+
allow_non_standard_options_ = allowed;
401+
return this;
402+
}
403+
395404
/// Set the subcommand to be disabled by default, so on clear(), at the start of each parse it is disabled
396405
App *disabled_by_default(bool disable = true) {
397406
if(disable) {
@@ -1146,6 +1155,9 @@ class App {
11461155
/// Get the status of silence
11471156
CLI11_NODISCARD bool get_silent() const { return silent_; }
11481157

1158+
/// Get the status of silence
1159+
CLI11_NODISCARD bool get_allow_non_standard_option_names() const { return allow_non_standard_options_; }
1160+
11491161
/// Get the status of disabled
11501162
CLI11_NODISCARD bool get_immediate_callback() const { return immediate_callback_; }
11511163

include/CLI/Option.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,13 @@ class Option : public OptionBase<Option> {
341341
///@}
342342

343343
/// Making an option by hand is not defined, it must be made by the App class
344-
Option(std::string option_name, std::string option_description, callback_t callback, App *parent)
344+
Option(std::string option_name,
345+
std::string option_description,
346+
callback_t callback,
347+
App *parent,
348+
bool allow_non_standard = false)
345349
: description_(std::move(option_description)), parent_(parent), callback_(std::move(callback)) {
346-
std::tie(snames_, lnames_, pname_) = detail::get_names(detail::split_names(option_name));
350+
std::tie(snames_, lnames_, pname_) = detail::get_names(detail::split_names(option_name), allow_non_standard);
347351
}
348352

349353
public:

include/CLI/Split.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ CLI11_INLINE std::vector<std::pair<std::string, std::string>> get_default_flag_v
3939

4040
/// Get a vector of short names, one of long names, and a single name
4141
CLI11_INLINE std::tuple<std::vector<std::string>, std::vector<std::string>, std::string>
42-
get_names(const std::vector<std::string> &input);
42+
get_names(const std::vector<std::string> &input, bool allow_non_standard = false);
4343

4444
} // namespace detail
4545
// [CLI11:split_hpp:end]

include/CLI/Timer.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <array>
2020
#include <chrono>
21+
#include <cstdio>
2122
#include <functional>
2223
#include <iostream>
2324
#include <string>

0 commit comments

Comments
 (0)