-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmgit.mojo
More file actions
488 lines (462 loc) · 17.4 KB
/
mgit.mojo
File metadata and controls
488 lines (462 loc) · 17.4 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
"""Example: a git-like CLI to demonstrate argmojo with subcommands.
Simulates the interface of git. Only argument parsing is performed;
no actual version-control operations are implemented.
Showcases: subcommands, persistent (global) flags, per-command positional
args, boolean flags, count flags, negatable flags, choices, default values,
required arguments, value_name, hidden arguments, append/collect, value
delimiter, mutually exclusive groups, required-together groups, conditional
requirements, numeric range validation, aliases, deprecated arguments,
Commands section in help, Global Options heading, full command path in
child help/errors, unknown subcommand error, custom tips, and shell
completion script generation.
Try these:
git --help # root help (Commands + Global Options)
git --version
git clone --help # child help with full path
git clone https://example.com/repo.git my-project --depth 1
git commit -am "initial commit"
git log --oneline -n 20 --author "Alice"
git remote add origin https://example.com/repo.git
git -v push origin main --force --tags
git --completions bash # shell completion script (built-in)
"""
from argmojo import Argument, Command
fn main() raises:
var app = Command(
"mgit",
"A git-like CLI to demonstrate argmojo with subcommands.",
version="2.47.0",
)
# ── Persistent (global) flags ────────────────────────────────────────
app.add_argument(
Argument("verbose", help="Be more verbose")
.long["verbose"]()
.short["v"]()
.count()
.persistent()
)
app.add_argument(
Argument("no-pager", help="Do not pipe output into a pager")
.long["no-pager"]()
.flag()
.persistent()
)
app.add_argument(
Argument("git-dir", help="Set the path to the repository (.git)")
.long["git-dir"]()
.value_name["PATH"]()
.persistent()
)
app.add_argument(
Argument("work-tree", help="Set the path to the working tree")
.long["work-tree"]()
.value_name["PATH"]()
.persistent()
)
# ── Custom tips ──────────────────────────────────────────────────────
app.add_tip("Run 'git <command> --help' for detailed help on a command.")
# ── clone ────────────────────────────────────────────────────────────
var clone = Command("clone", "Clone a repository into a new directory")
clone.add_argument(
Argument("repository", help="Repository URL or path")
.positional()
.required()
)
clone.add_argument(
Argument("directory", help="Target directory name").positional()
)
clone.add_argument(
Argument("depth", help="Create a shallow clone with N commits")
.long["depth"]()
.value_name["N"]()
.range[1, 999999]()
)
clone.add_argument(
Argument("branch", help="Check out this branch instead of HEAD")
.long["branch"]()
.short["b"]()
)
clone.add_argument(
Argument("bare", help="Make a bare repository").long["bare"]().flag()
)
clone.add_argument(
Argument("recurse-submodules", help="Initialize submodules")
.long["recurse-submodules"]()
.flag()
)
clone.help_on_no_arguments()
var clone_aliases: List[String] = ["cl"]
clone.command_aliases(clone_aliases^)
app.add_subcommand(clone^)
# ── init ─────────────────────────────────────────────────────────────
var init = Command("init", "Create an empty Git repository")
init.add_argument(
Argument("directory", help="Directory to initialize")
.positional()
.default["."]()
)
init.add_argument(
Argument("bare", help="Create a bare repository").long["bare"]().flag()
)
init.add_argument(
Argument("template", help="Template directory")
.long["template"]()
.choice["default"]()
.choice["minimal"]()
.default["default"]()
)
init.add_argument(
Argument("initial-branch", help="Name for the initial branch")
.long["initial-branch"]()
.short["b"]()
.default["main"]()
)
app.add_subcommand(init^)
# ── add ──────────────────────────────────────────────────────────────
var add = Command("add", "Add file contents to the index")
add.add_argument(Argument("pathspec", help="Files to add").positional())
add.add_argument(
Argument("all", help="Add all changed files")
.long["all"]()
.short["A"]()
.flag()
)
add.add_argument(
Argument("force", help="Allow adding otherwise ignored files")
.long["force"]()
.short["f"]()
.flag()
)
add.add_argument(
Argument("dry-run", help="Show what would be added without adding")
.long["dry-run"]()
.short["n"]()
.flag()
)
add.add_argument(
Argument("patch", help="Interactively select hunks to add")
.long["patch"]()
.short["p"]()
.flag()
)
app.add_subcommand(add^)
# ── commit ───────────────────────────────────────────────────────────
var commit = Command("commit", "Record changes to the repository")
commit.add_argument(
Argument("message", help="Commit message")
.long["message"]()
.short["m"]()
.required()
)
commit.add_argument(
Argument("all", help="Automatically stage modified/deleted files")
.long["all"]()
.short["a"]()
.flag()
)
commit.add_argument(
Argument("amend", help="Amend the previous commit")
.long["amend"]()
.flag()
)
commit.add_argument(
Argument("no-verify", help="Skip pre-commit and commit-msg hooks")
.long["no-verify"]()
.short["n"]()
.flag()
)
commit.add_argument(
Argument("author", help="Override the commit author")
.long["author"]()
.value_name["NAME"]()
)
# Deprecated flag
commit.add_argument(
Argument("cleanup-mode", help="Set cleanup mode (legacy)")
.long["cleanup-mode"]()
.deprecated["Use --cleanup instead"]()
)
var commit_aliases: List[String] = ["ci"]
commit.command_aliases(commit_aliases^)
app.add_subcommand(commit^)
# ── push ─────────────────────────────────────────────────────────────
var push = Command("push", "Update remote refs along with objects")
push.add_argument(
Argument("remote", help="Remote name").positional().default["origin"]()
)
push.add_argument(
Argument("refspec", help="Branch or refspec to push").positional()
)
push.add_argument(
Argument("force", help="Force push (use with caution!)")
.long["force"]()
.short["f"]()
.flag()
)
push.add_argument(
Argument("set-upstream", help="Set upstream for the branch")
.long["set-upstream"]()
.short["u"]()
.flag()
)
push.add_argument(
Argument("tags", help="Push all tags").long["tags"]().flag()
)
push.add_argument(
Argument("dry-run", help="Simulate the push without sending")
.long["dry-run"]()
.short["n"]()
.flag()
)
# Mutually exclusive: force strategies
push.add_argument(
Argument("force-with-lease", help="Safe force push")
.long["force-with-lease"]()
.flag()
)
var force_group: List[String] = ["force", "force-with-lease"]
push.mutually_exclusive(force_group^)
app.add_subcommand(push^)
# ── pull ─────────────────────────────────────────────────────────────
var pull = Command("pull", "Fetch from and integrate with a remote")
pull.add_argument(
Argument("remote", help="Remote name").positional().default["origin"]()
)
pull.add_argument(Argument("branch", help="Branch to pull").positional())
# Mutually exclusive: merge strategy
pull.add_argument(
Argument("rebase", help="Rebase instead of merge")
.long["rebase"]()
.flag()
)
pull.add_argument(
Argument("no-rebase", help="Merge instead of rebase")
.long["no-rebase"]()
.flag()
)
var merge_strat: List[String] = ["rebase", "no-rebase"]
pull.mutually_exclusive(merge_strat^)
pull.add_argument(
Argument("autostash", help="Stash changes before pull, reapply after")
.long["autostash"]()
.flag()
)
app.add_subcommand(pull^)
# ── log ──────────────────────────────────────────────────────────────
var log = Command("log", "Show commit logs")
log.add_argument(
Argument("oneline", help="Compact one-line format")
.long["oneline"]()
.flag()
)
log.add_argument(
Argument("graph", help="Draw ASCII commit graph").long["graph"]().flag()
)
log.add_argument(
Argument("number", help="Limit number of commits shown")
.long["number"]()
.short["n"]()
.value_name["N"]()
.range[1, 999999]()
)
log.add_argument(
Argument("author", help="Filter by author")
.long["author"]()
.value_name["PATTERN"]()
)
log.add_argument(
Argument("since", help="Show commits after date")
.long["since"]()
.value_name["DATE"]()
)
log.add_argument(
Argument("until", help="Show commits before date")
.long["until"]()
.value_name["DATE"]()
)
# Append: multiple --grep patterns
log.add_argument(
Argument("grep", help="Filter by commit message (repeatable)")
.long["grep"]()
.append()
)
# Aliases
log.add_argument(
Argument("format", help="Pretty-print format")
.long["format"]()
.alias_name["pretty"]()
.choice["oneline"]()
.choice["short"]()
.choice["medium"]()
.choice["full"]()
.choice["fuller"]()
)
app.add_subcommand(log^)
# ── remote ───────────────────────────────────────────────────────────
var remote = Command("remote", "Manage set of tracked repositories")
# remote itself has subcommands: add, remove, rename, show
var remote_add = Command("add", "Add a new remote")
remote_add.add_argument(
Argument("name", help="Remote name").positional().required()
)
remote_add.add_argument(
Argument("url", help="Remote URL").positional().required()
)
remote_add.add_argument(
Argument("fetch", help="Fetch the remote after adding")
.long["fetch"]()
.short["f"]()
.flag()
)
remote_add.help_on_no_arguments()
remote.add_subcommand(remote_add^)
var remote_remove = Command("remove", "Remove a remote")
remote_remove.add_argument(
Argument("name", help="Remote name to remove").positional().required()
)
remote_remove.help_on_no_arguments()
remote.add_subcommand(remote_remove^)
var remote_rename = Command("rename", "Rename a remote")
remote_rename.add_argument(
Argument("old", help="Current remote name").positional().required()
)
remote_rename.add_argument(
Argument("new", help="New remote name").positional().required()
)
remote_rename.help_on_no_arguments()
remote.add_subcommand(remote_rename^)
var remote_show = Command("show", "Show information about a remote")
remote_show.add_argument(
Argument("name", help="Remote name").positional().required()
)
remote_show.help_on_no_arguments()
remote.add_subcommand(remote_show^)
remote.help_on_no_arguments()
app.add_subcommand(remote^)
# ── branch ───────────────────────────────────────────────────────────
var branch = Command("branch", "List, create, or delete branches")
var branch_aliases: List[String] = ["br"]
branch.command_aliases(branch_aliases^)
branch.add_argument(Argument("name", help="Branch name").positional())
branch.add_argument(
Argument("delete", help="Delete a branch")
.long["delete"]()
.short["d"]()
.flag()
)
branch.add_argument(
Argument("force-delete", help="Force delete a branch")
.long["force-delete"]()
.short["D"]()
.flag()
)
branch.add_argument(
Argument("list", help="List branches (default)").long["list"]().flag()
)
branch.add_argument(
Argument("all-branches", help="List both local and remote branches")
.long["all"]()
.short["a"]()
.flag()
)
branch.add_argument(
Argument("move", help="Rename branch")
.long["move"]()
.short["m"]()
.flag()
)
app.add_subcommand(branch^)
# ── diff ─────────────────────────────────────────────────────────────
var diff = Command("diff", "Show changes between commits, trees, etc.")
var diff_aliases: List[String] = ["di"]
diff.command_aliases(diff_aliases^)
diff.add_argument(Argument("path", help="Path to diff").positional())
diff.add_argument(
Argument("staged", help="Show staged changes").long["staged"]().flag()
)
# Alias for --staged
diff.add_argument(
Argument("cached", help="Synonym for --staged")
.long["cached"]()
.alias_name["staged"]()
.flag()
.hidden()
)
diff.add_argument(
Argument("stat", help="Show diffstat instead of patch")
.long["stat"]()
.flag()
)
diff.add_argument(
Argument("name-only", help="Show only names of changed files")
.long["name-only"]()
.flag()
)
# Nargs: unified context lines
diff.add_argument(
Argument("unified", help="Generate diffs with N lines of context")
.long["unified"]()
.short["U"]()
.value_name["N"]()
)
app.add_subcommand(diff^)
# ── tag ──────────────────────────────────────────────────────────────
var tag = Command("tag", "Create, list, or delete tags")
tag.add_argument(Argument("tagname", help="Tag name").positional())
tag.add_argument(
Argument("annotate", help="Create an annotated tag")
.long["annotate"]()
.short["a"]()
.flag()
)
tag.add_argument(
Argument("tag-message", help="Tag message (for annotated tags)")
.long["message"]()
.short["m"]()
)
tag.add_argument(
Argument("tag-delete", help="Delete a tag")
.long["delete"]()
.short["d"]()
.flag()
)
tag.add_argument(
Argument("tag-list", help="List tags matching a pattern")
.long["list"]()
.short["l"]()
.flag()
)
tag.add_argument(
Argument("force-tag", help="Replace an existing tag")
.long["force"]()
.short["f"]()
.flag()
)
app.add_subcommand(tag^)
# ── stash ────────────────────────────────────────────────────────────
var stash = Command("stash", "Stash changes in working directory")
var stash_aliases: List[String] = ["st"]
stash.command_aliases(stash_aliases^)
stash.add_argument(
Argument("stash-message", help="Stash message")
.long["message"]()
.short["m"]()
)
stash.add_argument(
Argument("keep-index", help="Keep staged changes in the index")
.long["keep-index"]()
.short["k"]()
.flag()
)
stash.add_argument(
Argument("include-untracked", help="Also stash untracked files")
.long["include-untracked"]()
.short["u"]()
.flag()
)
app.add_subcommand(stash^)
# ── Show help when invoked with no arguments ─────────────────────────
app.help_on_no_arguments()
# ── Parse & display ──────────────────────────────────────────────────
var result = app.parse()
result.print_summary()