Skip to content

Commit

Permalink
feat: Introduce an ability to display proper arg's value placeholder …
Browse files Browse the repository at this point in the history
…in help

Signed-off-by: prajwalch <[email protected]>
  • Loading branch information
prajwalch committed Sep 7, 2024
1 parent b620052 commit bd7873f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 29 deletions.
11 changes: 9 additions & 2 deletions examples/ls.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,15 @@ pub fn main() anyerror!void {
try myls.addArg(Arg.booleanOption("one-line", '1', "List each entries in new line"));
try myls.addArg(Arg.booleanOption("size", 's', "Display file size"));
try myls.addArg(Arg.booleanOption("version", null, "Display program version number"));
try myls.addArg(Arg.singleValueOption("ignore", 'I', "Ignore the given pattern"));
try myls.addArg(Arg.singleValueOption("hide", null, "Don't display hidden entries"));

var ignore_opt = Arg.singleValueOption("ignore", 'I', "Ignore the given pattern");
ignore_opt.setValuePlaceholder("PATTERN");

var hide_opt = Arg.singleValueOption("hide", null, "Don't display hidden entries");
hide_opt.setValuePlaceholder("PATTERN");

try myls.addArg(ignore_opt);
try myls.addArg(hide_opt);
try myls.addArg(Arg.singleValueOptionWithValidValues(
"color",
'C',
Expand Down
37 changes: 37 additions & 0 deletions src/Arg.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ short_name: ?u8 = null,
long_name: ?[]const u8 = null,
min_values: ?usize = null,
max_values: ?usize = null,
value_placeholder: ?[]const u8 = null,
valid_values: ?[]const []const u8 = null,
values_delimiter: ?[]const u8 = null,
index: ?usize = null,
Expand Down Expand Up @@ -321,6 +322,42 @@ pub fn setMaxValues(self: *Arg, num: usize) void {
self.max_values = if (num >= 1) num else null;
}

/// Sets the placeholder for the argument value in the help message.
///
/// The placeholder is only used to display on help message and by default,
/// if the placeholder is not set, argument name is displayed.
///
/// **NOTE:** If the argument doesn't take value, placeholder is ignored.
///
/// ## Examples
///
/// ```zig
/// var app = App.init(allocator, "myapp", "My app description");
/// defer app.deinit();
///
/// var root = app.rootCommand();
///
/// var arg = Arg.singleValueOption("exp-time", 't', "Set max expire time")
/// arg.setValuePlaceholder("SECS");
///
/// root.addArg(arg);
/// ```
///
/// On command line:
///
/// ```sh
/// $ myapp -h
/// My app description
///
/// Usage: myapp [OPTIONS]
///
/// Options:
/// -t, --exp-time=<SECS> Set max expire time
/// ```
pub fn setValuePlaceholder(self: *Arg, placeholder: []const u8) void {
self.value_placeholder = placeholder;
}

/// Sets the valid values for an argument.
///
/// ## Examples
Expand Down
37 changes: 10 additions & 27 deletions src/HelpMessageWriter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -141,34 +141,12 @@ fn writeOption(self: *HelpMessageWriter, option: *const Arg) !void {

// Value name.
if (option.hasProperty(.takes_value)) {
try signature_writer.writeByte('=');
// Otherwise print the option actual value name or option name itself.
const value_name = option.value_placeholder orelse option.name;
try signature_writer.print("=<{s}>", .{value_name});

// If the option has set acceptable values, print that.
if (option.valid_values) |valid_values| {
try signature_writer.writeByte('<');

for (valid_values, 0..) |value, idx| {
try signature_writer.print("{s}", .{value});

// Don't print `|` at first and last.
//
// For e.x.: --format=<json|toml|yaml>
if (idx < (valid_values.len - 1)) {
try signature_writer.writeByte('|');
}
}

try signature_writer.writeByte('>');
} else {
// Otherwise print the option name.
//
// TODO: Add new `Arg.placeholderName()` to display correct value
// or placeholder name. For e.x.: --time=SECS.
try signature_writer.print("<{s}>", .{option.name});

if (option.hasProperty(.takes_multiple_values)) {
try signature_writer.writeAll("...");
}
if (option.hasProperty(.takes_multiple_values)) {
try signature_writer.writeAll("...");
}
}

Expand All @@ -178,6 +156,11 @@ fn writeOption(self: *HelpMessageWriter, option: *const Arg) !void {
// Then write the description.
if (option.description) |description| {
try writer.print("\t\t{s}", .{description});

if (option.valid_values) |valid_values| {
// FIXME: Remove this hacky implmentation
try writer.print("\n\t\t\t\t\t\t values: {s}", .{valid_values});
}
}

// End of line.
Expand Down

0 comments on commit bd7873f

Please sign in to comment.