navid/veins / commit

0c1ad4fc2e5b

Navid M <navidmomtahen@gmail.com> · 2026-11-25

parents: 16b30ee10df1

Browse files at this commit · Download source

all: implement help subcommand
--- /dev/null+++ b/src/src/cli/help_cmd.sx@@ -0,0 +1,161 @@+/// `vn help`: show usage for the program or one of its commands.++val str   = use("std/string")+val stdio = use("std/stdio")++fn cmd_is(a: ref u8, text: ref u8, len: usize) bool = trust {+    return str.eql(str.from_cstr(a), str.view(text, len as i64))+}++pub fn usage() void = trust {+    stdio.puts("vein is a tool for managing source code")+    stdio.puts("")+    stdio.puts("commands:")+    stdio.puts("  init       create a repository")+    stdio.puts("  git        import from git")+    stdio.puts("  checkout   write a revision into the working directory")+    stdio.puts("  fsck       verify repository integrity")+    stdio.puts("  self-test  run the format self-tests")+    stdio.puts("  version    print version information")+    stdio.puts("  help       show help for a command")+    stdio.puts("")+    stdio.puts("run 'vn help <command>' for details")+}++fn show_init() void = trust {+    stdio.puts("usage: vn init [path] [-git-import:<gitdir>]")+    stdio.puts("")+    stdio.puts("create a vein repository, optionally importing an existing git repository")+    stdio.puts("")+    stdio.puts("arguments:")+    stdio.puts("  [path]                    directory to create (default: .)")+    stdio.puts("")+    stdio.puts("options:")+    stdio.puts("  -git-import:<gitdir>      import reachable commits and refs from <gitdir>")+}++fn show_git() void = trust {+    stdio.puts("usage: vn git <subcommand> [arguments]")+    stdio.puts("")+    stdio.puts("git bridge commands:")+    stdio.puts("  import [-source:<gitdir>] [path]")+    stdio.puts("                            import reachable git commits and refs")+    stdio.puts("")+    stdio.puts("run 'vn help git <subcommand>' for details")+}++fn show_git_import() void = trust {+    stdio.puts("usage: vn git import [-source:<gitdir>] [path]")+    stdio.puts("")+    stdio.puts("import commits and refs reachable from a git repository")+    stdio.puts("")+    stdio.puts("arguments:")+    stdio.puts("  [path]                    vein repository (default: .)")+    stdio.puts("")+    stdio.puts("options:")+    stdio.puts("  -source:<gitdir>          prepare the mirror from <gitdir> before importing")+}++fn show_checkout() void = trust {+    stdio.puts("usage: vn checkout [rev]")+    stdio.puts("")+    stdio.puts("write a revision into the working directory")+    stdio.puts("")+    stdio.puts("arguments:")+    stdio.puts("  [rev]                     revision to check out")+}++fn show_fsck() void = trust {+    stdio.puts("usage: vn fsck [path] [-v]")+    stdio.puts("")+    stdio.puts("verify pack, index, object, and operation-log integrity")+    stdio.puts("")+    stdio.puts("arguments:")+    stdio.puts("  [path]                    repository to check (default: .)")+    stdio.puts("")+    stdio.puts("options:")+    stdio.puts("  -v, --verbose             print verbose output")+}++fn show_selftest() void = trust {+    stdio.puts("usage: vn self-test")+    stdio.puts("")+    stdio.puts("run the format self-tests")+}++fn show_version() void = trust {+    stdio.puts("usage: vn version")+    stdio.puts("")+    stdio.puts("print version information")+}++fn show_help() void = trust {+    stdio.puts("usage: vn help [command]")+    stdio.puts("")+    stdio.puts("show help for the program or a command")+    stdio.puts("")+    stdio.puts("arguments:")+    stdio.puts("  [command]                 command to describe")+}++pub fn run(args: list[ref u8], start: usize) i32 = trust {+    if start >= @len(args) {+        usage()+        return 0+    }+    val cmd_opt = @get(args, start)+    val cmd: mut ref u8 = none+    match cmd_opt {+        some c => {+            cmd = c+        }+        none => {+            usage()+            return 0+        }+    }++    if cmd_is(cmd, c"init", 4) {+        show_init()+        return 0+    }+    if cmd_is(cmd, c"git", 3) {+        if start + 1 < @len(args) {+            val sub_opt = @get(args, start + 1)+            if sub_opt is some sub {+                if cmd_is(sub, c"import", 6) {+                    show_git_import()+                    return 0+                }+                stdio.eprint("vn: unknown git subcommand: {s}\n", {str.from_cstr(sub)})+                return 1+            }+        }+        show_git()+        return 0+    }+    if cmd_is(cmd, c"checkout", 8) {+        show_checkout()+        return 0+    }+    if cmd_is(cmd, c"fsck", 4) {+        show_fsck()+        return 0+    }+    if cmd_is(cmd, c"self-test", 9) {+        show_selftest()+        return 0+    }+    if cmd_is(cmd, c"version", 7) {+        show_version()+        return 0+    }+    if cmd_is(cmd, c"help", 4) {+        show_help()+        return 0+    }++    stdio.eprint("vn: unknown command: {s}\n", {str.from_cstr(cmd)})+    usage()+    return 1+}--- a/src/src/main.sx+++ b/src/src/main.sx@@ -9,6 +9,7 @@ val selftest_cmd = use("cli/selftest_cmd.sx") val git_cmd      = use("cli/git_cmd.sx") val checkout_cmd = use("cli/checkout_cmd.sx")+val help_cmd     = use("cli/help_cmd.sx")  fn cmd_is(a: ref u8, text: ref u8, len: usize) bool = {     if a == none {@@ -15,22 +16,6 @@         return false     }     return bytes.Bytes.eql(bytes.from_cstr(a), bytes.bytes(text, len))-}--fn usage() void = trust {-    stdio.puts("vein is a tool for managing source code")-    stdio.puts("")-    stdio.puts("usage: vn <command> [arguments]")-    stdio.puts("")-    stdio.puts("commands:")-    stdio.puts("  init [path] [-git-import:<gitdir>]")-    stdio.puts("                       create a repository, optionally importing git")-    stdio.puts("  git import [-source:<gitdir>] [path]")-    stdio.puts("                       import reachable git commits and refs")-    stdio.puts("  checkout [rev]       write a revision into the working directory")-    stdio.puts("  fsck [path] [-v]     verify pack, index, object, and op-log integrity")-    stdio.puts("  self-test            run the format self-tests")-    stdio.puts("  version              print version information") }  fn run_init(args: list[ref u8], start: usize) i32 = trust {@@ -84,7 +69,7 @@ pub fn main() i32 = trust {     val args: list[ref u8] = @args()     if @len(args) < 2 {-        usage()+        help_cmd.usage()         return 1     }     val cmd_opt = @get(args, 1)@@ -94,7 +79,7 @@             cmd = c         }         none => {-            usage()+            help_cmd.usage()             return 1         }     }@@ -118,12 +103,11 @@         stdio.puts("vn 0.0.1 (vein-store 1)")         return 0     }-    if cmd_is(cmd, c"help", 4) || cmd_is(cmd, c"--help", 6) {-        usage()-        return 0+    if cmd_is(cmd, c"help", 4) || cmd_is(cmd, c"--help", 6) || cmd_is(cmd, c"-h", 2) {+        return help_cmd.run(args, 2)     }      stdio.eprint("vn: unknown command: {s}\n", {str.from_cstr(cmd)})-    usage()+    help_cmd.usage()     return 1 }