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

program string argument without the value doesn't appear to work #67

Closed syacko closed 4 years ago

syacko commented 4 years ago

Based on the document, I should be able to have argument defined with a default. If no value is provided the default should be applied. Example: ./command -g all should work just like ./command -g when -g has a default of all. My code:

var genDoc string = "ALL"

func init() {
    flaggy.SetName("sdocs - Sote Docs")
    flaggy.SetDescription("sdocs will output markdown syntax for packages contenting publishable content")

    // You can disable various things by changing bools on the default parser
    // (or your own parser if you have created one).
    flaggy.DefaultParser.ShowHelpOnUnexpected = false

    // You can set a help prepend or append on the default parser.
    flaggy.DefaultParser.AdditionalHelpPrepend = "http://gitlab.com/getsote/utils"

    // Add a flag to the main program (this will be available in all subcommands as well).
    flaggy.Bool(&listVar, "l", "list", "This list the packages that are supported by this utility")
    flaggy.String(&genDoc, "g", "gen", "-g <package name> Generate docs from support packages")

    // Set the version and parse all inputs into variables.
    flaggy.SetVersion(version)
    flaggy.Parse()
}

When I run with ./sdoc -g, I get the following:

/Users/syacko/workspace/sotesoft/src/utils/sdocs/sdocs -g #gosetup
sdocs - Sote Docs - sdocs will output markdown syntax for packages contenting publishable content
http://gitlab.com/getsote/utils

  Flags: 
       --version   Displays the program version string.
    -h --help      Displays help with available flag, subcommand, and positional value parameters.
    -l --list      This list the packages that are supported by this utility
    -g --gen       -g <package name> Generate docs from support packages

Expected a following arg for flag g, but it did not exist.

Process finished with exit code 2

When I run /sdoc -g ALL, I get the following:

/Users/syacko/workspace/sotesoft/src/utils/sdocs/sdocs -g ALL #gosetup
You selected All

Process finished with exit code 0
integrii commented 4 years ago

Hey Scott. If you don't pass any flag, flaggy should leave the value alone. It's only when you specify the flag but don't provide a matching value for it that flaggy shows help (as in your example).

Using no flag will cause the flag's variable to not be modified. Passing a flag without a value after it will cause an error and show help (unless it was a boolean flag). Passing a flag and value after it overwrites the default value on the flag's variable with whatever was specified.

Does that make sense?

syacko commented 4 years ago

Ok, that makes sense. To recap, if there is no flag then the default is used. Otherwise, the existence of the flag means that a value must be provided.