Skip to content

Commit

Permalink
Move the subcommands to a separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
Dobefu committed Nov 24, 2024
1 parent 8258bfb commit b46cd26
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func main() {
}
}

func parseSubCommands() (string, subCommand) {
cmds := map[string]subCommand{
func getSubCommands() map[string]subCommand {
return map[string]subCommand{
"migrate:db": {
desc: "Migrate or initialise the database",
},
Expand All @@ -79,6 +79,11 @@ func parseSubCommands() (string, subCommand) {
},
}

}

func parseSubCommands() (string, subCommand) {
cmds := getSubCommands()

for cmdName, cmd := range cmds {
cmds[cmdName] = subCommand{
flag: flag.NewFlagSet(cmdName, flag.ExitOnError),
Expand All @@ -87,19 +92,21 @@ func parseSubCommands() (string, subCommand) {
}

if len(os.Args) < 2 {
listCmds(cmds)
listCmds()
}

subCmd, subCmdExists := cmds[os.Args[1]]

if !subCmdExists {
listCmds(cmds)
listCmds()
}

return os.Args[1], subCmd
}

func listCmds(cmds map[string]subCommand) {
func listCmds() {
cmds := getSubCommands()

fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])

for idx, cmd := range cmds {
Expand Down

0 comments on commit b46cd26

Please sign in to comment.