cli icon indicating copy to clipboard operation
cli copied to clipboard

Let commands access top-level flags?

Open isbadawi opened this issue 12 years ago • 9 comments

I'm not sure if I'm doing something wrong or if cli doesn't support this. I have an app with lots of subcommands, and I'd like to have some global flags that apply for every command (e.g. specifying the location of a config file). (Right now the commands don't have any command-specific flags, but potentially I could want both global flags and command specific flags).

If I specify the flags in the top level app.Flags, I can't seem to extract their value from the context in the action -- I always get empty strings back. Instead, I have to repeat the flags in each command definition for it to work. Is this the intended behaviour (top-level flags don't do anything for commands)?

isbadawi avatar Feb 23 '14 07:02 isbadawi

The global flags can be accessed like:

      Action: func(c *cli.Context) {
         println("User is....")
         if(c.GlobalString("user") != "") {
             println("User: ", c.GlobalString("user"))
         }
      },

chilicat avatar Feb 23 '14 17:02 chilicat

Ah, my bad! I guess I was fixated on String() because of the example in the README. Thanks!

isbadawi avatar Feb 23 '14 20:02 isbadawi

This should be in the README. I just spent WAY too long trying to figure out how to get this working. :(

slantview avatar Apr 01 '14 23:04 slantview

Agreed. I will make sure it is added to the readme

Sent from my iPhone

On Apr 1, 2014, at 4:55 PM, Steve Rude [email protected] wrote:

This should be in the README. I just spent WAY too long trying to figure out how to get this working. :(

— Reply to this email directly or view it on GitHub.

codegangsta avatar Apr 02 '14 00:04 codegangsta

Sorry for a possibly stupid question, but I am not able to get this working...

package main

import (
    "github.com/codegangsta/cli"
    "os"
    "fmt"
    "strconv"
    "github.com/op/go-logging"
)
var LOGGER = logging.MustGetLogger("tlookup")

func main() {

    app := cli.NewApp()
    app.Name     = "tlookup"
    app.Version  = Version
    app.Usage    = "kicking ass and taking names"
    app.Author   = "John Dyer"
    app.Email    = "[email protected]"
    app.Compiled = CompileTime()
    app.Commands = Commands

    app.Flags = []cli.Flag {
    cli.BoolFlag{"debug, d", "Run in debug mode"},
  }

    app.Action = func(c *cli.Context) {
        foo := strconv.FormatBool(c.GlobalBool("debug"))
        fmt.Println("====> ",  foo) // Nothing ever happens here
  }

    app.Run(os.Args)
}

johntdyer avatar Jul 03 '14 21:07 johntdyer

@johntdyer I tried running your code (minus the bits I don't have like the logger and some other things), and it seems to work fine, printing ====> false by default and ====> true when I pass -d or --debug. (It also works fine with Bool instead of GlobalBool, since you're using app.Action and a not a subcommand).

isbadawi avatar Jul 03 '14 23:07 isbadawi

Odd, it works for me now too... Must have been a PEBCAK error :)

johntdyer avatar Jul 04 '14 03:07 johntdyer

This is not true with subcommands apparently:

package main

import (
    "fmt"
    "os"
    "strconv"

    "github.com/codegangsta/cli"
)

func main() {

    app := cli.NewApp()
    app.Name = "tlookup"
    app.Usage = "kicking ass and taking names"
    app.Author = "John Dyer"
    app.Email = "[email protected]"

    app.Flags = []cli.Flag{
        cli.BoolFlag{"debug, d", "Run in debug mode"},
    }

    app.Commands = []cli.Command{
        {
            Name: "first",
            Subcommands: []cli.Command{
                {
                    Name: "test",
                    Action: func(c *cli.Context) {
                        foo := strconv.FormatBool(c.GlobalBool("debug"))
                        fmt.Println("====> ", foo) // Nothing ever happens here
                    },
                },
            },
        },
    }
    app.Run(os.Args)
}

Will fail to display "debug" correctly:

./testcli -d first test
====>  false

gravis avatar Jul 05 '14 09:07 gravis

This is indeed a bug. I have a fix, and will commit a PR today.

gravis avatar Jul 06 '14 09:07 gravis