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 7, 2024
1 parent 2e09985 commit 970949a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
43 changes: 35 additions & 8 deletions impl/command_line/options.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -184,28 +184,55 @@ namespace substrate::commandLine
}, _option);
}

[[nodiscard]] inline std::string_view option_t::typeToValue() const noexcept
{
if (!takesParameter())
return {};
switch (_valueType){
case optionValueType_t::signedInt:
return "INT"sv;
case optionValueType_t::unsignedInt:
return "UINT"sv;
case optionValueType_t::boolean:
return "BOOL"sv;
case optionValueType_t::string:
return "STRING"sv;
case optionValueType_t::path:
return "PATH"sv;
case optionValueType_t::userDefined:
return "VAL"sv;
}
}

[[nodiscard]] std::string option_t::displayName() const noexcept
{
if (_option.valueless_by_exception())
return ""s;
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} + " "s + std::string{typeToValue()}; },
[&](const optionFlagPair_t &option)
{
return std::string{option._shortFlag} + ", "s + std::string{option._longFlag} + " "s + std::string{typeToValue()};
},
[&](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;
}()};
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)
// 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; },
[&](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 970949a

Please sign in to comment.