-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathe8_flags_and_args.zig
More file actions
104 lines (89 loc) · 3.23 KB
/
e8_flags_and_args.zig
File metadata and controls
104 lines (89 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const std = @import("std");
const chilli = @import("chilli");
fn exec(ctx: chilli.CommandContext) !void {
const count = try ctx.getFlag("count", i32);
const message = try ctx.getFlag("message", []const u8);
const force = try ctx.getFlag("force", bool);
const required_arg = try ctx.getArg("required-arg", []const u8);
const optional_arg = try ctx.getArg("optional-arg", []const u8);
const variadic_args = ctx.getArgs("variadic-args");
const io = std.Options.debug_io;
var stdout_buf: [4096]u8 = undefined;
var stdout_fw = std.Io.File.stdout().writer(io, &stdout_buf);
defer stdout_fw.flush() catch {};
const stdout = &stdout_fw.interface;
try stdout.print("Flags:\n", .{});
try stdout.print(" --count: {d}\n", .{count});
try stdout.print(" --message: {s}\n", .{message});
try stdout.print(" --force: {}\n", .{force});
try stdout.print("\nArguments:\n", .{});
try stdout.print(" required-arg: {s}\n", .{required_arg});
try stdout.print(" optional-arg: {s}\n", .{optional_arg});
try stdout.print(" variadic-args: {any}\n", .{variadic_args});
}
pub fn main(init: std.process.Init.Minimal) anyerror!void {
var gpa: std.heap.DebugAllocator(.{}) = .init;
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var root_cmd = try chilli.Command.init(allocator, .{
.name = "flags-args-demo",
.description = "Demonstrates various flags and arguments.",
.exec = exec,
});
defer root_cmd.deinit();
try root_cmd.addFlag(.{
.name = "count",
.shortcut = 'c',
.description = "A number of times to do something.",
.type = .Int,
.default_value = .{ .Int = 1 },
});
try root_cmd.addFlag(.{
.name = "message",
.description = "A message to print.",
.type = .String,
.default_value = .{ .String = "default message" },
});
try root_cmd.addFlag(.{
.name = "force",
.shortcut = 'f',
.description = "Force an operation.",
.type = .Bool,
.default_value = .{ .Bool = false },
});
try root_cmd.addPositional(.{
.name = "required-arg",
.description = "A truly required argument.",
.is_required = true,
});
try root_cmd.addPositional(.{
.name = "optional-arg",
.description = "An optional argument.",
.is_required = false,
.default_value = .{ .String = "default value" },
});
try root_cmd.addPositional(.{
.name = "variadic-args",
.description = "Any number of additional arguments.",
.variadic = true,
});
try root_cmd.run(init.args, null);
}
// Example Invocations
//
// 1. Build the example executable:
// zig build e8_flags_and_args
//
// 2. Run with different arguments:
//
// // Show the help output
// ./zig-out/bin/e8_flags_and_args --help
//
// // Run with only the required argument
// ./zig-out/bin/e8_flags_and_args required_value
//
// // Run with all arguments and a mix of long and short flags
// ./zig-out/bin/e8_flags_and_args req_val opt_val --count 10 -f
//
// // Run with variadic arguments and grouped boolean flags
// ./zig-out/bin/e8_flags_and_args req_val opt_val extra1 extra2 extra3 -cf