Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

options: Add contextual value to the helper text #82

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 47 additions & 8 deletions impl/command_line/options.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -184,28 +184,67 @@
}, _option);
}

[[nodiscard]] inline std::string_view option_t::typeToValue() const noexcept
{
switch (_valueType)
{
case optionValueType_t::signedInt:
return "INT"sv;
case optionValueType_t::unsignedInt:
return "UINT"sv;
case optionValueType_t::boolean:
return "BOOL"sv;

Check warning on line 196 in impl/command_line/options.cxx

View check run for this annotation

Codecov / codecov/patch

impl/command_line/options.cxx#L191-L196

Added lines #L191 - L196 were not covered by tests
case optionValueType_t::string:
return "STRING"sv;
case optionValueType_t::path:
return "PATH"sv;
case optionValueType_t::userDefined:
return "VAL"sv;
}

Check warning on line 203 in impl/command_line/options.cxx

View check run for this annotation

Codecov / codecov/patch

impl/command_line/options.cxx#L201-L203

Added lines #L201 - L203 were not covered by tests
}

[[nodiscard]] std::string option_t::displayName() const noexcept
{
if (_option.valueless_by_exception())
return ""s;
const auto typeValue
{
[&]() -> std::string
{
if (!takesParameter())
return {};
return " "s + std::string{typeToValue()};
}()
};
return std::visit(match_t
{
[](const std::string_view &option) { return std::string{option}; },
[](const optionFlagPair_t &option)
{ return std::string{option._shortFlag} + ", "s + std::string{option._longFlag}; },
[](const optionValue_t &option) { return std::string{option.metaName()}; },
[&](const std::string_view &option) { return std::string{option} + typeValue; },
[&](const optionFlagPair_t &option)
{
return std::string{option._shortFlag} + ", "s + std::string{option._longFlag} + typeValue;
},
[&](const optionValue_t &option) { return std::string{option.metaName()} + (isRepeatable() ? "..."s : ""s); }
}, _option);
}

[[nodiscard]] size_t option_t::displayLength() const noexcept
{
const auto value_length

Check warning on line 232 in impl/command_line/options.cxx

View check run for this annotation

Codecov / codecov/patch

impl/command_line/options.cxx#L232

Added line #L232 was not covered by tests
{
[&]() -> size_t
{

Check warning on line 235 in impl/command_line/options.cxx

View check run for this annotation

Codecov / codecov/patch

impl/command_line/options.cxx#L235

Added line #L235 was not covered by tests
if (!takesParameter())
return 0U;
return typeToValue().length() + 1U;
}()
};

Check warning on line 240 in impl/command_line/options.cxx

View check run for this annotation

Codecov / codecov/patch

impl/command_line/options.cxx#L237-L240

Added lines #L237 - L240 were not covered by tests
return std::visit(match_t
{
[](const std::string_view &option) { return option.length(); },
[](const optionFlagPair_t &option)
[&](const std::string_view &option) { return option.length() + value_length; },
[&](const optionFlagPair_t &option)

Check warning on line 244 in impl/command_line/options.cxx

View check run for this annotation

Codecov / codecov/patch

impl/command_line/options.cxx#L243-L244

Added lines #L243 - L244 were not covered by tests
// Add the lengths of the two flags together, and the extra ", " that is inserted by displayName()
{ return option._shortFlag.length() + option._longFlag.length() + 2U; },
[](const optionValue_t &option) { return option.metaName().length(); },
{ return option._shortFlag.length() + option._longFlag.length() + 2U + value_length; },

Check warning on line 246 in impl/command_line/options.cxx

View check run for this annotation

Codecov / codecov/patch

impl/command_line/options.cxx#L246

Added line #L246 was not covered by tests
[&](const optionValue_t &option) { return option.metaName().length() + (isRepeatable() ? 3U : 0U); },
}, _option);
}

Expand Down
1 change: 1 addition & 0 deletions substrate/command_line/options
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ namespace substrate::commandLine
}

private:
[[nodiscard]] std::string_view typeToValue() const noexcept;
[[nodiscard]] std::optional<std::any> parseSignedValue(const std::string_view &value) const noexcept;
[[nodiscard]] std::optional<std::any> parseUnsignedValue(const std::string_view &value) const noexcept;
[[nodiscard]] static std::optional<std::any> parseBoolValue(const std::string_view &value) noexcept;
Expand Down
Loading