-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeature_command.zig
More file actions
95 lines (85 loc) · 3.03 KB
/
feature_command.zig
File metadata and controls
95 lines (85 loc) · 3.03 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
const std = @import("std");
const Allocator = @import("std").mem.Allocator;
const log = @import("std").log.scoped(.feature_command);
const help_strings = @import("help_strings");
/// sparse feature [ options ] <feature_name> [<slice_name>]
const Params = struct {
/// args:
/// <feature_name>: name of the feature to be created. If a feature with the same name
/// exists, sparse simply switches to that feature.
feature_name: [1][]u8,
/// <slice_name>: name of the first slice in newly created feature. This
/// argument is ignored if the feature already exists.
slice_name: ?[1][]u8 = null,
///
/// options:
_options: struct {
const Options = @This();
/// --to <branch>: branch to build on top. Pull requests for the new
/// feature will target this branch. (default: main)
@"--to": []const u8 = "main",
/// -h, --help: shows this help message.
@"--help": *const fn () void = Options.help,
@"-h": *const fn () void = Options.help,
pub fn help() void {
var buffer: [4096]u8 = .{0} ** 4096;
var stdout_writer = std.fs.File.stdout().writer(&buffer);
const stdout = &stdout_writer.interface;
defer {
stdout.flush() catch {};
}
stdout.print(help_strings.sparse_feature, .{}) catch {};
}
} = .{},
};
///
/// sparse feature [ options ] <feature_name> [<slice_name>]
///
pub const FeatureCommand = struct {
pub fn run(self: FeatureCommand, alloc: Allocator) !u8 {
_ = self;
var params = Params{ .feature_name = undefined };
const args = try std.process.argsAlloc(alloc);
defer std.process.argsFree(alloc, args);
for (args) |arg| {
log.debug("got cli arguments: {s}", .{arg});
}
const cli_positionals = command.parseOptions(
@TypeOf(params._options),
alloc,
¶ms._options,
args,
) catch |err| switch (err) {
command.Error.OptionHandledAlready => return 0,
else => return err,
};
defer alloc.free(cli_positionals);
try command.parsePositionals(
Params,
alloc,
¶ms,
cli_positionals,
);
log.debug(
"parsed feature command:: feature_name:{s} slice_name:{s} _options.--to:{s}",
.{
params.feature_name[0],
if (params.slice_name) |s| s[0] else "null",
params._options.@"--to",
},
);
Sparse.feature(
params.feature_name[0],
if (params.slice_name) |s| s[0] else null,
params._options.@"--to",
) catch |err| switch (err) {
else => {
log.err("error: {any}", .{err});
return 1;
},
};
return 0;
}
};
const Sparse = @import("sparse_lib").Sparse;
const command = @import("command.zig");