Skip to content

Commit

Permalink
Stay under 100 chars per line in all the code
Browse files Browse the repository at this point in the history
  • Loading branch information
Hejsil committed Jun 24, 2021
1 parent fb2ee06 commit 9db6e56
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 41 deletions.
120 changes: 87 additions & 33 deletions clap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ pub const Values = enum {
/// * "-abc value"
/// * "-abc=value"
/// * "-abcvalue"
/// * Long ("--long-param"): Should be used for less common parameters, or when no single character
/// can describe the paramter.
/// * Long ("--long-param"): Should be used for less common parameters, or when no single
/// character can describe the paramter.
/// * They can take a value two different ways.
/// * "--long-param value"
/// * "--long-param=value"
Expand Down Expand Up @@ -229,9 +229,18 @@ pub const Diagnostic = struct {
Arg{ .prefix = "", .name = diag.arg };

switch (err) {
error.DoesntTakeValue => try stream.print("The argument '{s}{s}' does not take a value\n", .{ a.prefix, a.name }),
error.MissingValue => try stream.print("The argument '{s}{s}' requires a value but none was supplied\n", .{ a.prefix, a.name }),
error.InvalidArgument => try stream.print("Invalid argument '{s}{s}'\n", .{ a.prefix, a.name }),
error.DoesntTakeValue => try stream.print(
"The argument '{s}{s}' does not take a value\n",
.{ a.prefix, a.name },
),
error.MissingValue => try stream.print(
"The argument '{s}{s}' requires a value but none was supplied\n",
.{ a.prefix, a.name },
),
error.InvalidArgument => try stream.print(
"Invalid argument '{s}{s}'\n",
.{ a.prefix, a.name },
),
else => try stream.print("Error while parsing arguments: {s}\n", .{@errorName(err)}),
}
}
Expand All @@ -246,15 +255,51 @@ fn testDiag(diag: Diagnostic, err: anyerror, expected: []const u8) !void {

test "Diagnostic.report" {
try testDiag(.{ .arg = "c" }, error.InvalidArgument, "Invalid argument 'c'\n");
try testDiag(.{ .name = .{ .long = "cc" } }, error.InvalidArgument, "Invalid argument '--cc'\n");
try testDiag(.{ .name = .{ .short = 'c' } }, error.DoesntTakeValue, "The argument '-c' does not take a value\n");
try testDiag(.{ .name = .{ .long = "cc" } }, error.DoesntTakeValue, "The argument '--cc' does not take a value\n");
try testDiag(.{ .name = .{ .short = 'c' } }, error.MissingValue, "The argument '-c' requires a value but none was supplied\n");
try testDiag(.{ .name = .{ .long = "cc" } }, error.MissingValue, "The argument '--cc' requires a value but none was supplied\n");
try testDiag(.{ .name = .{ .short = 'c' } }, error.InvalidArgument, "Invalid argument '-c'\n");
try testDiag(.{ .name = .{ .long = "cc" } }, error.InvalidArgument, "Invalid argument '--cc'\n");
try testDiag(.{ .name = .{ .short = 'c' } }, error.SomethingElse, "Error while parsing arguments: SomethingElse\n");
try testDiag(.{ .name = .{ .long = "cc" } }, error.SomethingElse, "Error while parsing arguments: SomethingElse\n");
try testDiag(
.{ .name = .{ .long = "cc" } },
error.InvalidArgument,
"Invalid argument '--cc'\n",
);
try testDiag(
.{ .name = .{ .short = 'c' } },
error.DoesntTakeValue,
"The argument '-c' does not take a value\n",
);
try testDiag(
.{ .name = .{ .long = "cc" } },
error.DoesntTakeValue,
"The argument '--cc' does not take a value\n",
);
try testDiag(
.{ .name = .{ .short = 'c' } },
error.MissingValue,
"The argument '-c' requires a value but none was supplied\n",
);
try testDiag(
.{ .name = .{ .long = "cc" } },
error.MissingValue,
"The argument '--cc' requires a value but none was supplied\n",
);
try testDiag(
.{ .name = .{ .short = 'c' } },
error.InvalidArgument,
"Invalid argument '-c'\n",
);
try testDiag(
.{ .name = .{ .long = "cc" } },
error.InvalidArgument,
"Invalid argument '--cc'\n",
);
try testDiag(
.{ .name = .{ .short = 'c' } },
error.SomethingElse,
"Error while parsing arguments: SomethingElse\n",
);
try testDiag(
.{ .name = .{ .long = "cc" } },
error.SomethingElse,
"Error while parsing arguments: SomethingElse\n",
);
}

pub fn Args(comptime Id: type, comptime params: []const Param(Id)) type {
Expand Down Expand Up @@ -466,7 +511,9 @@ test "clap.help" {
parseParam("-c, --cc Both flag.") catch unreachable,
parseParam("-d, --dd <V3> Both option.") catch unreachable,
parseParam("-d, --dd <V3>... Both repeated option.") catch unreachable,
parseParam("<P> Positional. This should not appear in the help message.") catch unreachable,
parseParam(
"<P> Positional. This should not appear in the help message.",
) catch unreachable,
},
);

Expand Down Expand Up @@ -516,12 +563,16 @@ pub fn usageFull(

const prefix = if (param.names.short) |_| "-" else "--";

// Seems the zig compiler is being a little wierd. I doesn't allow me to write
// @as(*const [1]u8, s) VVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
const name = if (param.names.short) |*s| @ptrCast([*]const u8, s)[0..1] else param.names.long orelse {
positional = param;
continue;
};
const name = if (param.names.short) |*s|
// Seems the zig compiler is being a little wierd. I doesn't allow me to write
// @as(*const [1]u8, s)
@ptrCast([*]const u8, s)[0..1]
else
param.names.long orelse {
positional = param;
continue;
};

if (cos.bytes_written != 0)
try cs.writeByte(' ');

Expand Down Expand Up @@ -601,16 +652,19 @@ test "usage" {
try testUsage("<file>", &.{
try parseParam("<file>"),
});
try testUsage("[-ab] [-c <value>] [-d <v>] [--e] [--f] [--g <value>] [--h <v>] [-i <v>...] <file>", &.{
try parseParam("-a"),
try parseParam("-b"),
try parseParam("-c <value>"),
try parseParam("-d <v>"),
try parseParam("--e"),
try parseParam("--f"),
try parseParam("--g <value>"),
try parseParam("--h <v>"),
try parseParam("-i <v>..."),
try parseParam("<file>"),
});
try testUsage(
"[-ab] [-c <value>] [-d <v>] [--e] [--f] [--g <value>] [--h <v>] [-i <v>...] <file>",
&.{
try parseParam("-a"),
try parseParam("-b"),
try parseParam("-c <value>"),
try parseParam("-d <v>"),
try parseParam("--e"),
try parseParam("--f"),
try parseParam("--g <value>"),
try parseParam("--h <v>"),
try parseParam("-i <v>..."),
try parseParam("<file>"),
},
);
}
7 changes: 6 additions & 1 deletion clap/args.zig
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,12 @@ pub const ShellIterator = struct {
}
}

fn result(iter: *ShellIterator, start: usize, end: usize, list: *std.ArrayList(u8)) Error!?[]const u8 {
fn result(
iter: *ShellIterator,
start: usize,
end: usize,
list: *std.ArrayList(u8),
) Error!?[]const u8 {
const res = iter.str[start..end];

// If we already have something in `list` that means that we could not
Expand Down
3 changes: 1 addition & 2 deletions clap/comptime.zig
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ pub fn ComptimeClap(
pub fn parse(iter: anytype, opt: clap.ParseOptions) !@This() {
const allocator = opt.allocator;
var multis = [_]std.ArrayList([]const u8){undefined} ** multi_options;
for (multis) |*multi| {
for (multis) |*multi|
multi.* = std.ArrayList([]const u8).init(allocator);
}

var pos = std.ArrayList([]const u8).init(allocator);

Expand Down
22 changes: 17 additions & 5 deletions clap/streaming.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ pub fn Arg(comptime Id: type) type {
}

/// A command line argument parser which, given an ArgIterator, will parse arguments according
/// to the params. StreamingClap parses in an iterating manner, so you have to use a loop together with
/// StreamingClap.next to parse all the arguments of your program.
/// to the params. StreamingClap parses in an iterating manner, so you have to use a loop
/// together with StreamingClap.next to parse all the arguments of your program.
pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type {
return struct {
const State = union(enum) {
Expand Down Expand Up @@ -203,7 +203,11 @@ pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type {
};
}

fn testNoErr(params: []const clap.Param(u8), args_strings: []const []const u8, results: []const Arg(u8)) !void {
fn testNoErr(
params: []const clap.Param(u8),
args_strings: []const []const u8,
results: []const Arg(u8),
) !void {
var iter = args.SliceIterator{ .args = args_strings };
var c = StreamingClap(u8, args.SliceIterator){
.params = params,
Expand All @@ -225,7 +229,11 @@ fn testNoErr(params: []const clap.Param(u8), args_strings: []const []const u8, r
return error.TestFailed;
}

fn testErr(params: []const clap.Param(u8), args_strings: []const []const u8, expected: []const u8) !void {
fn testErr(
params: []const clap.Param(u8),
args_strings: []const []const u8,
expected: []const u8,
) !void {
var diag: clap.Diagnostic = undefined;
var iter = args.SliceIterator{ .args = args_strings };
var c = StreamingClap(u8, args.SliceIterator){
Expand Down Expand Up @@ -420,5 +428,9 @@ test "errors" {
try testErr(&params, &.{"-a=1"}, "The argument '-a' does not take a value\n");
try testErr(&params, &.{"--aa=1"}, "The argument '--aa' does not take a value\n");
try testErr(&params, &.{"-c"}, "The argument '-c' requires a value but none was supplied\n");
try testErr(&params, &.{"--cc"}, "The argument '--cc' requires a value but none was supplied\n");
try testErr(
&params,
&.{"--cc"},
"The argument '--cc' requires a value but none was supplied\n",
);
}

0 comments on commit 9db6e56

Please sign in to comment.