akamensky / argparse

Argparse for golang. Just because `flag` sucks
MIT License
604 stars 62 forks source link

Options.Default should be ignored for Flag #98

Closed jasonwee closed 2 years ago

jasonwee commented 2 years ago
package main

import (
    "fmt"
    "github.com/akamensky/argparse"
    "os"
)

func main() {
   parser := argparse.NewParser("accounts", "Prints provided string to stdout")
   noTest := parser.Flag("n", "no-test", &argparse.Options{Help: "boolean true/false", Default: true})

   err := parser.Parse(os.Args)
   if err != nil {
      fmt.Print(parser.Usage(err))
   }
   fmt.Println(*noTest)
}

why is this always true? the second should be false?

$ go run main.go 
true
$ go run main.go -n
true
$
akamensky commented 2 years ago

Using Options.Default with Flag doesn't make much sense.

If flag is not provided it is false (default boolean value), if provided it is true.

Setting default to true makes it to be always true.

I agree it can be improved by simply ignoring Options.Default for Flag.

jasonwee commented 2 years ago

Thanks for considering

akamensky commented 2 years ago

Resolved in #103

jasonwee commented 2 years ago

will upgrade and test this feature, thank you!