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
855 stars 30 forks source link

Error customization #85

Open silvioprog opened 2 years ago

silvioprog commented 2 years ago

Hi.

Consider the following example:

package main

import "github.com/integrii/flaggy"

func main() {
    // Declare variables and their defaults
    var stringFlag = "defaultValue"

    // Create the subcommand
    subcommand := flaggy.NewSubcommand("subcommandExample")

    // Add a flag to the subcommand
    subcommand.String(&stringFlag, "f", "flag", "A test string flag")

    // Add the subcommand to the parser at position 1
    flaggy.AttachSubcommand(subcommand, 1)

    // Parse the subcommand and all flags
    flaggy.Parse()

    // Use the flag
    println(stringFlag)
}

when calling this by % ./main -f a, it shows the following error:

mymain

  Usage:
    mymain [subcommandExample]

  Subcommands: 
    subcommandExample

  Flags: 
       --version   Displays the program version string.
    -h --help      Displays help with available flag, subcommand, and positional value parameters.

Unknown arguments supplied:  f a

so, is there any option to show the error as Unknown arguments supplied: -f a instead of Unknown arguments supplied: f a? I.e, replacing the empty space before f by the - symbol.

Edit: the same for Expected a following arg for flag f, but it did not exist, it would be nice an option to show Expected a following arg for flag -f, but it did not exist instead, and Expected a following arg for flag --flag, but it did not exist instead of Expected a following arg for flag flag, but it did not exist, and so on.

TIA

codenoid commented 4 months ago

it should be called like this: ./main f <value>, if you want a flag, try

flaggy.String(&stringFlag, "f", "flong", "desc")

then you can use ./main -f <value> command