akamensky / argparse

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

Can't have duplicate switches in different commands? #79

Closed lespea closed 3 years ago

lespea commented 3 years ago

If different commands both have switches that share the same short or long value, it errors with unable to add String: short name {} occurs more than once. This completely renders this library unusable for me unfortunately as many of my sub-commands do share the same switches (with different uses). Is this something that would be easy to fix or should I look for another library?

akamensky commented 3 years ago

@lespea can you please provide minimal code that shows this bug? If the subcommands that use same short-name and long-name are on different branches of the command tree, there should be no such error and then it would be a bug.

akamensky commented 3 years ago

@lespea I have just tested and I don't see this issue at all:

package main

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

func main() {
    p := argparse.NewParser("test", "test")

    c1 := p.NewCommand("c1", "c1")
    c2 := p.NewCommand("c2", "c2")

    arg1 := c1.String("s", "str", &argparse.Options{
        Help:    "arg1 in c1",
        Default: "arg1",
    })
    arg2 := c2.String("s", "str", &argparse.Options{
        Help:    "arg2 in c2",
        Default: "arg2",
    })

    err := p.Parse(os.Args)
    if err != nil {
        panic(err)
    }

    if c1.Happened() {
        fmt.Println(*arg1)
    } else if c2.Happened() {
        fmt.Println(*arg2)
    } else {
        fmt.Println("pick either c1 or c2")
    }
}

The result of running:

(base) [alexey.kamenskiy@macbook-pro:test]$ go run main.go c1
arg1
(base) [alexey.kamenskiy@macbook-pro:test]$ go run main.go c2
arg2
(base) [alexey.kamenskiy@macbook-pro:test]$

I imagine that you trying to add the same argument short-name or long-name within the SAME branch of subcommand tree, that is not going to work.

lespea commented 3 years ago

Hmm interesting, I must have really messed something up. Let me check when I get back to work on Monday and see what I did. As far as I could tell I was adding switches to different sub-commands but I must have done something weird.

lespea commented 3 years ago

Okay my previous test I messed up. I found in my source code where I was accidentally adding one of the args to the parent Parser object not the Command objec). Sorry for wasting your time.