Any way to handle none-option parameters?
suntong opened this issue · 6 comments
Does CLI
support handling none-option parameters?
For e.g., rsync
takes an awful lot of options, but besides them, at the end of command line, there needs to be none-option parameters like source and optionally destination. Can CLI
handle cases like this?
Thanks
Yes, it can. You can get the parameters by cli.Args()
, e.g.
package main
import (
"github.com/mkideal/cli"
)
type argT struct {
cli.Helper
Hello string `cli:"hello" usage:"world"`
}
func main() {
cli.Run(new(argT), func(ctx *cli.Context) error {
ctx.String("native args: %v\n", ctx.NativeArgs())
ctx.String("args: %v\n", ctx.Args())
return nil
})
}
$ go run main.go --hello world p1 p2 p3
native args: [--hello world p1 p2 p3]
args: [p1 p2 p3]
Thanks a lot!
Sorry mkideal I cannot connect the dots with my normal cli controlling structure, which looks like this:
var root = &cli.Command{
Name: "dump",
Desc: "dump assistant program",
Text: "Swiss-army assistant toolkit",
Argv: func() interface{} { return new(rootT) },
Fn: dump,
NumOption: cli.AtLeast(1),
}
func main() {
// default writer is os.Stdout
if err := cli.Root(root).Run(os.Args[1:]); err != nil {
fmt.Fprintln(os.Stderr, err)
}
fmt.Println("")
}
How to tweak above to accept none-option parameters?
The full code is at, https://gist.github.com/suntong/6658063cc0cf752f5d7ad5cc299f30bd
and when I run it, I got:
$ go run /tmp/dump.go aaa
ERR! command aaa not found
Please help. Thx.
Removing that NumOption: cli.AtLeast(1),
won't help either.
var root = &cli.Command{
Name: "dump",
Desc: "dump assistant program",
Text: "Swiss-army assistant toolkit",
Argv: func() interface{} { return new(rootT) },
CanSubRoute: true, // NOTE: add this flag
Fn: dump,
NumOption: cli.AtLeast(1),
}
Thanks a lot! working code updated in above url.