-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
StringSliceFlags has strange behavior #790
Comments
This is I imagine some unintended and untested behavior currently. I only tested it in the master branch with the following but put together a small command for easy validation later. I agree that both the short and the long form of the flag should show up as IsSet in the case you have shown above. I'm trying to think through an example where this would cause unintended behavior but am not able to at the moment. I guess the easy work around for the time being would be to to check if both are set and then just use the values appended to each other. package main
import (
"fmt"
"log"
"os"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Flags = []cli.Flag{
cli.StringSliceFlag{
Name: "Hostnames, n",
Usage: "`HOSTNAME`s to listen to.",
Value: &cli.StringSlice{"localhost"},
},
cli.StringFlag{
Name: "lang, l",
Value: "english",
Usage: "language for the greeting",
},
}
app.Action = func(c *cli.Context) error {
fmt.Println("Hostname is set ...", c.IsSet("Hostnames"))
fmt.Println("n is set ...", c.IsSet("n"))
fmt.Println("land is set ...", c.IsSet("lang"))
fmt.Println("l is set ...", c.IsSet("l"))
return nil
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
} myuser@gentoo ~/code/collins-go-cli $ go run lol.go -n somehost
Hostname is set ... false
n is set ... true
land is set ... false
l is set ... false Here is conformation on the above that non slice values do work as you would expect. myuser@gentoo ~/code/collins-go-cli $ go run lol.go -Hostnames somehost -l hi
Hostname is set ... true
n is set ... false
land is set ... true
l is set ... true |
That also got me super confused today, glad I'm not the only one 😅 |
Same problem on my end. It also prevents me from using |
This issue or PR has been automatically marked as stale because it has not had recent activity. Please add a comment bumping this if you're still interested in it's resolution! Thanks for your help, please let us know if you need anything else. |
Can someone put together a feature request or bug report (templates are here) describing what should actually be done here? An issue titled "X has strange behavior" is hard to triage. |
This issue or PR has been bumped and is no longer marked as stale! Feel free to bump it again in the future, if it's still relevant. |
This issue or PR has been automatically marked as stale because it has not had recent activity. Please add a comment bumping this if you're still interested in it's resolution! Thanks for your help, please let us know if you need anything else. |
Closing this as it has become stale. |
As far as I can tell, this is no longer a problem on current package main
import (
"fmt"
"log"
"os"
"github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
Flags: []cli.Flag{
&cli.StringSliceFlag{
Name: "Hostnames",
Aliases: []string{"n"},
Usage: "`HOSTNAME`s to listen to.",
Value: cli.NewStringSlice("localhost"),
},
&cli.StringFlag{
Name: "lang",
Aliases: []string{"l"},
Value: "english",
Usage: "language for the greeting",
},
},
Action: func(c *cli.Context) error {
fmt.Println("Hostname is set ...", c.IsSet("Hostnames"))
fmt.Println("n is set ...", c.IsSet("n"))
fmt.Println("land is set ...", c.IsSet("lang"))
fmt.Println("l is set ...", c.IsSet("l"))
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
Please holler if this problem is still affecting you on |
Using:
Running
invocation -n somehost
returns[localhost somehost]
but I would expect it to return only[somehost]
.invocation -n somehost
thencontext.IsSet("Hostnames") == false
, however,context.IsSet("n") == true
. This is different from the behavior of the default values, such as Int or String where IsSet would return true whether the alternative switchname was used or not.The text was updated successfully, but these errors were encountered: