From e73b56aa4bcb7e53144ef96ee978f2a19b32669d Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Tue, 22 Oct 2024 17:46:47 +0200 Subject: refactor: Always access using full namespace This is my new preferred style of programming Zig :) --- example/help.zig | 8 ++++---- example/simple-ex.zig | 22 +++++++++------------- example/simple.zig | 19 ++++++++----------- example/streaming-clap.zig | 22 +++++++++------------- example/usage.zig | 6 +++--- 5 files changed, 33 insertions(+), 44 deletions(-) (limited to 'example') diff --git a/example/help.zig b/example/help.zig index 2f063c5..b80ee35 100644 --- a/example/help.zig +++ b/example/help.zig @@ -1,6 +1,3 @@ -const clap = @import("clap"); -const std = @import("std"); - pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); @@ -17,9 +14,12 @@ pub fn main() !void { defer res.deinit(); // `clap.help` is a function that can print a simple help message. It can print any `Param` - // where `Id` has a `describtion` and `value` method (`Param(Help)` is one such parameter). + // where `Id` has a `description` and `value` method (`Param(Help)` is one such parameter). // The last argument contains options as to how `help` should print those parameters. Using // `.{}` means the default options. if (res.args.help != 0) return clap.help(std.io.getStdErr().writer(), clap.Help, ¶ms, .{}); } + +const clap = @import("clap"); +const std = @import("std"); diff --git a/example/simple-ex.zig b/example/simple-ex.zig index 2943f4e..4ca9791 100644 --- a/example/simple-ex.zig +++ b/example/simple-ex.zig @@ -1,10 +1,3 @@ -const clap = @import("clap"); -const std = @import("std"); - -const debug = std.debug; -const io = std.io; -const process = std.process; - pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); @@ -38,19 +31,22 @@ pub fn main() !void { // allowed. .assignment_separators = "=:", }) catch |err| { - diag.report(io.getStdErr().writer(), err) catch {}; + diag.report(std.io.getStdErr().writer(), err) catch {}; return err; }; defer res.deinit(); if (res.args.help != 0) - debug.print("--help\n", .{}); + std.debug.print("--help\n", .{}); if (res.args.number) |n| - debug.print("--number = {}\n", .{n}); + std.debug.print("--number = {}\n", .{n}); if (res.args.answer) |a| - debug.print("--answer = {s}\n", .{@tagName(a)}); + std.debug.print("--answer = {s}\n", .{@tagName(a)}); for (res.args.string) |s| - debug.print("--string = {s}\n", .{s}); + std.debug.print("--string = {s}\n", .{s}); for (res.positionals) |pos| - debug.print("{s}\n", .{pos}); + std.debug.print("{s}\n", .{pos}); } + +const clap = @import("clap"); +const std = @import("std"); diff --git a/example/simple.zig b/example/simple.zig index a7207c7..7f1bfc0 100644 --- a/example/simple.zig +++ b/example/simple.zig @@ -1,9 +1,3 @@ -const clap = @import("clap"); -const std = @import("std"); - -const debug = std.debug; -const io = std.io; - pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); @@ -27,17 +21,20 @@ pub fn main() !void { .allocator = gpa.allocator(), }) catch |err| { // Report useful error and exit - diag.report(io.getStdErr().writer(), err) catch {}; + diag.report(std.io.getStdErr().writer(), err) catch {}; return err; }; defer res.deinit(); if (res.args.help != 0) - debug.print("--help\n", .{}); + std.debug.print("--help\n", .{}); if (res.args.number) |n| - debug.print("--number = {}\n", .{n}); + std.debug.print("--number = {}\n", .{n}); for (res.args.string) |s| - debug.print("--string = {s}\n", .{s}); + std.debug.print("--string = {s}\n", .{s}); for (res.positionals) |pos| - debug.print("{s}\n", .{pos}); + std.debug.print("{s}\n", .{pos}); } + +const clap = @import("clap"); +const std = @import("std"); diff --git a/example/streaming-clap.zig b/example/streaming-clap.zig index 9d99859..2a20bcb 100644 --- a/example/streaming-clap.zig +++ b/example/streaming-clap.zig @@ -1,10 +1,3 @@ -const clap = @import("clap"); -const std = @import("std"); - -const debug = std.debug; -const io = std.io; -const process = std.process; - pub fn main() !void { const allocator = std.heap.page_allocator; @@ -22,7 +15,7 @@ pub fn main() !void { .{ .id = 'f', .takes_value = .one }, }; - var iter = try process.ArgIterator.initWithAllocator(allocator); + var iter = try std.process.ArgIterator.initWithAllocator(allocator); defer iter.deinit(); // Skip exe argument @@ -32,7 +25,7 @@ pub fn main() !void { // This is optional. You can also leave the `diagnostic` field unset if you // don't care about the extra information `Diagnostic` provides. var diag = clap.Diagnostic{}; - var parser = clap.streaming.Clap(u8, process.ArgIterator){ + var parser = clap.streaming.Clap(u8, std.process.ArgIterator){ .params = ¶ms, .iter = &iter, .diagnostic = &diag, @@ -41,19 +34,22 @@ pub fn main() !void { // Because we use a streaming parser, we have to consume each argument parsed individually. while (parser.next() catch |err| { // Report useful error and exit - diag.report(io.getStdErr().writer(), err) catch {}; + diag.report(std.io.getStdErr().writer(), err) catch {}; return err; }) |arg| { // arg.param will point to the parameter which matched the argument. switch (arg.param.id) { - 'h' => debug.print("Help!\n", .{}), - 'n' => debug.print("--number = {s}\n", .{arg.value.?}), + 'h' => std.debug.print("Help!\n", .{}), + 'n' => std.debug.print("--number = {s}\n", .{arg.value.?}), // arg.value == null, if arg.param.takes_value == .none. // Otherwise, arg.value is the value passed with the argument, such as "-a=10" // or "-a 10". - 'f' => debug.print("{s}\n", .{arg.value.?}), + 'f' => std.debug.print("{s}\n", .{arg.value.?}), else => unreachable, } } } + +const clap = @import("clap"); +const std = @import("std"); diff --git a/example/usage.zig b/example/usage.zig index a773dd2..59ac84a 100644 --- a/example/usage.zig +++ b/example/usage.zig @@ -1,6 +1,3 @@ -const clap = @import("clap"); -const std = @import("std"); - pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); @@ -22,3 +19,6 @@ pub fn main() !void { if (res.args.help != 0) return clap.usage(std.io.getStdErr().writer(), clap.Help, ¶ms); } + +const clap = @import("clap"); +const std = @import("std"); -- cgit v1.2.3