Skip to content

Instantly share code, notes, and snippets.

@therealplato
Last active September 25, 2017 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save therealplato/8381f6943108149047e60f9a77e48377 to your computer and use it in GitHub Desktop.
Save therealplato/8381f6943108149047e60f9a77e48377 to your computer and use it in GitHub Desktop.
urfave/cli delegating flag handling
//$GOPATH/src/github.com/therealplato/scratch/repro-urfave/baz.go
package baz
import (
"flag"
"fmt"
)
func Run() {
filename := flag.String("c", "", "filename of the JSON-based configuration file")
flag.Parse()
fmt.Printf("flag was `%v`", *filename)
}
//$GOPATH/src/github.com/therealplato/scratch/repro-urfave/cmd/main.go
/*
repro-urfave/cmd Ω ./cmd bar baz -c asdf
flag was ``%
repro-urfave/cmd Ω ./cmd bar baz -c=asdf
flag was ``%
repro-urfave/cmd Ω ./cmd bar baz -c='asdf'
flag was ``%
*/
package main
import (
"os"
baz "github.com/therealplato/scratch/repro-urfave"
cli "gopkg.in/urfave/cli.v1"
)
var args []string
func main() {
app := cli.NewApp()
app.Name = "foo"
app.Commands = []cli.Command{
{
Name: "bar",
Usage: "initialize",
SkipFlagParsing: true,
Subcommands: []cli.Command{
{
Name: "baz",
SkipFlagParsing: true,
Action: func(c *cli.Context) error {
baz.Run()
return nil
},
},
},
},
}
app.Run(os.Args)
}
package main
import (
"flag"
"fmt"
)
func main() {
filename := flag.String("c", "", "filename of the JSON-based configuration file")
flag.Parse()
fmt.Printf("flag was `%v`", *filename)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment