integrii / flaggy

Idiomatic Go input parsing with subcommands, positional values, and flags at any position. No required project or package layout and no external dependencies.
The Unlicense
856 stars 30 forks source link

How do you use the ShowHelpOnUnexpected option for DefaultParser? #47

Closed atc0005 closed 4 years ago

atc0005 commented 5 years ago

I tried using the option as shown here:

https://github.com/integrii/flaggy/blob/master/README.md#recommended-program-structure

and with tag 1.2.2 (425304205580254c6ea87908f35848c46eb99d5e) I do see the help output when I use an invalid flag, but only if I don't specify a value for the flag when I specify the flag.

Example program:

package main

import (
    "fmt"

    "github.com/integrii/flaggy"
)

func main() {

    optBool := false
    optIntegerTestVar := 1
    optStringTestVar := "My test string"

    flaggy.SetName("Test")
    flaggy.SetDescription("Short example to illustrate issue")

    flaggy.DefaultParser.ShowHelpOnUnexpected = true

    // Add flags
    flaggy.Bool(&optBool, "bo", "bool", "Boolean test option")
    flaggy.Int(&optIntegerTestVar, "int", "inttest", "Integer test option")
    flaggy.String(&optStringTestVar, "str", "stringtest", "String test option")

    // Parse the flag
    flaggy.Parse()

    fmt.Printf("%v\n", optBool)
    fmt.Printf("%v\n", optIntegerTestVar)
    fmt.Printf("%v\n", optStringTestVar)

}

Example usage:

$ go run main.go -warning
Test - Short example to illustrate issue

  Flags:
       --version  Displays the program version string.
    -h --help  Displays help with available flag, subcommand, and positional value parameters.
    -bo --bool  Boolean test option
    -int --inttest  Integer test option (default: 1)
    -str --stringtest  String test option (default: My test string)

Expected a following arg for flag warning, but it did not exist.
exit status 2
$ go run main.go -warning=no
false
1
My test string
$ go run main.go --warning=no
false
1
My test string
integrii commented 5 years ago

I need to look into this. This may be a bug.

atc0005 commented 5 years ago

I need to look into this. This may be a bug.

Thanks!

ravenpride commented 4 years ago

@integrii Is there any progress fixing the bug? If you are busy, I could possibly help.

integrii commented 4 years ago

I keep meaning to look at this... I welcome any help @ravenpride!

ravenpride commented 4 years ago

The issue should be fixed. Please see PR #54.

integrii commented 4 years ago

I took a look at your stuff @ravenpride and I appreciate your help. However, I dove in and I think PR #55 fixes this in a slightly more simple way. Let me know what you guys think and I'll merge it.

integrii commented 4 years ago

You can actually see here (in the current live code) that I put in a comment describing what should go below, but never actually wrote it! The PR just fills it in:

https://github.com/integrii/flaggy/blob/master/subCommand.go#L163

ravenpride commented 4 years ago

I see, but could you please have a look at my comments regarding PR #55 ?

integrii commented 4 years ago

Launched in release 1.4.0!