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 1fba992
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
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 @@ 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
{

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

0 comments on commit 1fba992

Please sign in to comment.