Repository navigation
Expand file tree
/
Copy pathadvanced.zig
More file actions
87 lines (71 loc) · 2.63 KB
/
Copy pathadvanced.zig
File metadata and controls
87 lines (71 loc) · 2.63 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
const std = @import("std");
const argsparse = @import("argonaut");
fn validatePort(args: []const []const u8) !void {
if (args.len != 1) return;
const port = try std.fmt.parseInt(u16, args[0], 10);
if (port < 1024) {
return error.PortTooLow;
}
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const parser = try argsparse.newParser(allocator, "advanced-example", "Advanced features demonstration");
defer parser.deinit();
const verbose = try parser.flagCounter("v", "verbose", null);
var port_opts = argsparse.Options{
.help = "Server port (must be >= 1024)",
.default_int = 8080,
.validate = validatePort,
};
const port = try parser.int("p", "port", &port_opts);
var files_opts = argsparse.Options{
.help = "Input files to process",
};
const files = try parser.stringList("f", "file", &files_opts);
const input_file = try parser.stringPositional(null);
var numbers_opts = argsparse.Options{
.help = "List of numbers",
};
const numbers = try parser.intList("n", "number", &numbers_opts);
var thresholds_opts = argsparse.Options{
.help = "Threshold values",
.default_float_list = &[_]f64{ 0.5, 0.75, 0.9 },
};
const thresholds = try parser.floatList("t", "threshold", &thresholds_opts);
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);
parser.parse(args) catch |err| {
std.debug.print("Error: {}\n\n", .{err});
const usage_text = try parser.usage(null);
defer allocator.free(usage_text);
std.debug.print("{s}", .{usage_text});
std.process.exit(1);
};
std.debug.print("Verbosity level: {d}\n", .{verbose.*});
std.debug.print("Port: {d}\n", .{port.*});
if (input_file.*.len > 0) {
std.debug.print("Input file: {s}\n", .{input_file.*});
}
if (files.items.len > 0) {
std.debug.print("Additional files:\n", .{});
for (files.items) |file| {
std.debug.print(" - {s}\n", .{file});
}
}
if (numbers.items.len > 0) {
std.debug.print("Numbers: ", .{});
for (numbers.items, 0..) |num, i| {
if (i > 0) std.debug.print(", ", .{});
std.debug.print("{d}", .{num});
}
std.debug.print("\n", .{});
}
std.debug.print("Thresholds: ", .{});
for (thresholds.items, 0..) |threshold, i| {
if (i > 0) std.debug.print(", ", .{});
std.debug.print("{d:.2}", .{threshold});
}
std.debug.print("\n", .{});
}