-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathe7_calculator.zig
More file actions
109 lines (96 loc) · 3.42 KB
/
e7_calculator.zig
File metadata and controls
109 lines (96 loc) · 3.42 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
const std = @import("std");
const chilli = @import("chilli");
const Command = chilli.Command;
const CommandOptions = chilli.CommandOptions;
const CommandContext = chilli.CommandContext;
const PositionalArg = chilli.PositionalArg;
fn addExec(ctx: CommandContext) !void {
const a = try ctx.getArg("a", f64);
const b = try ctx.getArg("b", f64);
const result = a + 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;
try stdout.print("{d} + {d} = {d}\n", .{ a, b, result });
}
fn subtractExec(ctx: CommandContext) !void {
const a = try ctx.getArg("a", f64);
const b = try ctx.getArg("b", f64);
const result = a - 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;
try stdout.print("{d} - {d} = {d}\n", .{ a, b, result });
}
fn multiplyExec(ctx: CommandContext) !void {
const a = try ctx.getArg("a", f64);
const b = try ctx.getArg("b", f64);
const result = a * 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;
try stdout.print("{d} * {d} = {d}\n", .{ a, b, result });
}
fn calculatorRootExec(ctx: CommandContext) !void {
try ctx.command.printHelp();
}
fn makeOperationCmd(
allocator: std.mem.Allocator,
options: CommandOptions,
) !*Command {
var cmd = try Command.init(allocator, options);
try cmd.addPositional(PositionalArg{ .name = "a", .description = "First number", .is_required = true, .type = .Float });
try cmd.addPositional(PositionalArg{ .name = "b", .description = "Second number", .is_required = true, .type = .Float });
return cmd;
}
pub fn main(init: std.process.Init.Minimal) !void {
var gpa: std.heap.DebugAllocator(.{}) = .init;
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var root_cmd = try Command.init(allocator, CommandOptions{
.name = "calculator",
.description = "A simple CLI calculator.",
.exec = calculatorRootExec,
});
defer root_cmd.deinit();
const add_cmd = try makeOperationCmd(allocator, .{
.name = "add",
.description = "Adds two numbers.",
.exec = addExec,
});
const subtract_cmd = try makeOperationCmd(allocator, .{
.name = "subtract",
.description = "Subtracts two numbers.",
.exec = subtractExec,
});
const multiply_cmd = try makeOperationCmd(allocator, .{
.name = "multiply",
.description = "Multiplies two numbers.",
.exec = multiplyExec,
});
try root_cmd.addSubcommand(add_cmd);
try root_cmd.addSubcommand(subtract_cmd);
try root_cmd.addSubcommand(multiply_cmd);
try root_cmd.run(init.args, null);
}
// Example Invocations
//
// 1. Build the example executable:
// zig build e7_calculator
//
// 2. Run with different arguments:
//
// // Add two numbers
// ./zig-out/bin/e7_calculator add 10.5 22
//
// // Subtract two numbers
// ./zig-out/bin/e7_calculator subtract 100 42.5
//
// // Multiply two numbers
// ./zig-out/bin/e7_calculator multiply -5.5 10