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 9, 2024
1 parent 29962af commit 23ef023
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
54 changes: 46 additions & 8 deletions impl/command_line/options.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -184,28 +184,66 @@ 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 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 {
if (!takesParameter())
return 0U;
return typeToValue().length() + 1U;
}()
};

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

View check run for this annotation

Codecov / codecov/patch

impl/command_line/options.cxx#L236-L239

Added lines #L236 - L239 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 243 in impl/command_line/options.cxx

View check run for this annotation

Codecov / codecov/patch

impl/command_line/options.cxx#L242-L243

Added lines #L242 - L243 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 245 in impl/command_line/options.cxx

View check run for this annotation

Codecov / codecov/patch

impl/command_line/options.cxx#L245

Added line #L245 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 23ef023

Please sign in to comment.