user3918985
user3918985

Reputation: 4419

Go: command line flags

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!

Upvotes: 1

Views: 471

Answers (2)

Simon
Simon

Reputation: 32893

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

Upvotes: 1

Not_a_Golfer
Not_a_Golfer

Reputation: 49205

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))
}

Upvotes: 6

Related Questions