Skip to content

Commit

Permalink
options: Add contextual value to the helper text
Browse files Browse the repository at this point in the history
  • Loading branch information
amyspark committed Jan 8, 2024
1 parent 2e09985 commit bbc3039
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
46 changes: 38 additions & 8 deletions impl/command_line/options.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -184,28 +184,58 @@ namespace substrate::commandLine
}, _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 195 in impl/command_line/options.cxx

View check run for this annotation

Codecov / codecov/patch

impl/command_line/options.cxx#L190-L195

Added lines #L190 - L195 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 202 in impl/command_line/options.cxx

View check run for this annotation

Codecov / codecov/patch

impl/command_line/options.cxx#L200-L202

Added lines #L200 - L202 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()}; },
[&typeValue](const std::string_view &option) { return std::string{option} + typeValue ; },
[&typeValue](const optionFlagPair_t &option)
{
return std::string{option._shortFlag} + ", "s + std::string{option._longFlag} + typeValue;
},
[this](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{[&]() -> size_t {
if (!takesParameter())
return 0U;
return typeToValue().length() + 1U;
}()};

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

View check run for this annotation

Codecov / codecov/patch

impl/command_line/options.cxx#L229-L231

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

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#L234-L235

Added lines #L234 - L235 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 237 in impl/command_line/options.cxx

View check run for this annotation

Codecov / codecov/patch

impl/command_line/options.cxx#L237

Added line #L237 was not covered by tests
[this](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

0 comments on commit bbc3039

Please sign in to comment.