-
Notifications
You must be signed in to change notification settings - Fork 13
/
ls.zig
103 lines (84 loc) · 3.11 KB
/
ls.zig
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
const std = @import("std");
const yazap = @import("yazap");
const allocator = std.heap.page_allocator;
const log = std.log;
const App = yazap.App;
const Arg = yazap.Arg;
pub fn main() anyerror!void {
var app = App.init(allocator, "myls", "My custom ls");
defer app.deinit();
var myls = app.rootCommand();
myls.setProperty(.help_on_empty_args);
var update_cmd = app.createCommand("update", "Update the app or check for new updates");
update_cmd.setProperty(.help_on_empty_args);
try update_cmd.addArg(Arg.booleanOption("check-only", null, "Only check for new update"));
try update_cmd.addArg(Arg.singleValueOptionWithValidValues(
"branch",
'b',
"Branch to update",
&[_][]const u8{ "stable", "nightly", "beta" },
));
try myls.addSubcommand(update_cmd);
try myls.addArg(Arg.booleanOption("all", 'a', "Don't ignore the hidden directories"));
try myls.addArg(Arg.booleanOption("recursive", 'R', "List subdirectories recursively"));
try myls.addArg(Arg.booleanOption("one-line", '1', "List each entries in new line"));
try myls.addArg(Arg.booleanOption("size", 's', "Display file size"));
try myls.addArg(Arg.booleanOption("version", null, "Display program version number"));
var ignore_opt = Arg.singleValueOption("ignore", 'I', "Ignore the given pattern");
ignore_opt.setValuePlaceholder("PATTERN");
var hide_opt = Arg.singleValueOption("hide", null, "Don't display hidden entries");
hide_opt.setValuePlaceholder("PATTERN");
try myls.addArg(ignore_opt);
try myls.addArg(hide_opt);
try myls.addArg(Arg.singleValueOptionWithValidValues(
"color",
'C',
"Enable or disable output color",
&[_][]const u8{ "always", "auto", "never" },
));
const matches = try app.parseProcess();
// Use `.help_on_empty_args` property.
//
// if (!(matches.containsArgs())) {
// try app.displayHelp();
// return;
// }
if (matches.containsArg("version")) {
log.info("v0.1.0", .{});
return;
}
if (matches.subcommandMatches("update")) |update_cmd_matches| {
// Use `.help_on_empty_args` property.
//
// if (!(update_cmd_matches.containsArgs())) {
// try app.displaySubcommandHelp();
// return;
// }
if (update_cmd_matches.containsArg("check-only")) {
std.log.info("Check and report new update", .{});
return;
}
if (update_cmd_matches.getSingleValue("branch")) |branch| {
std.log.info("Branch to update: {s}", .{branch});
return;
}
return;
}
if (matches.containsArg("all")) {
log.info("show all", .{});
return;
}
if (matches.containsArg("recursive")) {
log.info("show recursive", .{});
return;
}
if (matches.getSingleValue("ignore")) |pattern| {
log.info("ignore pattern = {s}", .{pattern});
return;
}
if (matches.containsArg("color")) {
const when = matches.getSingleValue("color").?;
log.info("color={s}", .{when});
return;
}
}