-
Notifications
You must be signed in to change notification settings - Fork 60
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 ignored after positional arguments (v4 alpha) #124
Comments
Ran into the same today, would be handy to be able to specify the flags either way. |
Is |
It depends how the |
What about the case when Or, consider
Do we parse |
Fair point, I hadn't noticed that Boolean flags could take a value. Intuitively I'd expect anything that looks like a flag ( Of course if the library deliberately forces flags to come before any positional argument this is also fine, as long as this design decision is clearly communicated to consumers :) |
This comment was marked as outdated.
This comment was marked as outdated.
edit: I see in your OP that you're working with v4, mea culpa |
Hi, I'm using ff v4, and think I hit the same issue. Below is the code which can be used to reproduce the problem (click to expand):package main
import (
"context"
"errors"
"fmt"
"os"
"github.com/peterbourgon/ff/v4"
"github.com/peterbourgon/ff/v4/ffhelp"
)
func cli() int {
root := ff.NewFlagSet("example")
debug := root.Bool('D', "debug", "enable debug mode")
exampleCmd := &ff.Command{
Name: "example",
Usage: "example [FLAGS] SUBCOMMAND ...",
Flags: root,
}
// example server
serverFlags := ff.NewFlagSet("server").SetParent(root)
serverCmd := &ff.Command{
Name: "server",
Usage: "example server",
ShortHelp: "run example server in foreground",
Flags: serverFlags,
Exec: func(ctx context.Context, args []string) error {
directory := ""
if len(args) < 1 {
return fmt.Errorf("missing directory")
}
directory = args[0]
if *debug {
fmt.Printf("starting server in directory: %s\n", directory)
}
return nil
},
}
exampleCmd.Subcommands = append(exampleCmd.Subcommands, serverCmd)
err := exampleCmd.ParseAndRun(context.Background(), os.Args[1:])
switch {
case errors.Is(err, ff.ErrHelp):
ffhelp.Command(exampleCmd).WriteTo(os.Stderr)
return 0
case err != nil:
fmt.Fprintf(os.Stderr, "error: %v\n", err)
return 1
default:
return 0
}
}
func main() {
os.Exit(cli())
}
I could not understand reading this thread whether this was the intended behavior or not. Do you believe that it should be possible to do that with Anyway thanks this awesome library |
I decided to kick the tires on I respect your position, but it'd be great if you could reconsider this particular one. In practice, the most common thing is being able to mix and match flags and args. If it quaks like a flag, it's registered as a flag, then it's probably a flag (and that's good enough for 99% of cases!). There will always be some edge cases, and maybe for those situations there could be an escape hatch to "parse as raw" deferring all the logic to the CLI author. |
Just to peel back the curtain a bit: I definitely want to make this work. I've been stuck on how to do that without completely re-implementing how flags, flag-sets, args, etc. are parsed. |
I've gotten around this with the standard library Pushed an example here, https://github.com/mfridman/xflag Is it possible to add a similar function to |
Since For me, |
While experimenting with
v4.0.0-alpha.4
for a toy project, I noticed that flags located after any positional argument were ignored during parsing.The issue can be reproduced with the
objectctl
example on the latest tag: https://github.com/peterbourgon/ff/tree/v4.0.0-alpha.4/examples/objectctlExamples:
To provide more context about what I was originally trying to do:
I have a command with a global flag
--verbose
/-v
which enables verbose logging.Sometimes, the invocation of a command will fail and return the final error with some context, but not all. When that happens, I typically re-call the same command from my shell history, and append
-v
before pressing Enter to replay the actions with more details, such as the API calls that are being performed, etc.With a command implemented with
ff
, I need instead to travel before the subcommand's positional arguments to add-v
.The text was updated successfully, but these errors were encountered: