skybet / cali

Cali Automation Layout Initialiser
MIT License
32 stars 7 forks source link

Allow for reuse of flag names in different tasks #13

Open wheresalice opened 6 years ago

wheresalice commented 6 years ago

Given I am using the flag name 'port' in one command When I define the flag name 'port' in a second command Then the default value will be set to the one in the command highest in the alphabet.

These values are written with a function belonging to command and therefore look like they should be command-specific:

command.Flags().StringP("port", "p", "8000", "Port to expose on host")

However the function to read them does not seem to be command-specific:

cli.FlagValues().GetString("port")

There should be a way of setting the same flag for different commands without them overriding each other. There should also be a way of setting a global flag for all commands to reuse.

lucymhdavies commented 6 years ago

Global flags can already be set, by virtue of a cali.Cli also being a cali.Command.

var (
    // Define this here, then all other files in cmd can add subcommands to it
    cli = cali.NewCli("lucli")
)

func init() {
    cli.SetShort("Example CLI tool")
    cli.SetLong("A nice long description of what your tool actually does")

    cli.Flags().StringP("bob", "r", "default", "Which bob should we use?")
    cli.BindFlags()
}

I should definitely start a Wiki for this https://github.com/skybet/cali/wiki <-- currently empty.

As for setting the same flag for different commands... Yeah, that would be useful.

lucymhdavies commented 6 years ago

Wondering how it would work.

e.g. perhaps

command.FlagValues().GetString("port")

(this might already work, and be unambiguous, at least for Cobra flags, as you're only ever running one Cobra command. For Viper configs, there's still ambiguity)