cli icon indicating copy to clipboard operation
cli copied to clipboard

How to implement something like an OptionFlag?

Open marcofranssen opened this issue 4 years ago • 16 comments

How to implement something like an OptionFlag

I'm looking to implement a Flag which can have a few predefined values.

E.g.

mycli --output json
mycli --output xml

Is there a way to implement this using the existing flags while also informing the user of the cli what are the available options?

If not would it make sense to add this OptionFlag? I would be happy to make a PR for that with some guidance.

marcofranssen avatar Jun 19 '20 09:06 marcofranssen

To rephrase the question - are if asking if there's a builtin way to implement a flag that gives users a set of choices from a predefined list? Something like Choices: []string{'json', 'xml'}?

coilysiren avatar Jul 02 '20 03:07 coilysiren

@lynncyrin yes that is exactly what I meant.

marcofranssen avatar Jul 02 '20 11:07 marcofranssen

Makes sense! There's no obvious builtin way to implement this, but I've implemented a few "manual" versions of it. I don't happen to have that code on-hand, though.

coilysiren avatar Jul 02 '20 17:07 coilysiren

@lynncyrin would be great if you could dig it up and share a snippet here with the community. I'm happy to take that snippet of your further to integrate it here.

marcofranssen avatar Jul 03 '20 11:07 marcofranssen

Ping @lynncyrin

MarvinJWendt avatar Aug 24 '20 03:08 MarvinJWendt

This was previously requested in #620

nielsvaneck avatar Sep 17 '20 02:09 nielsvaneck

This issue or PR has been automatically marked as stale because it has not had recent activity. Please add a comment bumping this if you're still interested in it's resolution! Thanks for your help, please let us know if you need anything else.

stale[bot] avatar Dec 16 '20 02:12 stale[bot]

Not stale

MarvinJWendt avatar Dec 16 '20 12:12 MarvinJWendt

This issue or PR has been bumped and is no longer marked as stale! Feel free to bump it again in the future, if it's still relevant.

stale[bot] avatar Dec 16 '20 12:12 stale[bot]

Here is how I implemented it using GenericFlag and a custom value

https://play.golang.org/p/BWBaQ0tTLKe

dearchap avatar Feb 06 '21 02:02 dearchap

There is a PR for this feature. https://github.com/urfave/cli/pull/1234

dearchap avatar Feb 08 '21 01:02 dearchap

This issue or PR has been automatically marked as stale because it has not had recent activity. Please add a comment bumping this if you're still interested in it's resolution! Thanks for your help, please let us know if you need anything else.

stale[bot] avatar Jun 02 '21 15:06 stale[bot]

Closing this as it has become stale.

stale[bot] avatar Jul 04 '21 04:07 stale[bot]

Wy Wy Wy! should we open it again?

abdennour avatar Jul 23 '21 23:07 abdennour

There are 2 ways to approach this. One is with PR #1234 . The other is using validations as in PR #1261 . Its upto the maintainers to choose the way forward.

dearchap avatar Jul 25 '21 00:07 dearchap

This issue or PR has been bumped and is no longer marked as stale! Feel free to bump it again in the future, if it's still relevant.

stale[bot] avatar Jul 25 '21 00:07 stale[bot]

This is a duplicate of #786 .

dearchap avatar Oct 21 '22 21:10 dearchap