mkideal / cli

CLI - A package for building command line app with go
MIT License
732 stars 43 forks source link

NeedArgs clarification #11

Closed suntong closed 8 years ago

suntong commented 8 years ago

Hey 仕晋,

Does NeedArgs means need argument or need option?

Please see my demo how to handle none-option parameters, in which I added a none-option parameters to build:

Build golang application

Usage:
  gogo build [Options] Arch(i386|amd64)

It runs fine:

$ gogo build --dir=.. -o out i386
build: {
    "Help": false,
    "Dir": "..",
    "Suffix": ".go,.c,.s",
    "Out": "out"
}
Arch: i386

However, my code would panic if the Arch none-option parameter is not specified. So we need clarification here. Once again, rsync takes an awful lot of options, but besides them, at the end of command line, there are none-option parameters, normally called argument. I hope that NeedArgs really means this, which goes with common understanding and all other commands.

Thanks

mkideal commented 8 years ago

Ok ,I will fix it.

suntong commented 8 years ago

Many thanks!

mkideal commented 8 years ago

Hey. I removed NeedArgs. Now, NumArg added. See commit 0a75f3d and example_narg_test.go

suntong commented 8 years ago

That's super! Amazingly thorough!!

suntong commented 8 years ago

Please take a look at my

allow multiple arguments (none-option parameters) for install

I think I've done it right, but this is what I got:

$ gogo install pkg1 pkg2 pkg3
ERR! command install pkg1 pkg2 pkg3 not found
mkideal commented 8 years ago

In this case, you should set CanSubRoute true for command install, otherwise CLI will try find the command install->pkg1->pkg2->pkg3

mkideal commented 8 years ago
var _ = app.Register(&cli.Command{
    Name: "install",
    Desc: "Install golang application",
    Text: "Usage:\n  gogo install [Options] package [package...]",
    Argv: func() interface{} { return new(installT) },
    Fn:   install,
    CanSubRoute: true,
    NumArg: cli.AtLeast(1),
  })
suntong commented 8 years ago

Thanks. Works great.

Hmm.., should CanSubRoute true affect ctx.Path() as well?

var _ = app.Register(&cli.Command{
    Name: "install",
    Desc: "Install golang application",
    Text: "Usage:\n  gogo install [Options] package [package...]",
    Argv: func() interface{} { return new(installT) },
    Fn:   install,

    NumArg:      cli.AtLeast(1),
    CanSubRoute: true,
})

...

    ctx.String("%s: %v", ctx.Path(), jsonIndent(argv))
$ gogo install pkg1 pkg2 pkg3
install pkg1 pkg2 pkg3: {
^^^^^^^^^^^^^^^^^^^^^^
    "Help": false,
    "Dir": "./",
    "Suffix": ".go,.c,.s",
    "Out": ""
}
Installing: [pkg1 pkg2 pkg3]
mkideal commented 8 years ago

😭,This is a bug!

suntong commented 8 years ago

hehe, glad that I can help squashing some. :)

mkideal commented 8 years ago

I should write more tests.

suntong commented 8 years ago

You "should" have more people to use it. haha...

Don't worry, they will. :)