Skip to content

Commit

Permalink
Allow global flags to go after the subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
Dobefu committed Nov 26, 2024
1 parent 719f570 commit ba29e7d
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ func init() {

func main() {
flag.Parse()
applyGlobalFlags()

args := flag.Args()

if len(args) < 1 {
listSubCommands()
}

cmdName := args[0]
var err error

Expand All @@ -50,14 +54,18 @@ func main() {
switch cmdName {
case "migrate:db":
reset := flag.Bool("reset", false, "Migrate from a clean database. Warning: this will delete existing data")
registerGlobalFlags(flag)
flag.Parse(args[1:])
applyGlobalFlags()

err = migrate_db.Main(*reset)
break

case "remote:sync":
reset := flag.Bool("reset", false, "Synchronise all data, instead of starting from the last sync token")
registerGlobalFlags(flag)
flag.Parse(args[1:])
applyGlobalFlags()

err = remote_sync.Sync(*reset)
break
Expand All @@ -70,6 +78,12 @@ func main() {
}
}

func registerGlobalFlags(fset *flag.FlagSet) {
flag.VisitAll(func(f *flag.Flag) {
fset.Var(f.Value, f.Name, f.Usage)
})
}

func applyGlobalFlags() {
if *verbose {
logger.SetLogLevel(logger.LOG_VERBOSE)
Expand Down

0 comments on commit ba29e7d

Please sign in to comment.