Giter VIP home page Giter VIP logo

Comments (5)

donatj avatar donatj commented on May 13, 2024

I've been trying to figure out the best way to use the top level flags myself.

from subcommands.

the-maldridge avatar the-maldridge commented on May 13, 2024

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.

adjackura avatar adjackura commented on May 13, 2024

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.

fgm avatar fgm commented on May 13, 2024

Suggested example: https://play.golang.org/p/BGV9lVDjIc5

from subcommands.

fgm avatar fgm commented on May 13, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.