-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathe5_advanced_cli.zig
More file actions
138 lines (117 loc) · 4.23 KB
/
e5_advanced_cli.zig
File metadata and controls
138 lines (117 loc) · 4.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const std = @import("std");
const chilli = @import("chilli");
const AppContext = struct {
log_level: u8,
start_time: i64,
};
fn rootExec(ctx: chilli.CommandContext) !void {
const is_verbose = try ctx.getFlag("verbose", bool);
std.debug.print("Advanced CLI Example\n", .{});
std.debug.print(" - Verbose mode: {}\n", .{is_verbose});
if (ctx.getContextData(AppContext)) |app_ctx| {
app_ctx.log_level = if (is_verbose) 1 else 0;
}
try ctx.command.printHelp();
}
fn addExec(ctx: chilli.CommandContext) !void {
if (try ctx.getFlag("verbose", bool)) {
const app_ctx = ctx.getContextData(AppContext).?;
std.debug.print("Running 'add' command... Log Level: {d}\n", .{app_ctx.log_level});
}
const a = try ctx.getArg("a", i64);
const b = try ctx.getArg("b", i64);
const precision = try ctx.getFlag("precision", f64);
const result = @as(f64, @floatFromInt(a)) + @as(f64, @floatFromInt(b));
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;
const precision_int: u32 = @intFromFloat(@max(0.0, @min(precision, 20.0)));
var buf: [64]u8 = undefined;
const formatted_result = try std.fmt.bufPrint(&buf, "{d:.[prec]}", .{
.num = result,
.prec = precision_int,
});
try stdout.print("Result: {s}\n", .{formatted_result});
}
fn greetExec(ctx: chilli.CommandContext) !void {
const name = try ctx.getArg("name", []const u8);
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("Hello, {s}!\n", .{name});
}
pub fn main(init: std.process.Init.Minimal) anyerror!void {
var gpa: std.heap.DebugAllocator(.{}) = .init;
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var app_context = AppContext{
.log_level = 0,
.start_time = 0,
};
var root_cmd = try chilli.Command.init(allocator, .{
.name = "comp-cli",
.description = "A comprehensive example of the Chilli framework.",
.version = "v1.2.3",
.exec = rootExec,
});
defer root_cmd.deinit();
try root_cmd.addFlag(.{
.name = "verbose",
.shortcut = 'v',
.description = "Enable verbose output",
.type = .Bool,
.default_value = .{ .Bool = false },
});
var add_cmd = try chilli.Command.init(allocator, .{
.name = "add",
.description = "Adds two integers",
.aliases = &[_][]const u8{"sum"},
.shortcut = 'a',
.exec = addExec,
});
try root_cmd.addSubcommand(add_cmd);
try add_cmd.addFlag(.{
.name = "precision",
.type = .Float,
.description = "Number of decimal places for the output",
.default_value = .{ .Float = 2.0 },
});
try add_cmd.addPositional(.{ .name = "a", .description = "First number", .is_required = true, .type = .Int });
try add_cmd.addPositional(.{ .name = "b", .description = "Second number", .is_required = true, .type = .Int });
var greet_cmd = try chilli.Command.init(allocator, .{
.name = "greet",
.description = "Prints a greeting",
.exec = greetExec,
.section = "Extra Commands",
});
try root_cmd.addSubcommand(greet_cmd);
try greet_cmd.addPositional(.{
.name = "name",
.description = "The name to greet",
.is_required = false,
.default_value = .{ .String = "World" },
});
try root_cmd.run(init.args, &app_context);
}
// Example Invocations
//
// 1. Build the example executable:
// zig build e5_advanced_cli
//
// 2. Run with different arguments:
//
// // Add two numbers
// ./zig-out/bin/e5_advanced_cli add 15 27
//
// // Use the 'sum' alias, the persistent '--verbose' flag, and the local '--precision' flag
// ./zig-out/bin/e5_advanced_cli --verbose sum 10 5.5 --precision=4
//
// // Greet the default 'World'
// ./zig-out/bin/e5_advanced_cli greet
//
// // Greet a specific person
// ./zig-out/bin/e5_advanced_cli greet Ziggy