Skip to content
Closed
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
49 changes: 32 additions & 17 deletions include/rfl/cli/read_app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ inline rfl::Result<bool> take_builtin_flag(
/// instead of reading the struct.
template <class T, class... Ps, class ParseFn>
rfl::Result<std::variant<T, HelpOrVersion>> parse_and_read_app(
const std::string& _program_name, const ParseFn& _parse) {
const std::string& _program_name, const ParseFn& _parse,
const UsageOptions& _usage_opts = UsageOptions{.detailed = true}) {
using ProcessorsType = Processors<SnakeCaseToKebabCase, Ps...>;
using ResultType = std::variant<T, HelpOrVersion>;
auto options = make_options<T, ProcessorsType>();
Expand Down Expand Up @@ -78,7 +79,7 @@ rfl::Result<std::variant<T, HelpOrVersion>> parse_and_read_app(
}

if (*help || *help_short) {
return usage<T, Ps...>(_program_name, true)
return usage<T, Ps...>(_program_name, _usage_opts)
.transform([](std::string _text) {
return ResultType(
HelpOrVersion{HelpOrVersion::Kind::help, std::move(_text)});
Expand Down Expand Up @@ -135,36 +136,45 @@ inline std::string program_name(int argc, const wchar_t* const argv[]) {
/// has no name.
/// @return The struct, or the help or version text, or an error
template <class T, class... Ps>
rfl::Result<std::variant<T, HelpOrVersion>> read_app(int argc,
const char* const argv[]) {
rfl::Result<std::variant<T, HelpOrVersion>> read_app(
int argc, const char* const argv[],
const UsageOptions& _usage_opts = UsageOptions{.detailed = true}) {
return internal::parse_and_read_app<T, Ps...>(
internal::program_name(argc, argv),
[&](const auto& _options) {
return internal::parse_argv(argc, argv, _options);
});
},
_usage_opts);
}

template <class T, class... Ps>
rfl::Result<std::variant<T, HelpOrVersion>> read_app(int argc, char* argv[]) {
return read_app<T, Ps...>(argc, static_cast<const char* const*>(argv));
rfl::Result<std::variant<T, HelpOrVersion>> read_app(
int argc, char* argv[],
const UsageOptions& _usage_opts = UsageOptions{.detailed = true}) {
return read_app<T, Ps...>(argc, static_cast<const char* const*>(argv),
_usage_opts);
}

/// Like rfl::cli::read_app, for wide-character arguments, which are
/// converted to UTF-8 first.
template <class T, class... Ps>
rfl::Result<std::variant<T, HelpOrVersion>> read_app(
int argc, const wchar_t* const argv[]) {
int argc, const wchar_t* const argv[],
const UsageOptions& _usage_opts = UsageOptions{.detailed = true}) {
return internal::parse_and_read_app<T, Ps...>(
internal::program_name(argc, argv),
[&](const auto& _options) {
return internal::parse_argv(argc, argv, _options);
});
},
_usage_opts);
}

template <class T, class... Ps>
rfl::Result<std::variant<T, HelpOrVersion>> read_app(int argc,
wchar_t* argv[]) {
return read_app<T, Ps...>(argc, static_cast<const wchar_t* const*>(argv));
rfl::Result<std::variant<T, HelpOrVersion>> read_app(
int argc, wchar_t* argv[],
const UsageOptions& _usage_opts = UsageOptions{.detailed = true}) {
return read_app<T, Ps...>(argc, static_cast<const wchar_t* const*>(argv),
_usage_opts);
}

/// Like rfl::cli::read_app, reading every element of a range of narrow or
Expand All @@ -174,22 +184,27 @@ template <class T, class... Ps,
class R = std::initializer_list<std::string_view>>
requires(internal::StringRange<R> || internal::WideStringRange<R>)
rfl::Result<std::variant<T, HelpOrVersion>> read_app(
std::string_view _program_name, R&& _args) {
std::string_view _program_name, R&& _args,
const UsageOptions& _usage_opts = UsageOptions{.detailed = true}) {
return internal::parse_and_read_app<T, Ps...>(
std::string(_program_name), [&](const auto& _options) {
std::string(_program_name),
[&](const auto& _options) {
return internal::parse_argv(std::forward<R>(_args), _options);
});
},
_usage_opts);
}

/// Like rfl::cli::read_app, reading every element of a range of narrow or
/// wide strings, using the rfl::cli::App's name as the program name.
template <class T, class... Ps,
class R = std::initializer_list<std::string_view>>
requires(internal::StringRange<R> || internal::WideStringRange<R>)
rfl::Result<std::variant<T, HelpOrVersion>> read_app(R&& _args) {
rfl::Result<std::variant<T, HelpOrVersion>> read_app(
R&& _args,
const UsageOptions& _usage_opts = UsageOptions{.detailed = true}) {
using ProcessorsType = Processors<SnakeCaseToKebabCase, Ps...>;
return read_app<T, Ps...>(internal::app_settings_t<ProcessorsType>::name,
std::forward<R>(_args));
std::forward<R>(_args), _usage_opts);
}

} // namespace rfl::cli
Expand Down
185 changes: 142 additions & 43 deletions include/rfl/cli/usage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@

namespace rfl::cli {

/// Runtime customization for `rfl::cli::usage` and `rfl::cli::read_app`.
struct UsageOptions {
bool detailed = true;
/// If non-empty, shown before the Usage line in detailed output (overrides
/// any header from rfl::cli::App).
std::string header = {};
/// If non-empty, replaces the auto-generated synopsis after "Usage: ".
std::string synopsis = {};
/// If non-empty, shown after the Usage line in detailed output.
std::string description = {};
/// If non-empty, appended after a blank line at the end of detailed output.
std::string footer = {};
/// If non-empty, places built-in `--help` and `--version` in this group
/// (at the start of the group if fields already use it).
std::string help_group = {};
/// Maximum left-column width used to align option descriptions; options
/// wider than this place their description on the next line instead of
/// widening every other row. Set to 0 for no cap.
size_t max_option_width = 40;
/// Group names to omit from generated detailed help output (e.g. conditional
/// feature groups hidden at runtime).
std::vector<std::string> hidden_groups = {};
};

namespace internal {

/// Whether a top-level field is required, which the schema cannot tell the
Expand Down Expand Up @@ -624,17 +648,19 @@ inline std::string usage_help_text(const UsageArg& _arg) {

/// The rows of the built-in options of rfl::cli::App that no field takes.
inline std::vector<UsageArg> builtin_usage_args(
const BuiltinOptions& _builtin) {
const BuiltinOptions& _builtin,
const std::string& _help_group = std::string()) {
auto args = std::vector<UsageArg>();
const auto make = [](std::string_view _name, std::string_view _short_name,
std::string _description) {
const auto make = [&](std::string_view _name, std::string_view _short_name,
std::string _description) {
auto arg = UsageArg{};
arg.name = std::string(_name);
arg.short_name = std::string(_short_name);
arg.kind = OptionKind::flag;
arg.bare = true;
arg.required = false;
arg.description = std::move(_description);
arg.group = _help_group;
return arg;
};
if (_builtin.help) {
Expand All @@ -654,22 +680,47 @@ inline std::vector<UsageArg> builtin_usage_args(
/// argument (rfl::cli::OptionsFirst), so the synopsis lists them first.
inline std::string format_usage(const std::string& _program_name,
const std::vector<UsageArg>& _args,
const bool _detailed,
const UsageOptions& _opts,
const std::optional<char> _delimiter,
const bool _options_first,
const std::string& _header) {
auto synopsis = _args;
if (_options_first) {
std::stable_partition(synopsis.begin(), synopsis.end(),
[](const UsageArg& _arg) { return !_arg.positional; });
}
auto out = (_detailed ? _header : std::string()) + "Usage: " + _program_name;
for (const auto& arg : synopsis) {
const auto str = usage_arg_to_string(arg, "|", _delimiter);
out += arg.required ? " " + str : " [" + str + "]";
}
out += '\n';
if (!_detailed || _args.empty()) {
auto out = std::string();
if (_opts.detailed) {
if (!_opts.header.empty()) {
out += _opts.header;
if (!out.ends_with('\n')) {
out += '\n';
}
out += '\n';
} else {
out += _header;
}
}
if (!_opts.synopsis.empty()) {
out += "Usage: " + _opts.synopsis + "\n";
} else {
auto synopsis = _args;
if (_options_first) {
std::stable_partition(synopsis.begin(), synopsis.end(),
[](const UsageArg& _arg) { return !_arg.positional; });
}
out += "Usage: " + _program_name;
for (const auto& arg : synopsis) {
const auto str = usage_arg_to_string(arg, "|", _delimiter);
out += arg.required ? " " + str : " [" + str + "]";
}
out += '\n';
}
if (!_opts.detailed) {
return out;
}
if (!_opts.description.empty()) {
out += "\n" + _opts.description + "\n";
}
if (_args.empty()) {
if (!_opts.footer.empty()) {
out += "\n" + _opts.footer + "\n";
}
return out;
}

Expand All @@ -678,7 +729,12 @@ inline std::string format_usage(const std::string& _program_name,
for (const auto& arg : _args) {
rows.emplace_back(usage_arg_to_string(arg, ", ", _delimiter),
usage_help_text(arg));
width = std::max(width, rows.back().first.size());
const size_t row_w = rows.back().first.size();
if (_opts.max_option_width == 0 || row_w <= _opts.max_option_width) {
width = std::max(width, row_w);
} else {
width = std::max(width, _opts.max_option_width);
}
}

constexpr size_t indent = 2;
Expand All @@ -688,7 +744,11 @@ inline std::string format_usage(const std::string& _program_name,
const std::string& _right) {
out += std::string(indent, ' ') + _left;
if (!_right.empty()) {
out += std::string(width - _left.size() + gap, ' ');
if (_left.size() <= width) {
out += std::string(width - _left.size() + gap, ' ');
} else {
out += continuation;
}
for (const char c : _right) {
if (c == '\n') {
out += continuation;
Expand All @@ -709,30 +769,32 @@ inline std::string format_usage(const std::string& _program_name,
for (const auto& [left, right] : rows) {
emit_row(left, right);
}
return out;
}

// Emit sections in order of first appearance; ungrouped args (if any) go
// under "Arguments".
std::vector<std::string> section_order;
for (const auto& arg : _args) {
const std::string section =
arg.group.empty() ? std::string("Arguments") : arg.group;
if (std::find(section_order.begin(), section_order.end(), section)
== section_order.end()) {
section_order.push_back(section);
} else {
// Emit sections in order of first appearance; ungrouped args (if any) go
// under "Arguments".
std::vector<std::string> section_order;
for (const auto& arg : _args) {
const std::string section =
arg.group.empty() ? std::string("Arguments") : arg.group;
if (std::find(section_order.begin(), section_order.end(), section)
== section_order.end()) {
section_order.push_back(section);
}
}
}
for (const auto& section : section_order) {
out += "\n" + section + ":\n";
for (size_t i = 0; i < _args.size(); ++i) {
const std::string arg_section =
_args[i].group.empty() ? std::string("Arguments") : _args[i].group;
if (arg_section == section) {
emit_row(rows[i].first, rows[i].second);
for (const auto& section : section_order) {
out += "\n" + section + ":\n";
for (size_t i = 0; i < _args.size(); ++i) {
const std::string arg_section =
_args[i].group.empty() ? std::string("Arguments") : _args[i].group;
if (arg_section == section) {
emit_row(rows[i].first, rows[i].second);
}
}
}
}
if (!_opts.footer.empty()) {
out += "\n" + _opts.footer + "\n";
}
return out;
}

Expand Down Expand Up @@ -781,7 +843,7 @@ std::optional<Generic> usage_defaults() {
/// rfl::DefaultVal and default-constructing it throws.
template <class T, class... Ps>
rfl::Result<std::string> usage(const std::string& _program_name,
const bool _detailed = false) {
const UsageOptions& _opts) {
using ProcessorsType = Processors<SnakeCaseToKebabCase, Ps...>;
using NamedTupleType = rfl::internal::processed_t<T, ProcessorsType>;
constexpr auto delimiter = internal::list_delimiter_v<ProcessorsType>;
Expand All @@ -806,20 +868,57 @@ rfl::Result<std::string> usage(const std::string& _program_name,
auto collector = internal::UsageCollector(
&definition, &meta, &options,
rfl::internal::no_optionals_v<ProcessorsType>, delimiter);
auto args = internal::builtin_usage_args(builtin);
const auto fields = collector.collect(
auto builtin_args =
internal::builtin_usage_args(builtin, _opts.help_group);
auto fields = collector.collect(
rfl::internal::default_if_missing_v<ProcessorsType>,
internal::usage_defaults<T, Ps...>());
args.insert(args.end(), fields.begin(), fields.end());
auto args = std::vector<internal::UsageArg>();
if (!_opts.help_group.empty() && !builtin_args.empty()) {
const auto pos = std::find_if(
fields.begin(), fields.end(),
[&](const internal::UsageArg& _a) {
return _a.group == _opts.help_group;
});
if (pos != fields.end()) {
args.insert(args.end(), fields.begin(), pos);
args.insert(args.end(), builtin_args.begin(), builtin_args.end());
args.insert(args.end(), pos, fields.end());
} else {
args = std::move(builtin_args);
args.insert(args.end(), fields.begin(), fields.end());
}
} else {
args = std::move(builtin_args);
args.insert(args.end(), fields.begin(), fields.end());
}
if (!_opts.hidden_groups.empty()) {
args.erase(
std::remove_if(
args.begin(), args.end(),
[&](const internal::UsageArg& _a) {
return !_a.group.empty() &&
std::find(_opts.hidden_groups.begin(),
_opts.hidden_groups.end(),
_a.group) != _opts.hidden_groups.end();
}),
args.end());
}
return internal::format_usage(
_program_name, args, _detailed, delimiter, options.options_first,
_program_name, args, _opts, delimiter, options.options_first,
internal::app_header<ProcessorsType>(_program_name));
} catch (std::exception& e) {
return error(e.what());
}
});
}

template <class T, class... Ps>
rfl::Result<std::string> usage(const std::string& _program_name,
const bool _detailed = false) {
return usage<T, Ps...>(_program_name, UsageOptions{.detailed = _detailed});
}

} // namespace rfl::cli

#endif
Loading