aboutsummaryrefslogtreecommitdiff
path: root/flag.go
diff options
context:
space:
mode:
authorEric Paris <[email protected]>2015-09-16 11:25:42 -0400
committerEric Paris <[email protected]>2015-09-16 11:25:42 -0400
commitb25fea9a8363e6c84673209fa30d9e0023546599 (patch)
treea661c078ce95d5ddc36f5d0327ed36366511b9d3 /flag.go
parentf735fdff4ffb34299727eb2e3c9abab588742d41 (diff)
Store the length of Args when a -- is found in commandline
This allows a program to know what args came before the -- and what args came after.
Diffstat (limited to 'flag.go')
-rw-r--r--flag.go11
1 files changed, 11 insertions, 0 deletions
diff --git a/flag.go b/flag.go
index 66ef096..57d9339 100644
--- a/flag.go
+++ b/flag.go
@@ -140,6 +140,7 @@ type FlagSet struct {
formal map[NormalizedName]*Flag
shorthands map[byte]*Flag
args []string // arguments after flags
+ argsLenAtDash int // len(args) when a '--' was located when parsing, or -1 if no --
exitOnError bool // does the program exit if there's an error?
errorHandling ErrorHandling
output io.Writer // nil means stderr; use out() accessor
@@ -292,6 +293,13 @@ func (f *FlagSet) getFlagType(name string, ftype string, convFunc func(sval stri
return result, nil
}
+// ArgsLenAtDash will return the length of f.Args at the moment when a -- was
+// found during arg parsing. This allows your program to know which args were
+// before the -- and which came after.
+func (f *FlagSet) ArgsLenAtDash() int {
+ return f.argsLenAtDash
+}
+
// MarkDeprecated indicated that a flag is deprecated in your program. It will
// continue to function but will not show up in help or usage messages. Using
// this flag will also print the given usageMessage.
@@ -740,6 +748,7 @@ func (f *FlagSet) parseArgs(args []string) (err error) {
if s[1] == '-' {
if len(s) == 2 { // "--" terminates the flags
+ f.argsLenAtDash = len(f.args)
f.args = append(f.args, args...)
break
}
@@ -806,6 +815,7 @@ func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet {
f := &FlagSet{
name: name,
errorHandling: errorHandling,
+ argsLenAtDash: -1,
interspersed: true,
}
return f
@@ -822,4 +832,5 @@ func (f *FlagSet) SetInterspersed(interspersed bool) {
func (f *FlagSet) Init(name string, errorHandling ErrorHandling) {
f.name = name
f.errorHandling = errorHandling
+ f.argsLenAtDash = -1
}