Skip to content

Commit a4da0f6

Browse files
committed
bundle-server: use arg parser
Update 'main.go' to use the custom argument parser in the 'argparse' package, specifying each command as a "subcommand" and invoking it with 'InvokeSubcommand()'. By using the argument parser, the '-h' or '--help' flags will automatically print usage information, and improperly formed invocations (invalid command, no command specified, unused flags, etc.) will trigger an informative error and exit with the usage string. This change substantially simplifies 'main.go' and improves user experience with the CLI. Signed-off-by: Victoria Dye <[email protected]>
1 parent 9b77324 commit a4da0f6

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

cmd/git-bundle-server/main.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,15 @@ func all() []argparse.Subcommand {
2121
func main() {
2222
cmds := all()
2323

24-
if len(os.Args) < 2 {
25-
log.Fatal("usage: git-bundle-server <command> [<options>]\n")
26-
return
24+
parser := argparse.NewArgParser("git-bundle-server <command> [<options>]")
25+
parser.SetIsTopLevel(true)
26+
for _, cmd := range cmds {
27+
parser.Subcommand(cmd)
2728
}
29+
parser.Parse(os.Args[1:])
2830

29-
for i := 0; i < len(cmds); i++ {
30-
if cmds[i].Name() == os.Args[1] {
31-
err := cmds[i].Run(os.Args[2:])
32-
if err != nil {
33-
log.Fatal("Failed with error: ", err)
34-
}
35-
}
31+
err := parser.InvokeSubcommand()
32+
if err != nil {
33+
log.Fatal("Failed with error: ", err)
3634
}
3735
}

0 commit comments

Comments
 (0)