Repository navigation
Expand file tree
/
Copy pathsubcommands.zig
More file actions
70 lines (62 loc) · 2.59 KB
/
Copy pathsubcommands.zig
File metadata and controls
70 lines (62 loc) · 2.59 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
const std = @import("std");
const argsparse = @import("argonaut");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const parser = try argsparse.newParser(allocator, "git", "A git-like command example");
defer parser.deinit();
const commit_cmd = try parser.newCommand("commit", "Record changes to the repository");
var commit_msg_opts = argsparse.Options{
.required = true,
.help = "Commit message",
};
const commit_message = try commit_cmd.string("m", "message", &commit_msg_opts);
const commit_all = try commit_cmd.flag("a", "all", null);
const branch_cmd = try parser.newCommand("branch", "List, create, or delete branches");
var branch_name_opts = argsparse.Options{
.help = "Branch name",
};
const branch_name = try branch_cmd.string("", "name", &branch_name_opts);
const branch_delete = try branch_cmd.flag("d", "delete", null);
const push_cmd = try parser.newCommand("push", "Update remote refs");
var remote_opts = argsparse.Options{
.help = "Remote name",
.default_string = "origin",
};
const remote = try push_cmd.string("", "remote", &remote_opts);
const force = try push_cmd.flag("f", "force", null);
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);
};
if (commit_cmd.happened) {
std.debug.print("Committing changes...\n", .{});
std.debug.print("Message: {s}\n", .{commit_message.*});
if (commit_all.*) {
std.debug.print("Including all changes\n", .{});
}
} else if (branch_cmd.happened) {
if (branch_delete.*) {
if (branch_name.*.len > 0) {
std.debug.print("Deleting branch: {s}\n", .{branch_name.*});
} else {
std.debug.print("Error: branch name required for delete\n", .{});
}
} else if (branch_name.*.len > 0) {
std.debug.print("Creating branch: {s}\n", .{branch_name.*});
} else {
std.debug.print("Listing branches...\n", .{});
}
} else if (push_cmd.happened) {
std.debug.print("Pushing to remote: {s}\n", .{remote.*});
if (force.*) {
std.debug.print("Force push enabled\n", .{});
}
}
}