Skip to content

Commit

Permalink
Add function for getting the prefix of a param name
Browse files Browse the repository at this point in the history
  • Loading branch information
Hejsil committed Oct 3, 2022
1 parent 4f4196f commit c20bbf1
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions clap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,23 @@ pub const Names = struct {
}

pub const Longest = struct {
kind: enum { long, short, positinal },
kind: Kind,
name: []const u8,
};

pub const Kind = enum {
long,
short,
positinal,

pub fn prefix(l: Longest) []const u8 {
return switch (l.kind) {
.long => "--",
.short => "-",
.positional => "",
};
}
};
};

/// Whether a param takes no value (a flag), one value, or can be specified multiple times.
Expand Down Expand Up @@ -550,29 +564,19 @@ pub const Diagnostic = struct {
/// Default diagnostics reporter when all you want is English with no colors.
/// Use this as a reference for implementing your own if needed.
pub fn report(diag: Diagnostic, stream: anytype, err: anyerror) !void {
const Arg = struct {
prefix: []const u8,
name: []const u8,
};
const a = if (diag.name.short) |*c|
Arg{ .prefix = "-", .name = @as(*const [1]u8, c)[0..] }
else if (diag.name.long) |l|
Arg{ .prefix = "--", .name = l }
else
Arg{ .prefix = "", .name = diag.arg };

const longest = diag.name.longest();
switch (err) {
streaming.Error.DoesntTakeValue => try stream.print(
"The argument '{s}{s}' does not take a value\n",
.{ a.prefix, a.name },
.{ longest.kind.prefix(), longest.name },
),
streaming.Error.MissingValue => try stream.print(
"The argument '{s}{s}' requires a value but none was supplied\n",
.{ a.prefix, a.name },
.{ longest.kind.prefix(), longest.name },
),
streaming.Error.InvalidArgument => try stream.print(
"Invalid argument '{s}{s}'\n",
.{ a.prefix, a.name },
.{ longest.kind.prefix(), longest.name },
),
else => try stream.print("Error while parsing arguments: {s}\n", .{@errorName(err)}),
}
Expand Down

0 comments on commit c20bbf1

Please sign in to comment.