-
Notifications
You must be signed in to change notification settings - Fork 366
Fix '--flag arg' and Allow flags to take optional arguments #20
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
Conversation
|
I see this as an alternative to #18 Both have the same results, just how and what is specified... Thoughts? |
|
Since 9294901 long arguments followed by a space do not work Test program: import (
"fmt"
"os"
"github.com/spf13/pflag"
)
func main() {
var flagvar int
pflag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
pflag.Parse()
fmt.Fprintf(os.Stdout, "val=%v\n", flagvar)
}So I decided to look at what ls does (and some other coreutils binaries), with 'optional' arguments. Optional arguments can not use the form
argument to ls --tabsize is not optional Given we already do not support That means the only place we do lose something is a flag which is both optional and provides a short form. Very few coreutils binaries have both optional and shortform flags. (mktemp, shred, split, tail, uniq) However they also do not allow We would, I think, want to turn on diff --git a/flag.go b/flag.go
index 9f16733..93222c8 100644
--- a/flag.go
+++ b/flag.go
@@ -530,11 +530,15 @@ func (f *FlagSet) parseLongArg(s string, args []string) (a []string, err error)
}
var value string
if len(split) == 1 {
- if bv, ok := flag.Value.(boolFlag); !ok || !bv.IsBoolFlag() {
+ if bv, ok := flag.Value.(boolFlag); ok && bv.IsBoolFlag() {
+ value = "true"
+ } else if len(a) > 0 {
+ value = a[0]
+ a = a[1:]
+ } else {
err = f.failf("flag needs an argument: %s", s)
return
}
- value = "true"
} else {
value = split[1]
} |
c6da467 to
ee1be60
Compare
|
At this point I'm feeling like I want to merge this. It is completely in line with what the ls command does. How much better of an example could we have to follow? |
|
@eparis I tested your changes (mainly the About the Thanks! |
This patch converts rkt to use the cobra cli library. It doesn't do any other change. For this reason the help/usage output is different from the previous one. Cobra internally uses the github.com/spf13/pflag library (forked from github.com/ogier/pflag, it's not a dropin replacement for stdlib flag packages due to an additional method Type() added to the Value interface). This library handles posix flags and interspersed flags (flags after non flag arguments). This means that long flags must start with a double dash (with package flag also a single dash is accepted). Until spf13/pflag#20 isn't fixed, an `=` between the long flag and its value is needed. Cobra adds the ability to merge the parent command flags (called persistent flags) to all the child commands. In this way there's no need to specify the global flags after the executable and before the command name. This patch also enables the handling of unambiguous not fully typed commands (like `rkt l` for `rkt list`). In rkt there're three special kind of arguments parsing, that needs special handling of the cobra/pflags default behavior: fetch: It can handle multiple images with an optional --signature option after every image. To avoid the --signature being parsed as a normal flag, interspersed flags must be disabled. enter: it handles an executable and optional arguments. To avoid these arguments being parsed as rkt flags, interspersed flags must be disabled. run/prepare: it can handle multiple images with image specific arguments. To do this the image arguments should be inserted between "--" and "---". This uses a special argument parsing to handle the "--" that usually makes the flag (and also pflag) libraries stop parsing flags, eating the "--" and returning everything after it as arguments to the relative command. To get the same behavior with the pflags library, interspersed flags must be disabled and the "--" must be readded to the command's arguments.
This patch converts rkt to use the cobra cli library. It doesn't do any other change. For this reason the help/usage output is different from the previous one. Cobra internally uses the github.com/spf13/pflag library (forked from github.com/ogier/pflag, it's not a dropin replacement for stdlib flag packages due to an additional method Type() added to the Value interface). This library handles posix flags and interspersed flags (flags after non flag arguments). This means that long flags must start with a double dash (with package flag also a single dash is accepted). Until spf13/pflag#20 isn't fixed, an `=` between the long flag and its value is needed. Cobra adds the ability to merge the parent command flags (called persistent flags) to all the child commands. In this way there's no need to specify the global flags after the executable and before the command name. This patch also enables the handling of unambiguous not fully typed commands (like `rkt l` for `rkt list`). In rkt there're three special kind of arguments parsing, that needs special handling of the cobra/pflags default behavior: fetch: It can handle multiple images with an optional --signature option after every image. To avoid the --signature being parsed as a normal flag, interspersed flags must be disabled. enter: it handles an executable and optional arguments. To avoid these arguments being parsed as rkt flags, interspersed flags must be disabled. run/prepare: it can handle multiple images with image specific arguments. To do this the image arguments should be inserted between "--" and "---". This uses a special argument parsing to handle the "--" that usually makes the flag (and also pflag) libraries stop parsing flags, eating the "--" and returning everything after it as arguments to the relative command. To get the same behavior with the pflags library, interspersed flags must be disabled and the "--" must be readded to the command's arguments.
This patch converts rkt to use the cobra cli library. It doesn't do any other change. For this reason the help/usage output is different from the previous one. Cobra internally uses the github.com/spf13/pflag library (forked from github.com/ogier/pflag, it's not a dropin replacement for stdlib flag packages due to an additional method Type() added to the Value interface). This library handles posix flags and interspersed flags (flags after non flag arguments). This means that long flags must start with a double dash (with package flag also a single dash is accepted). Until spf13/pflag#20 isn't fixed, an `=` between the long flag and its value is needed. Cobra adds the ability to merge the parent command flags (called persistent flags) to all the child commands. In this way there's no need to specify the global flags after the executable and before the command name. This patch also enables the handling of unambiguous not fully typed commands (like `rkt l` for `rkt list`). In rkt there're three special kind of arguments parsing, that needs special handling of the cobra/pflags default behavior: fetch: It can handle multiple images with an optional --signature option after every image. To avoid the --signature being parsed as a normal flag, interspersed flags must be disabled. enter: it handles an executable and optional arguments. To avoid these arguments being parsed as rkt flags, interspersed flags must be disabled. run/prepare: it can handle multiple images with image specific arguments. To do this the image arguments should be inserted between "--" and "---". This uses a special argument parsing to handle the "--" that usually makes the flag (and also pflag) libraries stop parsing flags, eating the "--" and returning everything after it as arguments to the relative command. To get the same behavior with the pflags library, interspersed flags must be disabled and the "--" must be readded to the command's arguments.
This patch converts rkt to use the cobra cli library. It doesn't do any other change. For this reason the help/usage output is different from the previous one. Cobra internally uses the github.com/spf13/pflag library (forked from github.com/ogier/pflag, it's not a dropin replacement for stdlib flag packages due to an additional method Type() added to the Value interface). This library handles posix flags and interspersed flags (flags after non flag arguments). This means that long flags must start with a double dash (with package flag also a single dash is accepted). Until spf13/pflag#20 isn't fixed, an `=` between the long flag and its value is needed. Cobra adds the ability to merge the parent command flags (called persistent flags) to all the child commands. In this way there's no need to specify the global flags after the executable and before the command name. This patch also enables the handling of unambiguous not fully typed commands (like `rkt l` for `rkt list`). In rkt there're three special kind of arguments parsing, that needs special handling of the cobra/pflags default behavior: fetch: It can handle multiple images with an optional --signature option after every image. To avoid the --signature being parsed as a normal flag, interspersed flags must be disabled. enter: it handles an executable and optional arguments. To avoid these arguments being parsed as rkt flags, interspersed flags must be disabled. run/prepare: it can handle multiple images with image specific arguments. To do this the image arguments should be inserted between "--" and "---". This uses a special argument parsing to handle the "--" that usually makes the flag (and also pflag) libraries stop parsing flags, eating the "--" and returning everything after it as arguments to the relative command. To get the same behavior with the pflags library, interspersed flags must be disabled and the "--" must be readded to the command's arguments.
This patch converts rkt to use the cobra cli library. It doesn't do any other change. For this reason the help/usage output is different from the previous one. Cobra internally uses the github.com/spf13/pflag library (forked from github.com/ogier/pflag, it's not a dropin replacement for stdlib flag packages due to an additional method Type() added to the Value interface). This library handles posix flags and interspersed flags (flags after non flag arguments). This means that long flags must start with a double dash (with package flag also a single dash is accepted). Until spf13/pflag#20 isn't fixed, an `=` between the long flag and its value is needed. Cobra adds the ability to merge the parent command flags (called persistent flags) to all the child commands. In this way there's no need to specify the global flags after the executable and before the command name. This patch also enables the handling of unambiguous not fully typed commands (like `rkt l` for `rkt list`). In rkt there're three special kind of arguments parsing, that needs special handling of the cobra/pflags default behavior: fetch: It can handle multiple images with an optional --signature option after every image. To avoid the --signature being parsed as a normal flag, interspersed flags must be disabled. enter: it handles an executable and optional arguments. To avoid these arguments being parsed as rkt flags, interspersed flags must be disabled. run/prepare: it can handle multiple images with image specific arguments. To do this the image arguments should be inserted between "--" and "---". This uses a special argument parsing to handle the "--" that usually makes the flag (and also pflag) libraries stop parsing flags, eating the "--" and returning everything after it as arguments to the relative command. To get the same behavior with the pflags library, interspersed flags must be disabled and the "--" must be readded to the command's arguments.
Fix '--flag arg' and Allow flags to take optional arguments
starting from spf13/pflag#20 it accepts also `--flag value` while before `--flag=value` was needed. Additionaly it supports default values for flags specified without an explicit value (`--flag`) (which is different from the default value for flags not passed as arguments). In that cae, the `--flag=value` is needed for flags that can have a default value because the parser won't know if the argument after `--flag` is the flag's value or another argument. This patches changed the way flags are handled and removed the use of the boolFlag interface as now default values can be provided for every type (not just bool) setting the NoOptDefVal variable. This patch fixes rkt to use the new behavior.
starting from spf13/pflag#20 it accepts also `--flag value` while before `--flag=value` was needed. Additionaly it supports default values for flags specified without an explicit value (`--flag`) (which is different from the default value for flags not passed as arguments). In that cae, the `--flag=value` is needed for flags that can have a default value because the parser won't know if the argument after `--flag` is the flag's value or another argument. That fix also changed the way flags are handled and removed the use of the boolFlag interface as now default values can be provided for every type (not just bool) by setting the NoOptDefVal variable. This patch fixes rkt to use the new behavior.
starting from spf13/pflag#20 it accepts also `--flag value` while before `--flag=value` was needed. Additionaly it supports default values for flags specified without an explicit value (`--flag`) (which is different from the default value for flags not passed as arguments). In that case, the `--flag=value` is needed for flags that can have a default value because the parser won't know if the argument after `--flag` is the flag's value or another argument. That fix also changed the way flags are handled and removed the use of the boolFlag interface as now default values can be provided for every type (not just bool) by setting the NoOptDefVal variable. This patch fixes rkt to use the new behavior.
starting from spf13/pflag#20 it accepts also `--flag value` while before `--flag=value` was needed. Additionally it supports default values for flags specified without an explicit value (`--flag`) (which is different from the default value for flags not passed as arguments). In that case, the `--flag=value` is needed for flags that can have a default value because the parser won't know if the argument after `--flag` is the flag's value or another argument. That fix also changed the way flags are handled and removed the use of the boolFlag interface as now default values can be provided for every type (not just bool) by setting the NoOptDefVal variable. This patch fixes rkt to use the new behavior.
Today
--flag argdoes not work. It just fails to parse. So fix that.At the same time allow flags to specify that their args are 'optional'. Much as with a bool =true =false are optional. So one could do
In which case
--flagand--flag=20would be valid, but--flag 20would not work.