-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accessing global flags from subcommand #1268
Comments
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. |
Bump |
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. |
Duplicate of #427? Looks like this has been asked about several times over the span of years. |
@dargueta no, I'm interested in global flags staying in their usual place, but being accessible from within subcommand code. |
Oh, I misunderstood. Ignore me! 😅 |
@makeworld-the-better-one The way you do it is fine. I dont think there is a better way. |
@dearchap Well this is obviously kind of a hack, and not something simple for a library user to figure out on their own. If what I'm doing is the best way, then it should be added to the library API as function for getting global args. And there is even potentially a bug in the library code, as you can see from my original issue comment. |
@makeworld-the-better-one I agree and I would love too see an addition to the library API to help with this 🤘🏼 Is this something you're up for contributing? |
Not right at the moment, but maybe some time in the next couple weeks. I'll comment here if I take it on, so if anyone else wants to before then feel free. |
@makeworld-the-better-one couldnt you just use context.Value('globalFlagName') ? |
@makeworld-the-better-one after thinking about this a bit more, I'm recalling the Lines 245 to 255 in c24c9f3
It seems to me now that dropping the Lines 165 to 176 in d62ac9c
In your usage, are you finding that the internal traversal of context lineage isn't working because there's a flag name conflict or similar? |
@meatballhat it's been a while since I used this code, so I'm not sure right now. I don't believe there was a conflict, just that I couldn't easily access the value of a global flag from within a subcommand context. |
- Move '--config' flag from global options to 'web' subcommand. - Set default '--config' flag value to 'fyj.ini'. [cli](github.com/urfave/cli) library requires global flags before subcommands/arguments. For refer to urfave/cli#1113, urfave/cli#1268 and urfave/cli#1391.
@makew0rld I really am not seeing the issue.
You can get the value of a global flag from a subcommand action. If you mean set the value of a global flag in a subcommand that support is available in v3 via marking a flag as persistent |
can this tun into a feature? I have a cli im working on soon where every sub-command will need a --env flag |
This is a feature in v3. It's already implemented and working. You can try it out right now |
@dearchap can you point me to v3? i see no branch/tag/release/docs/etc |
@acidjazz
I hope this helps! 😁 |
Here is how I am accessing global flags from a subcommand. This func also handles accessing a global flag when the provided context is also global, because it simplifies code.
This has been working for me. Is there a better way or built-in way to do this? Also, do you know why I have to handle that special case? Is that a bug?
The text was updated successfully, but these errors were encountered: