jawher/mow.cli

unsuccessful parsing in case of nested command

the-shank opened this issue · 3 comments

I am facing a wierd issue. Considering the following code

package main

import (
    "log"
    "os"

    "github.com/jawher/mow.cli"
)

func main() {
    mycli := cli.App("mycli", "")

    mycli.Command("labels", "Work with labels in this project", func(labelsCmd *cli.Cmd) {

        labelsCmd.Command("create", "Create label", func(createCmd *cli.Cmd) {
            createCmd.Spec = "(--title | TITLE) (--color)"

            var titleOpt = createCmd.StringOpt("t title", "", "title of the label")
            var titleArg = createCmd.StringArg("TITLE", "", "title of the label")
            var color = createCmd.StringOpt("c color", "", "color of the label, in hex notation")

            log.Println("color:", *color)
            log.Println("titleOpt:", *titleOpt)
            log.Println("titleArg:", *titleArg)

            createCmd.Action = func() {
                log.Println("executing create command")
            }
        })
    })

    mycli.Run(os.Args)
}

I then compile using go build .
Then I run the command ./mycli labels create -t test -c red
The output that I am getting is

2016/10/24 22:13:20 color: 
2016/10/24 22:13:20 titleOpt: 
2016/10/24 22:13:20 titleArg: 
2016/10/24 22:13:20 executing create command

So basically it is failing to parse the arguments that I am providing. I am unable to locate the issue. Any help would be much appreciated.

BTW, thanks for this amazing library !

Hi @the-shank !

This is not a bug in mow.cli nor an issue with your spec string.
The problem is that you're accessing the options and args values outside of the Action function, before mow.cli has had any chance to actually parse the passed arguments.

Simply move your log statements inside the Action and that should fix the problem.

@jawher Thank you for the prompt response ! And apologies for not checking more carefully !

@the-shank No worries 👍