From 96832abac60531acc24aa47c310a9cb21a12eece Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 14 Jan 2022 07:48:47 +0000 Subject: [PATCH] Accept --ignore-extras argument --- src/main.zig | 26 +++++++++++++++++++------- test.sh | 13 +++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/main.zig b/src/main.zig index 9a1a7a0..f5241d0 100644 --- a/src/main.zig +++ b/src/main.zig @@ -46,6 +46,7 @@ const Config = struct { file: ?fs.File, line: ?[]const u8 = null, token: ?[]const u8 = null, + ignoreExtras: bool, pub fn deinit(self: @This()) void { if (self.file) |f| { @@ -56,17 +57,18 @@ const Config = struct { fn parseArgs(allocator: mem.Allocator) !Config { const params = comptime [_]clap.Param(clap.Help) { - clap.parseParam(" The number of lines to skip") catch unreachable, - clap.parseParam("[] The file to read or stdin if not given") catch unreachable, - clap.parseParam("-l, --line Skip until N lines matching this") catch unreachable, - clap.parseParam("-t, --token Skip lines until N tokens found") catch unreachable, - clap.parseParam("-h, --help Display this help and exit") catch unreachable, - clap.parseParam("-v, --version Display the version") catch unreachable, + clap.parseParam(" The number of lines to skip") catch unreachable, + clap.parseParam("[] The file to read or stdin if not given") catch unreachable, + clap.parseParam("-l, --line Skip until N lines matching this") catch unreachable, + clap.parseParam("-t, --token Skip lines until N tokens found") catch unreachable, + clap.parseParam("-i, --ignore-extras Only count the first token on each line") catch unreachable, + clap.parseParam("-h, --help Display this help and exit") catch unreachable, + clap.parseParam("-v, --version Display the version") catch unreachable, }; var diag = clap.Diagnostic{}; var args = clap.parse(clap.Help, ¶ms, .{ .diagnostic = &diag }) catch |err| { diag.report(io.getStdErr().writer(), err) catch {}; - return error.EarlyExit; + return error.BadArgs; }; defer args.deinit(); @@ -97,6 +99,15 @@ fn parseArgs(allocator: mem.Allocator) !Config { if (args.option("--token")) |match| { token = try allocator.dupe(u8, match); } + var ignoreExtras: bool = false; + if (args.flag("--ignore-extras")) { + if (token) |_| { + ignoreExtras = true; + } else { + try io.getStdErr().writer().print("Error: --ignore-extras requires --token\n", .{}); + return error.BadArgs; + } + } var n: u32 = 0; var file: ?fs.File = null; @@ -122,6 +133,7 @@ fn parseArgs(allocator: mem.Allocator) !Config { .file = file, .line = line, .token = token, + .ignoreExtras = ignoreExtras, }; } diff --git a/test.sh b/test.sh index 06408d2..ce149ea 100755 --- a/test.sh +++ b/test.sh @@ -75,6 +75,19 @@ diff --brief test.expect.err test.err rm test.expect test.out rm test.expect.err test.err +echo "> handle ignore-extra when token is missing" +cat< test.expect.err +Error: --ignore-extras requires --token +EOF +cat< test.expect +EOF +touch test.out test.err +./skip --ignore-extras > test.out 2> test.err +diff --brief test.expect test.out +diff --brief test.expect.err test.err +rm test.expect test.out +rm test.expect.err test.err + echo "> skip lines until 3 tokens seen - ignored extra tokens on same line" cat< test.in Lorem ipsum dolor sit amet,