cli icon indicating copy to clipboard operation
cli copied to clipboard

Required Flags

Open slantview opened this issue 12 years ago • 44 comments

It would be really nice to have required flags and required arguments. I'm mostly putting this here as a place holder to discuss although I would be willing to put some work in on this if we can talk about how we would like to implement it.

I was thinking the easiest would just add a new field to the struct called "Required". Internally we could have some checking and fail if the required flag is not set.

type Flag interface {
    fmt.Stringer
    Required bool
    // Apply Flag settings to the given flag set
    Apply(*flag.FlagSet)
    getName() string
}

func (f *Flag) IsRequired() bool {
  return f.Required
}

slantview avatar Apr 29 '14 00:04 slantview

:+1:

johntdyer avatar Jul 03 '14 18:07 johntdyer

What is the use case for a required flag? I have never come across a cli app where a flag was required

codegangsta avatar Jul 03 '14 18:07 codegangsta

Thanks for the quick response. Regarding the request, I cam think of dozens of reasons to define a flag as required. username, url, password, ect ...

johntdyer avatar Jul 03 '14 18:07 johntdyer

Just like required commands, this may be a better thing to handle in your application logic.

Also, adding a field to the Flag interface will unfortunately break the interface and all existing implementations.

On Jul 3, 2014, at 11:24 AM, John Dyer [email protected] wrote:

Thanks for the quick response. Regarding the request, I cam think of dozens of reasons to define a flag as required. username, url, password, ect ...

— Reply to this email directly or view it on GitHub.

codegangsta avatar Jul 03 '14 18:07 codegangsta

I agree with @codegangsta here, there is no reason a flag should be "required". You should provide sensible defaults for the flag or instead read the value in as a command line argument.

jszwedko avatar Jul 11 '14 15:07 jszwedko

We wrote a tool called "motherbrain" (https://github.com/RiotGames/motherbrain) and we have use cases where an environment flag is required. Just because you don't have a case for it, doesn't mean they don't exist. What is the opposition to adding a way to require a flag? If you don't have that use case, simply don't use the feature. We really like this library, but there is no reason not to add features just because you don't need that particular feature.

Like I said in the original message, it would be really nice to have both required flags and required arguments. Every time I add a required global argument, I have to add a significant amount of boiler plate to each and every action function.

Edit: to clarify, we are willing to contribute code, but I was hoping this would be more of a discussion of how you wanted to implement, not "I don't need that feature, so I don't want it in here."

slantview avatar Jul 11 '14 18:07 slantview

The (sort-of) Ruby equivalent library for writing command line arguments has a required option for flags https://github.com/erikhuda/thor/wiki/Method-Options

slantview avatar Jul 11 '14 18:07 slantview

@slantview thanks for the example. I can see the use case for it now.

Unfortunately the proposed solution adds a method to the Flag interface. This will break other implementations of Flag and we can't do that unless we bump the package up to 2.0.

Maybe a utility function on context that will assert that a flag is set would be a better solution. Essentially it is just checking that a generic flag is not nil:

if c.Generic("flagname") == nil {
  log.Fatalln("flagname is required")
}

Although it is only 3 lines of code so it might not be as valuable to have in the codebase. Maybe useful to have in the readme?

codegangsta avatar Jul 11 '14 20:07 codegangsta

@slantview fair enough :)

jszwedko avatar Jul 11 '14 22:07 jszwedko

Most of the CLIs I write have required flags, would be great to have that handled in the package rather than re-implementing it each time.

wjessop avatar Aug 18 '14 13:08 wjessop

+1

ahall avatar Aug 21 '14 23:08 ahall

I think this applies to things like StringFlag more than Generic. For example, I have 3 files I need to specify for a command. The flags don't seem to get printed for subcommand help, and adding 9 lines to handle it is a bit much. Especially since you need to have a Value in order to get the help to display properly.

Edit: Nevermind about subcommand help, I was just using it wrong :) At the same time, required flags is a useful feature IMO.

sugarb avatar Aug 22 '14 15:08 sugarb

+1 totally agree. Required flags are very useful.

parnurzeal avatar Oct 06 '14 05:10 parnurzeal

+1

enmand avatar Oct 28 '14 22:10 enmand

:+1: for this.

I agree with @codegangsta here, there is no reason a flag should be "required". You should provide sensible defaults for the flag or instead read the value in as a command line argument.

I'm developing a load testing cli tool and you need at least provide resource name which is --url flag.

Also, it should blow up the process if the required flag is not set.

tugberkugurlu avatar Nov 12 '14 15:11 tugberkugurlu

+1

I was looking for this feature and ended up here. Just as @slantview I also have, what I feel, unnecessary boilerplate code to make sure flags / parameters are set. For example database connection strings, user names, passwords and so on.

spagettikod avatar Nov 22 '14 08:11 spagettikod

+1

jhiemer avatar Mar 31 '15 11:03 jhiemer

:+1:

assimovt avatar Apr 07 '15 20:04 assimovt

+1

bbiskup avatar Jun 10 '15 17:06 bbiskup

@codegangsta I am going to go against the tide here: if a flag is required, it should be a positional argument. Having required flags in the package promotes this anti-pattern. The suggestion in https://github.com/codegangsta/cli/issues/85#issuecomment-48777510 covers the use case. Perhaps, some helper functions could be added to cover the required use case, as well as cases where certain flags are required when others are set.

stevvooe avatar Jun 11 '15 05:06 stevvooe