Refactor global flags

This commit is contained in:
2021-03-30 23:54:05 -05:00
parent 05db35cdfe
commit 9c024454f2
9 changed files with 22 additions and 24 deletions

View File

@ -81,6 +81,13 @@ func RegisterCommand(cmd Command) error {
if _, ok := commandRegistry[cmd.Name]; ok {
return fmt.Errorf("%s command is already registered")
}
if cmd.Flags == nil {
cmd.Flags = flag.NewFlagSet(cmd.Name, flag.ExitOnError)
}
mergeGlobalFlags(cmd.Flags)
commandRegistry[cmd.Name] = cmd
return nil
}
@ -89,3 +96,10 @@ func GetCommand(name string) (Command, bool) {
cmd, ok := commandRegistry[name]
return cmd, ok
}
func mergeGlobalFlags(fs *flag.FlagSet) {
fs.Bool("debug", false, "Enable debug mode")
outputFormat := fs.String("output-format", defaultOutputFormat, "Set the output format")
fs.StringVar(outputFormat, "o", defaultOutputFormat, "Set the output format")
}