Skip to content

Commit

Permalink
Updated Usage Message f/ Options.
Browse files Browse the repository at this point in the history
- Added the `alias_fmt` field to the Option's config to allow Option Long Name Aliases to be shown inline with the Option's Usage Message.
  • Loading branch information
00JCIV00 committed Dec 3, 2024
1 parent d6f87c1 commit 8f12e48
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions src/Option.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

const std = @import("std");
const ascii = std.ascii;
const fmt = std.fmt;
const mem = std.mem;
const meta = std.meta;

Expand Down Expand Up @@ -88,13 +89,21 @@ pub const Config = struct {
/// Must support the following format types in this order:
/// 1. Character (Short Prefix)
/// 2. Optional Character "{?u}" (Short Name)
/// 3. String Name Separator
/// 3. String (Name Separator)
/// 4. String (Long Prefix)
/// 5. Optional String "{?s}" (Long Name)
/// 6. String (Value Type)
/// 6. String (Alias Long Names)
/// 7. String (Value Type)
///
/// Note, a comma "," will automatically be placed between the short and long name if they both exist.
usage_fmt: []const u8 = "{u}{?u}{s}{s}{?s} <{s}>",
usage_fmt: []const u8 = "{u}{?u}{s}{s}{?s}{s} <{s}>",
/// Format for each Alias Long Name in the Usage message.
///
/// Must support the following format types in this order:
/// 1. String (Name Separator)
/// 2. String (Long Prefix)
/// 3. String "{s}" (Alias Long Name)
alias_fmt: []const u8 = "{s}{s}{s}",

/// Prefix for Short Options.
short_prefix: ?u8 = '-',
Expand Down Expand Up @@ -165,6 +174,9 @@ pub fn Custom(comptime config: Config) type {
/// Usage Format.
/// Check `Options.Config` for details.
pub const usage_fmt = config.usage_fmt;
/// Long Name Aliases Format.
/// Check `Options.Config` for details.
pub const alias_fmt = config.alias_fmt;

/// Short Prefix.
/// Check `Options.Config` for details.
Expand Down Expand Up @@ -328,12 +340,31 @@ pub fn Custom(comptime config: Config) type {
}) |usageFn| return usageFn(self, writer, self._alloc);
if (global_usage_fn) |usageFn| return usageFn(self, writer, self._alloc);

const AliasFormatter = struct {
aliases: ?[]const []const u8,

pub fn format(
formatter: @This(),
_: []const u8,
_: fmt.FormatOptions,
fmt_writer: anytype,
) !void {
if (formatter.aliases == null or formatter.aliases.?.len == 0) {
try fmt_writer.print("", .{});
return;
}
for (formatter.aliases.?) |alias|
try fmt_writer.print(alias_fmt, .{ name_sep_fmt, long_prefix.?, alias });
}
};

try writer.print(usage_fmt, .{
@as(u21, if (self.short_name != null) short_prefix orelse 0x200B else 0x200B),
@as(u21, if (short_prefix != null) self.short_name orelse 0x200B else 0x200B),
if (self.short_name != null and self.long_name != null) name_sep_fmt else "",
if (long_prefix != null and self.long_name != null) long_prefix.? else "",
if (long_prefix != null) self.long_name orelse "" else "",
AliasFormatter{ .aliases = self.alias_long_names },
//self.val.name(),
self.val.childTypeName(),
});
Expand Down

0 comments on commit 8f12e48

Please sign in to comment.