-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create args view with positionals
- Loading branch information
1 parent
4ea81b6
commit 717cfcd
Showing
3 changed files
with
174 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1 @@ | ||
#include <format> | ||
|
||
#include "experimental/parsing.hpp" | ||
|
||
// +---------------------+ | ||
// | parsing helpers | | ||
// +---------------------+ | ||
|
||
constexpr bool is_dash_dash(std::string_view const whole_arg) noexcept { | ||
return whole_arg.length() == 2 && whole_arg[0] == '-' && whole_arg[1] == '-'; | ||
} | ||
|
||
constexpr bool looks_positional(std::string_view const whole_arg) noexcept { | ||
auto const num_of_dashes = whole_arg.find_first_not_of('-'); | ||
return num_of_dashes == 0 || (whole_arg.length() == 1 && num_of_dashes == std::string_view::npos); | ||
} | ||
|
||
constexpr std::string_view get_if_short_flags(std::string_view const whole_arg) noexcept { | ||
auto const num_of_dashes = whole_arg.find_first_not_of('-'); | ||
auto const flags = whole_arg.substr(1); | ||
if (num_of_dashes == 1 && flags.length() >= 1) | ||
return flags; | ||
return {}; | ||
} | ||
|
||
constexpr std::string_view get_if_long_flag(std::string_view const whole_arg) noexcept { | ||
auto const name = whole_arg.substr(2); | ||
auto const num_of_dashes = whole_arg.find_first_not_of('-'); | ||
if (num_of_dashes == 2 && name.length() >= 2) | ||
return name; | ||
return {}; | ||
} | ||
|
||
constexpr ParsedOption try_parse_option(std::string_view const whole_arg) noexcept { | ||
auto const num_of_dashes = whole_arg.find_first_not_of('-'); | ||
auto const eq_idx = whole_arg.find('=', num_of_dashes); | ||
bool const has_equals = eq_idx != std::string_view::npos; | ||
if (num_of_dashes == 1) { | ||
// short option, e.g. `-O` | ||
auto const name = whole_arg.substr(1, 1); | ||
if (has_equals) { | ||
if (whole_arg.length() > 3) { | ||
// has equals followed by some value, e.g. `-O=2` | ||
return {name, whole_arg.substr(3)}; | ||
} | ||
// should this `-O=` be handled like this? | ||
return {name, ""}; | ||
} | ||
|
||
if (whole_arg.length() > 2) { | ||
// only followed by some value, e.g. `-O2` | ||
return {name, whole_arg.substr(2)}; | ||
} | ||
|
||
// case left: has no value (next CLI argument could be it) | ||
return {name, std::nullopt}; | ||
} | ||
|
||
if (num_of_dashes == 2 && whole_arg.length() > 3) { | ||
// long option, e.g. `--name` | ||
if (has_equals) { | ||
auto const name = whole_arg.substr(2, eq_idx - 2); | ||
auto const value = whole_arg.substr(eq_idx + 1); | ||
return {name, value}; | ||
} | ||
// has no value (long options cannot have "glued" values like `-O2`; next CLI argument could be it) | ||
return {whole_arg.substr(2), std::nullopt}; | ||
} | ||
|
||
// not an option | ||
return {"", std::nullopt}; | ||
} |