-
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
Flags are treated as Args #991
Comments
👍 @akashdobaria this issue here makes sense, marking it was |
@akashdobaria if you have some extra time on your hands, you're totally free to work on the fix for this issue yourself! otherwise, I'm sure someone will swing around to work on it eventually 👀 |
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. |
This is still waiting for someone to help out with it 🙏 |
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. |
Did a little bit of investigation on this issue The problem here is that if the first value passed is an arg not a flag, the flag won't even be parsed as a flag, but as an argument. package main
import (
"log"
"os"
"github.com/urfave/cli/v2"
)
func main() {
app := cli.NewApp()
app.Commands = []*cli.Command{
{
Name: "init",
Usage: "Establish a connection",
ArgsUsage: "<host url>",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"V"},
Usage: "Be verbose",
},
},
Action: func(c *cli.Context) error {
fmt.Printf("Args : %v\n", c.Args())
fmt.Println(c.Bool("verbose"))
return nil
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
} Running with flag first works fine :
If the argument is passed first, the flag is not even parsed:
On V1 it did work with flag being at the end, but the question here would be, isn't current behavior intended?
Help message in usage shows that options(flags) come before arguments, so it's working as intended? |
i have same issue, i can't parse a flag in command |
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. |
my urfave/cli version is
2(recently updated with the master)
Checklist
Dependency Management
Describe the bug
I am using some flags in the command.
The Init() method:
If I run
cli init http://localhost:8090 -V
, I get the length of the arguments as 2 which shouldn't be the case as I would be checking if the user has passed the correct number of arguments for the command.I have been using V1 earlier. I have read the V2 docs, but didn't find any way other than using
SkipFlagParsing: false
. For V1 it was working perfectly as expected.To reproduce
Try creating any command, which requires an argument as above, with the flags.
Expected behavior
When used Context.Args(), it should not include any flags.
For example when run with
cli init http://localhost:8090 -V
, Context.c.NArg() should return 1 instead of 2.Additional context
Add any other context about the problem here.
If the issue relates to a specific open source Github repo, please link that repo here.
If you can reproduce this issue with a public CI system, please link a failing build here.
Want to fix this yourself?
We'd love to have more contributors on this project! If the fix for this bug is easily explained and very small, free free to create a pull request for it.
Run
go version
and paste its output hereRun
go env
and paste its output hereThe text was updated successfully, but these errors were encountered: