summaryrefslogtreecommitdiff
path: root/clap.zig
diff options
context:
space:
mode:
authorGravatar Jimmi Holst Christensen2022-10-03 12:45:55 +0200
committerGravatar Jimmi Holst Christensen2022-10-03 12:46:43 +0200
commitc20bbf13df33d82d27b1d70628d90fe0b489d07e (patch)
tree323db3770f065d6f1dc8b1eaf87f62da74f95b9c /clap.zig
parentuse builtin.Type instead of deprecated builtin.TypeInfo (diff)
downloadzig-clap-c20bbf13df33d82d27b1d70628d90fe0b489d07e.tar.gz
zig-clap-c20bbf13df33d82d27b1d70628d90fe0b489d07e.tar.xz
zig-clap-c20bbf13df33d82d27b1d70628d90fe0b489d07e.zip
Add function for getting the prefix of a param name
Diffstat (limited to 'clap.zig')
-rw-r--r--clap.zig34
1 files changed, 19 insertions, 15 deletions
diff --git a/clap.zig b/clap.zig
index 05fa723..9c3b6d1 100644
--- a/clap.zig
+++ b/clap.zig
@@ -40,9 +40,23 @@ pub const Names = struct {
40 } 40 }
41 41
42 pub const Longest = struct { 42 pub const Longest = struct {
43 kind: enum { long, short, positinal }, 43 kind: Kind,
44 name: []const u8, 44 name: []const u8,
45 }; 45 };
46
47 pub const Kind = enum {
48 long,
49 short,
50 positinal,
51
52 pub fn prefix(l: Longest) []const u8 {
53 return switch (l.kind) {
54 .long => "--",
55 .short => "-",
56 .positional => "",
57 };
58 }
59 };
46}; 60};
47 61
48/// Whether a param takes no value (a flag), one value, or can be specified multiple times. 62/// Whether a param takes no value (a flag), one value, or can be specified multiple times.
@@ -550,29 +564,19 @@ pub const Diagnostic = struct {
550 /// Default diagnostics reporter when all you want is English with no colors. 564 /// Default diagnostics reporter when all you want is English with no colors.
551 /// Use this as a reference for implementing your own if needed. 565 /// Use this as a reference for implementing your own if needed.
552 pub fn report(diag: Diagnostic, stream: anytype, err: anyerror) !void { 566 pub fn report(diag: Diagnostic, stream: anytype, err: anyerror) !void {
553 const Arg = struct { 567 const longest = diag.name.longest();
554 prefix: []const u8,
555 name: []const u8,
556 };
557 const a = if (diag.name.short) |*c|
558 Arg{ .prefix = "-", .name = @as(*const [1]u8, c)[0..] }
559 else if (diag.name.long) |l|
560 Arg{ .prefix = "--", .name = l }
561 else
562 Arg{ .prefix = "", .name = diag.arg };
563
564 switch (err) { 568 switch (err) {
565 streaming.Error.DoesntTakeValue => try stream.print( 569 streaming.Error.DoesntTakeValue => try stream.print(
566 "The argument '{s}{s}' does not take a value\n", 570 "The argument '{s}{s}' does not take a value\n",
567 .{ a.prefix, a.name }, 571 .{ longest.kind.prefix(), longest.name },
568 ), 572 ),
569 streaming.Error.MissingValue => try stream.print( 573 streaming.Error.MissingValue => try stream.print(
570 "The argument '{s}{s}' requires a value but none was supplied\n", 574 "The argument '{s}{s}' requires a value but none was supplied\n",
571 .{ a.prefix, a.name }, 575 .{ longest.kind.prefix(), longest.name },
572 ), 576 ),
573 streaming.Error.InvalidArgument => try stream.print( 577 streaming.Error.InvalidArgument => try stream.print(
574 "Invalid argument '{s}{s}'\n", 578 "Invalid argument '{s}{s}'\n",
575 .{ a.prefix, a.name }, 579 .{ longest.kind.prefix(), longest.name },
576 ), 580 ),
577 else => try stream.print("Error while parsing arguments: {s}\n", .{@errorName(err)}), 581 else => try stream.print("Error while parsing arguments: {s}\n", .{@errorName(err)}),
578 } 582 }