Comments (5)
I've been trying to figure out the best way to use the top level flags myself.
from subcommands.
After looking into this more and looking into projects that include this, I believe this is referring instead to the flags that are explicitly defined with the flags package. They exist outside the configuration of this package, which is why they must come first. They also need to be plumbed in directly since they reside in the main package.
At least I think that's how it works.
from subcommands.
Yes, from what I recall you at correct. I think the confusion is coming from the comment calling these 'top-level flags', that simply means they are not treated as subcommands but instead parsed using flags.Parse(). Subcommands don't have direct access to these flags other than through normal channels like top level variables.
from subcommands.
Suggested example: https://play.golang.org/p/BGV9lVDjIc5
from subcommands.
Another way of looking at it, after spending some time creating a tutorial for this package (see https://github.com/fgm/subcommands_demo ): after parsing, add the parsed value to the context, like:
// package scope
type verboseKey string
// function scope
flag.BoolVar(&verbose, "v", false, "make it verbose)
fs.Parse()
ctx = context.WithValue(ctx, verboseKey("v"), verbose)
subcommands.Execute(ctx)
At this point your command can retrieve the flag value from the context without sharing a variable using ctx.Value(verboseKey("v")).(bool)
. Which makes sense, as a CLI run is pretty much like a request on a server.
You could also pass the value using the extra args, like subcommands.Execute(ctx, verbose)
and in the command, retrieve it as args[0].(bool)
, but that seems a little less robust and less semantically appropriate.
from subcommands.
Related Issues (20)
- API to print usage should be public, so commands can invoke it HOT 1
- Help mentions flags-subcommand even if it doesn't exists
- Tag versions correctly HOT 1
- Subcommand suggestions for typos
- Prefix matching for subcommands
- tag v1.0.2? HOT 2
- Kubectl / git like plugin system HOT 1
- Tag versions correctly HOT 1
- Prevent registering homonymous commands
- println is redirected to stderr HOT 2
- Cancel propagate on Signals HOT 2
- go.sum not checked into repository HOT 2
- help command incorrectly locates flags in its result
- Commander misses initializing flagset output
- Feature Request: Allow hidden commands HOT 1
- Feature: Allow to run default command without showing usage
- Programmatically Generating Autocompletion Hints
- package context: unrecognized import path "context" (import path does not begin with hostname) HOT 4
- proposal: Execute() (ExitStatus, error) HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from subcommands.