转到:命令行标志

I know you can specify command line flags and run your binary file against them like this:

./binary -output=html -type=doc

However, I was looking at the implementation of this Go package: https://github.com/jteeuwen/go-bindata

I'm wondering how the author is able to enable the user to run the commands like this

go-bindata /data 

instead of

./go-bindata -target=/data

Appreciate some help in case I missed out anything!

The trick is to use flag.Args() which is, simply, the non flag arguments following the flags. You can either fetch the entire list, or get a specific arg with flag.Arg(i). See http://golang.org/pkg/flag/#Args

And from the source of the program you posted:

// Create input configurations.
c.Input = make([]bindata.InputConfig, flag.NArg())
for i := range c.Input {
    c.Input[i] = parseInput(flag.Arg(i))
}

Easy as pie using the flag package. Checkout flag.Args