Skip to content
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

Global options? #141

Closed
fd0 opened this issue Jun 28, 2015 · 4 comments
Closed

Global options? #141

fd0 opened this issue Jun 28, 2015 · 4 comments

Comments

@fd0
Copy link

fd0 commented Jun 28, 2015

First, thanks for the excellent library!

I'm curious if it is possible to have top-level options that are valid as options after a command, i.e. exactly the use-case that (according to the documentation) is not supported. This behavior, unfortunately, confuses users (see restic/restic#198).

Is it maybe possible to define a struct with such global options, embed it in the top-level option struct and again in the command-level option struct? What's the easiest way to merge these two?

I'm looking forward to your comments!

@jessevdk
Copy link
Owner

You could possibly do this, but it does seem very cumbersome and weird. I'd rather just fix things and allow parent command level options to be specified after subcommands (i.e. lift the restriction).

@fd0
Copy link
Author

fd0 commented Jun 28, 2015

Huh, I wasn't aware that this is possible. Can you give me a hint? Or are you talking about something you will implement?

jessevdk added a commit that referenced this issue Jun 28, 2015
@jessevdk
Copy link
Owner

I was thinking of something like this:

package main

import "github.com/jessevdk/go-flags"

type Opts struct {
    Value bool `short:"v"`
}

func main() {
    var opts struct {
        *Opts

        Command struct {
            *Opts
        } `command:"cmd"`
    }

    opts.Opts = &Opts{}
    opts.Command.Opts = opts.Opts

    flags.Parse(&opts)
    println(opts.Value)
}

But that's definitely hacky :)

I went ahead and just removed the limitation, so command parent options can be specified after the command.

@fd0
Copy link
Author

fd0 commented Jun 28, 2015

Awesome, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants