| 1 |
747 |
jeremybenn |
// Copyright 2009 The Go Authors. All rights reserved.
|
| 2 |
|
|
// Use of this source code is governed by a BSD-style
|
| 3 |
|
|
// license that can be found in the LICENSE file.
|
| 4 |
|
|
|
| 5 |
|
|
/*
|
| 6 |
|
|
Package flag implements command-line flag parsing.
|
| 7 |
|
|
|
| 8 |
|
|
Usage:
|
| 9 |
|
|
|
| 10 |
|
|
Define flags using flag.String(), Bool(), Int(), etc. Example:
|
| 11 |
|
|
import "flag"
|
| 12 |
|
|
var ip *int = flag.Int("flagname", 1234, "help message for flagname")
|
| 13 |
|
|
If you like, you can bind the flag to a variable using the Var() functions.
|
| 14 |
|
|
var flagvar int
|
| 15 |
|
|
func init() {
|
| 16 |
|
|
flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
|
| 17 |
|
|
}
|
| 18 |
|
|
Or you can create custom flags that satisfy the Value interface (with
|
| 19 |
|
|
pointer receivers) and couple them to flag parsing by
|
| 20 |
|
|
flag.Var(&flagVal, "name", "help message for flagname")
|
| 21 |
|
|
For such flags, the default value is just the initial value of the variable.
|
| 22 |
|
|
|
| 23 |
|
|
After all flags are defined, call
|
| 24 |
|
|
flag.Parse()
|
| 25 |
|
|
to parse the command line into the defined flags.
|
| 26 |
|
|
|
| 27 |
|
|
Flags may then be used directly. If you're using the flags themselves,
|
| 28 |
|
|
they are all pointers; if you bind to variables, they're values.
|
| 29 |
|
|
fmt.Println("ip has value ", *ip);
|
| 30 |
|
|
fmt.Println("flagvar has value ", flagvar);
|
| 31 |
|
|
|
| 32 |
|
|
After parsing, the arguments after the flag are available as the
|
| 33 |
|
|
slice flag.Args() or individually as flag.Arg(i).
|
| 34 |
|
|
The arguments are indexed from 0 up to flag.NArg().
|
| 35 |
|
|
|
| 36 |
|
|
Command line flag syntax:
|
| 37 |
|
|
-flag
|
| 38 |
|
|
-flag=x
|
| 39 |
|
|
-flag x // non-boolean flags only
|
| 40 |
|
|
One or two minus signs may be used; they are equivalent.
|
| 41 |
|
|
The last form is not permitted for boolean flags because the
|
| 42 |
|
|
meaning of the command
|
| 43 |
|
|
cmd -x *
|
| 44 |
|
|
will change if there is a file called 0, false, etc. You must
|
| 45 |
|
|
use the -flag=false form to turn off a boolean flag.
|
| 46 |
|
|
|
| 47 |
|
|
Flag parsing stops just before the first non-flag argument
|
| 48 |
|
|
("-" is a non-flag argument) or after the terminator "--".
|
| 49 |
|
|
|
| 50 |
|
|
Integer flags accept 1234, 0664, 0x1234 and may be negative.
|
| 51 |
|
|
Boolean flags may be 1, 0, t, f, true, false, TRUE, FALSE, True, False.
|
| 52 |
|
|
Duration flags accept any input valid for time.ParseDuration.
|
| 53 |
|
|
|
| 54 |
|
|
The default set of command-line flags is controlled by
|
| 55 |
|
|
top-level functions. The FlagSet type allows one to define
|
| 56 |
|
|
independent sets of flags, such as to implement subcommands
|
| 57 |
|
|
in a command-line interface. The methods of FlagSet are
|
| 58 |
|
|
analogous to the top-level functions for the command-line
|
| 59 |
|
|
flag set.
|
| 60 |
|
|
*/
|
| 61 |
|
|
package flag
|
| 62 |
|
|
|
| 63 |
|
|
import (
|
| 64 |
|
|
"errors"
|
| 65 |
|
|
"fmt"
|
| 66 |
|
|
"io"
|
| 67 |
|
|
"os"
|
| 68 |
|
|
"sort"
|
| 69 |
|
|
"strconv"
|
| 70 |
|
|
"time"
|
| 71 |
|
|
)
|
| 72 |
|
|
|
| 73 |
|
|
// ErrHelp is the error returned if the flag -help is invoked but no such flag is defined.
|
| 74 |
|
|
var ErrHelp = errors.New("flag: help requested")
|
| 75 |
|
|
|
| 76 |
|
|
// -- bool Value
|
| 77 |
|
|
type boolValue bool
|
| 78 |
|
|
|
| 79 |
|
|
func newBoolValue(val bool, p *bool) *boolValue {
|
| 80 |
|
|
*p = val
|
| 81 |
|
|
return (*boolValue)(p)
|
| 82 |
|
|
}
|
| 83 |
|
|
|
| 84 |
|
|
func (b *boolValue) Set(s string) error {
|
| 85 |
|
|
v, err := strconv.ParseBool(s)
|
| 86 |
|
|
*b = boolValue(v)
|
| 87 |
|
|
return err
|
| 88 |
|
|
}
|
| 89 |
|
|
|
| 90 |
|
|
func (b *boolValue) String() string { return fmt.Sprintf("%v", *b) }
|
| 91 |
|
|
|
| 92 |
|
|
// -- int Value
|
| 93 |
|
|
type intValue int
|
| 94 |
|
|
|
| 95 |
|
|
func newIntValue(val int, p *int) *intValue {
|
| 96 |
|
|
*p = val
|
| 97 |
|
|
return (*intValue)(p)
|
| 98 |
|
|
}
|
| 99 |
|
|
|
| 100 |
|
|
func (i *intValue) Set(s string) error {
|
| 101 |
|
|
v, err := strconv.ParseInt(s, 0, 64)
|
| 102 |
|
|
*i = intValue(v)
|
| 103 |
|
|
return err
|
| 104 |
|
|
}
|
| 105 |
|
|
|
| 106 |
|
|
func (i *intValue) String() string { return fmt.Sprintf("%v", *i) }
|
| 107 |
|
|
|
| 108 |
|
|
// -- int64 Value
|
| 109 |
|
|
type int64Value int64
|
| 110 |
|
|
|
| 111 |
|
|
func newInt64Value(val int64, p *int64) *int64Value {
|
| 112 |
|
|
*p = val
|
| 113 |
|
|
return (*int64Value)(p)
|
| 114 |
|
|
}
|
| 115 |
|
|
|
| 116 |
|
|
func (i *int64Value) Set(s string) error {
|
| 117 |
|
|
v, err := strconv.ParseInt(s, 0, 64)
|
| 118 |
|
|
*i = int64Value(v)
|
| 119 |
|
|
return err
|
| 120 |
|
|
}
|
| 121 |
|
|
|
| 122 |
|
|
func (i *int64Value) String() string { return fmt.Sprintf("%v", *i) }
|
| 123 |
|
|
|
| 124 |
|
|
// -- uint Value
|
| 125 |
|
|
type uintValue uint
|
| 126 |
|
|
|
| 127 |
|
|
func newUintValue(val uint, p *uint) *uintValue {
|
| 128 |
|
|
*p = val
|
| 129 |
|
|
return (*uintValue)(p)
|
| 130 |
|
|
}
|
| 131 |
|
|
|
| 132 |
|
|
func (i *uintValue) Set(s string) error {
|
| 133 |
|
|
v, err := strconv.ParseUint(s, 0, 64)
|
| 134 |
|
|
*i = uintValue(v)
|
| 135 |
|
|
return err
|
| 136 |
|
|
}
|
| 137 |
|
|
|
| 138 |
|
|
func (i *uintValue) String() string { return fmt.Sprintf("%v", *i) }
|
| 139 |
|
|
|
| 140 |
|
|
// -- uint64 Value
|
| 141 |
|
|
type uint64Value uint64
|
| 142 |
|
|
|
| 143 |
|
|
func newUint64Value(val uint64, p *uint64) *uint64Value {
|
| 144 |
|
|
*p = val
|
| 145 |
|
|
return (*uint64Value)(p)
|
| 146 |
|
|
}
|
| 147 |
|
|
|
| 148 |
|
|
func (i *uint64Value) Set(s string) error {
|
| 149 |
|
|
v, err := strconv.ParseUint(s, 0, 64)
|
| 150 |
|
|
*i = uint64Value(v)
|
| 151 |
|
|
return err
|
| 152 |
|
|
}
|
| 153 |
|
|
|
| 154 |
|
|
func (i *uint64Value) String() string { return fmt.Sprintf("%v", *i) }
|
| 155 |
|
|
|
| 156 |
|
|
// -- string Value
|
| 157 |
|
|
type stringValue string
|
| 158 |
|
|
|
| 159 |
|
|
func newStringValue(val string, p *string) *stringValue {
|
| 160 |
|
|
*p = val
|
| 161 |
|
|
return (*stringValue)(p)
|
| 162 |
|
|
}
|
| 163 |
|
|
|
| 164 |
|
|
func (s *stringValue) Set(val string) error {
|
| 165 |
|
|
*s = stringValue(val)
|
| 166 |
|
|
return nil
|
| 167 |
|
|
}
|
| 168 |
|
|
|
| 169 |
|
|
func (s *stringValue) String() string { return fmt.Sprintf("%s", *s) }
|
| 170 |
|
|
|
| 171 |
|
|
// -- float64 Value
|
| 172 |
|
|
type float64Value float64
|
| 173 |
|
|
|
| 174 |
|
|
func newFloat64Value(val float64, p *float64) *float64Value {
|
| 175 |
|
|
*p = val
|
| 176 |
|
|
return (*float64Value)(p)
|
| 177 |
|
|
}
|
| 178 |
|
|
|
| 179 |
|
|
func (f *float64Value) Set(s string) error {
|
| 180 |
|
|
v, err := strconv.ParseFloat(s, 64)
|
| 181 |
|
|
*f = float64Value(v)
|
| 182 |
|
|
return err
|
| 183 |
|
|
}
|
| 184 |
|
|
|
| 185 |
|
|
func (f *float64Value) String() string { return fmt.Sprintf("%v", *f) }
|
| 186 |
|
|
|
| 187 |
|
|
// -- time.Duration Value
|
| 188 |
|
|
type durationValue time.Duration
|
| 189 |
|
|
|
| 190 |
|
|
func newDurationValue(val time.Duration, p *time.Duration) *durationValue {
|
| 191 |
|
|
*p = val
|
| 192 |
|
|
return (*durationValue)(p)
|
| 193 |
|
|
}
|
| 194 |
|
|
|
| 195 |
|
|
func (d *durationValue) Set(s string) error {
|
| 196 |
|
|
v, err := time.ParseDuration(s)
|
| 197 |
|
|
*d = durationValue(v)
|
| 198 |
|
|
return err
|
| 199 |
|
|
}
|
| 200 |
|
|
|
| 201 |
|
|
func (d *durationValue) String() string { return (*time.Duration)(d).String() }
|
| 202 |
|
|
|
| 203 |
|
|
// Value is the interface to the dynamic value stored in a flag.
|
| 204 |
|
|
// (The default value is represented as a string.)
|
| 205 |
|
|
type Value interface {
|
| 206 |
|
|
String() string
|
| 207 |
|
|
Set(string) error
|
| 208 |
|
|
}
|
| 209 |
|
|
|
| 210 |
|
|
// ErrorHandling defines how to handle flag parsing errors.
|
| 211 |
|
|
type ErrorHandling int
|
| 212 |
|
|
|
| 213 |
|
|
const (
|
| 214 |
|
|
ContinueOnError ErrorHandling = iota
|
| 215 |
|
|
ExitOnError
|
| 216 |
|
|
PanicOnError
|
| 217 |
|
|
)
|
| 218 |
|
|
|
| 219 |
|
|
// A FlagSet represents a set of defined flags.
|
| 220 |
|
|
type FlagSet struct {
|
| 221 |
|
|
// Usage is the function called when an error occurs while parsing flags.
|
| 222 |
|
|
// The field is a function (not a method) that may be changed to point to
|
| 223 |
|
|
// a custom error handler.
|
| 224 |
|
|
Usage func()
|
| 225 |
|
|
|
| 226 |
|
|
name string
|
| 227 |
|
|
parsed bool
|
| 228 |
|
|
actual map[string]*Flag
|
| 229 |
|
|
formal map[string]*Flag
|
| 230 |
|
|
args []string // arguments after flags
|
| 231 |
|
|
exitOnError bool // does the program exit if there's an error?
|
| 232 |
|
|
errorHandling ErrorHandling
|
| 233 |
|
|
output io.Writer // nil means stderr; use out() accessor
|
| 234 |
|
|
}
|
| 235 |
|
|
|
| 236 |
|
|
// A Flag represents the state of a flag.
|
| 237 |
|
|
type Flag struct {
|
| 238 |
|
|
Name string // name as it appears on command line
|
| 239 |
|
|
Usage string // help message
|
| 240 |
|
|
Value Value // value as set
|
| 241 |
|
|
DefValue string // default value (as text); for usage message
|
| 242 |
|
|
}
|
| 243 |
|
|
|
| 244 |
|
|
// sortFlags returns the flags as a slice in lexicographical sorted order.
|
| 245 |
|
|
func sortFlags(flags map[string]*Flag) []*Flag {
|
| 246 |
|
|
list := make(sort.StringSlice, len(flags))
|
| 247 |
|
|
i := 0
|
| 248 |
|
|
for _, f := range flags {
|
| 249 |
|
|
list[i] = f.Name
|
| 250 |
|
|
i++
|
| 251 |
|
|
}
|
| 252 |
|
|
list.Sort()
|
| 253 |
|
|
result := make([]*Flag, len(list))
|
| 254 |
|
|
for i, name := range list {
|
| 255 |
|
|
result[i] = flags[name]
|
| 256 |
|
|
}
|
| 257 |
|
|
return result
|
| 258 |
|
|
}
|
| 259 |
|
|
|
| 260 |
|
|
func (f *FlagSet) out() io.Writer {
|
| 261 |
|
|
if f.output == nil {
|
| 262 |
|
|
return os.Stderr
|
| 263 |
|
|
}
|
| 264 |
|
|
return f.output
|
| 265 |
|
|
}
|
| 266 |
|
|
|
| 267 |
|
|
// SetOutput sets the destination for usage and error messages.
|
| 268 |
|
|
// If output is nil, os.Stderr is used.
|
| 269 |
|
|
func (f *FlagSet) SetOutput(output io.Writer) {
|
| 270 |
|
|
f.output = output
|
| 271 |
|
|
}
|
| 272 |
|
|
|
| 273 |
|
|
// VisitAll visits the flags in lexicographical order, calling fn for each.
|
| 274 |
|
|
// It visits all flags, even those not set.
|
| 275 |
|
|
func (f *FlagSet) VisitAll(fn func(*Flag)) {
|
| 276 |
|
|
for _, flag := range sortFlags(f.formal) {
|
| 277 |
|
|
fn(flag)
|
| 278 |
|
|
}
|
| 279 |
|
|
}
|
| 280 |
|
|
|
| 281 |
|
|
// VisitAll visits the command-line flags in lexicographical order, calling
|
| 282 |
|
|
// fn for each. It visits all flags, even those not set.
|
| 283 |
|
|
func VisitAll(fn func(*Flag)) {
|
| 284 |
|
|
commandLine.VisitAll(fn)
|
| 285 |
|
|
}
|
| 286 |
|
|
|
| 287 |
|
|
// Visit visits the flags in lexicographical order, calling fn for each.
|
| 288 |
|
|
// It visits only those flags that have been set.
|
| 289 |
|
|
func (f *FlagSet) Visit(fn func(*Flag)) {
|
| 290 |
|
|
for _, flag := range sortFlags(f.actual) {
|
| 291 |
|
|
fn(flag)
|
| 292 |
|
|
}
|
| 293 |
|
|
}
|
| 294 |
|
|
|
| 295 |
|
|
// Visit visits the command-line flags in lexicographical order, calling fn
|
| 296 |
|
|
// for each. It visits only those flags that have been set.
|
| 297 |
|
|
func Visit(fn func(*Flag)) {
|
| 298 |
|
|
commandLine.Visit(fn)
|
| 299 |
|
|
}
|
| 300 |
|
|
|
| 301 |
|
|
// Lookup returns the Flag structure of the named flag, returning nil if none exists.
|
| 302 |
|
|
func (f *FlagSet) Lookup(name string) *Flag {
|
| 303 |
|
|
return f.formal[name]
|
| 304 |
|
|
}
|
| 305 |
|
|
|
| 306 |
|
|
// Lookup returns the Flag structure of the named command-line flag,
|
| 307 |
|
|
// returning nil if none exists.
|
| 308 |
|
|
func Lookup(name string) *Flag {
|
| 309 |
|
|
return commandLine.formal[name]
|
| 310 |
|
|
}
|
| 311 |
|
|
|
| 312 |
|
|
// Set sets the value of the named flag.
|
| 313 |
|
|
func (f *FlagSet) Set(name, value string) error {
|
| 314 |
|
|
flag, ok := f.formal[name]
|
| 315 |
|
|
if !ok {
|
| 316 |
|
|
return fmt.Errorf("no such flag -%v", name)
|
| 317 |
|
|
}
|
| 318 |
|
|
err := flag.Value.Set(value)
|
| 319 |
|
|
if err != nil {
|
| 320 |
|
|
return err
|
| 321 |
|
|
}
|
| 322 |
|
|
if f.actual == nil {
|
| 323 |
|
|
f.actual = make(map[string]*Flag)
|
| 324 |
|
|
}
|
| 325 |
|
|
f.actual[name] = flag
|
| 326 |
|
|
return nil
|
| 327 |
|
|
}
|
| 328 |
|
|
|
| 329 |
|
|
// Set sets the value of the named command-line flag.
|
| 330 |
|
|
func Set(name, value string) error {
|
| 331 |
|
|
return commandLine.Set(name, value)
|
| 332 |
|
|
}
|
| 333 |
|
|
|
| 334 |
|
|
// PrintDefaults prints, to standard error unless configured
|
| 335 |
|
|
// otherwise, the default values of all defined flags in the set.
|
| 336 |
|
|
func (f *FlagSet) PrintDefaults() {
|
| 337 |
|
|
f.VisitAll(func(flag *Flag) {
|
| 338 |
|
|
format := " -%s=%s: %s\n"
|
| 339 |
|
|
if _, ok := flag.Value.(*stringValue); ok {
|
| 340 |
|
|
// put quotes on the value
|
| 341 |
|
|
format = " -%s=%q: %s\n"
|
| 342 |
|
|
}
|
| 343 |
|
|
fmt.Fprintf(f.out(), format, flag.Name, flag.DefValue, flag.Usage)
|
| 344 |
|
|
})
|
| 345 |
|
|
}
|
| 346 |
|
|
|
| 347 |
|
|
// PrintDefaults prints to standard error the default values of all defined command-line flags.
|
| 348 |
|
|
func PrintDefaults() {
|
| 349 |
|
|
commandLine.PrintDefaults()
|
| 350 |
|
|
}
|
| 351 |
|
|
|
| 352 |
|
|
// defaultUsage is the default function to print a usage message.
|
| 353 |
|
|
func defaultUsage(f *FlagSet) {
|
| 354 |
|
|
fmt.Fprintf(f.out(), "Usage of %s:\n", f.name)
|
| 355 |
|
|
f.PrintDefaults()
|
| 356 |
|
|
}
|
| 357 |
|
|
|
| 358 |
|
|
// NOTE: Usage is not just defaultUsage(commandLine)
|
| 359 |
|
|
// because it serves (via godoc flag Usage) as the example
|
| 360 |
|
|
// for how to write your own usage function.
|
| 361 |
|
|
|
| 362 |
|
|
// Usage prints to standard error a usage message documenting all defined command-line flags.
|
| 363 |
|
|
// The function is a variable that may be changed to point to a custom function.
|
| 364 |
|
|
var Usage = func() {
|
| 365 |
|
|
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
|
| 366 |
|
|
PrintDefaults()
|
| 367 |
|
|
}
|
| 368 |
|
|
|
| 369 |
|
|
// NFlag returns the number of flags that have been set.
|
| 370 |
|
|
func (f *FlagSet) NFlag() int { return len(f.actual) }
|
| 371 |
|
|
|
| 372 |
|
|
// NFlag returns the number of command-line flags that have been set.
|
| 373 |
|
|
func NFlag() int { return len(commandLine.actual) }
|
| 374 |
|
|
|
| 375 |
|
|
// Arg returns the i'th argument. Arg(0) is the first remaining argument
|
| 376 |
|
|
// after flags have been processed.
|
| 377 |
|
|
func (f *FlagSet) Arg(i int) string {
|
| 378 |
|
|
if i < 0 || i >= len(f.args) {
|
| 379 |
|
|
return ""
|
| 380 |
|
|
}
|
| 381 |
|
|
return f.args[i]
|
| 382 |
|
|
}
|
| 383 |
|
|
|
| 384 |
|
|
// Arg returns the i'th command-line argument. Arg(0) is the first remaining argument
|
| 385 |
|
|
// after flags have been processed.
|
| 386 |
|
|
func Arg(i int) string {
|
| 387 |
|
|
return commandLine.Arg(i)
|
| 388 |
|
|
}
|
| 389 |
|
|
|
| 390 |
|
|
// NArg is the number of arguments remaining after flags have been processed.
|
| 391 |
|
|
func (f *FlagSet) NArg() int { return len(f.args) }
|
| 392 |
|
|
|
| 393 |
|
|
// NArg is the number of arguments remaining after flags have been processed.
|
| 394 |
|
|
func NArg() int { return len(commandLine.args) }
|
| 395 |
|
|
|
| 396 |
|
|
// Args returns the non-flag arguments.
|
| 397 |
|
|
func (f *FlagSet) Args() []string { return f.args }
|
| 398 |
|
|
|
| 399 |
|
|
// Args returns the non-flag command-line arguments.
|
| 400 |
|
|
func Args() []string { return commandLine.args }
|
| 401 |
|
|
|
| 402 |
|
|
// BoolVar defines a bool flag with specified name, default value, and usage string.
|
| 403 |
|
|
// The argument p points to a bool variable in which to store the value of the flag.
|
| 404 |
|
|
func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string) {
|
| 405 |
|
|
f.Var(newBoolValue(value, p), name, usage)
|
| 406 |
|
|
}
|
| 407 |
|
|
|
| 408 |
|
|
// BoolVar defines a bool flag with specified name, default value, and usage string.
|
| 409 |
|
|
// The argument p points to a bool variable in which to store the value of the flag.
|
| 410 |
|
|
func BoolVar(p *bool, name string, value bool, usage string) {
|
| 411 |
|
|
commandLine.Var(newBoolValue(value, p), name, usage)
|
| 412 |
|
|
}
|
| 413 |
|
|
|
| 414 |
|
|
// Bool defines a bool flag with specified name, default value, and usage string.
|
| 415 |
|
|
// The return value is the address of a bool variable that stores the value of the flag.
|
| 416 |
|
|
func (f *FlagSet) Bool(name string, value bool, usage string) *bool {
|
| 417 |
|
|
p := new(bool)
|
| 418 |
|
|
f.BoolVar(p, name, value, usage)
|
| 419 |
|
|
return p
|
| 420 |
|
|
}
|
| 421 |
|
|
|
| 422 |
|
|
// Bool defines a bool flag with specified name, default value, and usage string.
|
| 423 |
|
|
// The return value is the address of a bool variable that stores the value of the flag.
|
| 424 |
|
|
func Bool(name string, value bool, usage string) *bool {
|
| 425 |
|
|
return commandLine.Bool(name, value, usage)
|
| 426 |
|
|
}
|
| 427 |
|
|
|
| 428 |
|
|
// IntVar defines an int flag with specified name, default value, and usage string.
|
| 429 |
|
|
// The argument p points to an int variable in which to store the value of the flag.
|
| 430 |
|
|
func (f *FlagSet) IntVar(p *int, name string, value int, usage string) {
|
| 431 |
|
|
f.Var(newIntValue(value, p), name, usage)
|
| 432 |
|
|
}
|
| 433 |
|
|
|
| 434 |
|
|
// IntVar defines an int flag with specified name, default value, and usage string.
|
| 435 |
|
|
// The argument p points to an int variable in which to store the value of the flag.
|
| 436 |
|
|
func IntVar(p *int, name string, value int, usage string) {
|
| 437 |
|
|
commandLine.Var(newIntValue(value, p), name, usage)
|
| 438 |
|
|
}
|
| 439 |
|
|
|
| 440 |
|
|
// Int defines an int flag with specified name, default value, and usage string.
|
| 441 |
|
|
// The return value is the address of an int variable that stores the value of the flag.
|
| 442 |
|
|
func (f *FlagSet) Int(name string, value int, usage string) *int {
|
| 443 |
|
|
p := new(int)
|
| 444 |
|
|
f.IntVar(p, name, value, usage)
|
| 445 |
|
|
return p
|
| 446 |
|
|
}
|
| 447 |
|
|
|
| 448 |
|
|
// Int defines an int flag with specified name, default value, and usage string.
|
| 449 |
|
|
// The return value is the address of an int variable that stores the value of the flag.
|
| 450 |
|
|
func Int(name string, value int, usage string) *int {
|
| 451 |
|
|
return commandLine.Int(name, value, usage)
|
| 452 |
|
|
}
|
| 453 |
|
|
|
| 454 |
|
|
// Int64Var defines an int64 flag with specified name, default value, and usage string.
|
| 455 |
|
|
// The argument p points to an int64 variable in which to store the value of the flag.
|
| 456 |
|
|
func (f *FlagSet) Int64Var(p *int64, name string, value int64, usage string) {
|
| 457 |
|
|
f.Var(newInt64Value(value, p), name, usage)
|
| 458 |
|
|
}
|
| 459 |
|
|
|
| 460 |
|
|
// Int64Var defines an int64 flag with specified name, default value, and usage string.
|
| 461 |
|
|
// The argument p points to an int64 variable in which to store the value of the flag.
|
| 462 |
|
|
func Int64Var(p *int64, name string, value int64, usage string) {
|
| 463 |
|
|
commandLine.Var(newInt64Value(value, p), name, usage)
|
| 464 |
|
|
}
|
| 465 |
|
|
|
| 466 |
|
|
// Int64 defines an int64 flag with specified name, default value, and usage string.
|
| 467 |
|
|
// The return value is the address of an int64 variable that stores the value of the flag.
|
| 468 |
|
|
func (f *FlagSet) Int64(name string, value int64, usage string) *int64 {
|
| 469 |
|
|
p := new(int64)
|
| 470 |
|
|
f.Int64Var(p, name, value, usage)
|
| 471 |
|
|
return p
|
| 472 |
|
|
}
|
| 473 |
|
|
|
| 474 |
|
|
// Int64 defines an int64 flag with specified name, default value, and usage string.
|
| 475 |
|
|
// The return value is the address of an int64 variable that stores the value of the flag.
|
| 476 |
|
|
func Int64(name string, value int64, usage string) *int64 {
|
| 477 |
|
|
return commandLine.Int64(name, value, usage)
|
| 478 |
|
|
}
|
| 479 |
|
|
|
| 480 |
|
|
// UintVar defines a uint flag with specified name, default value, and usage string.
|
| 481 |
|
|
// The argument p points to a uint variable in which to store the value of the flag.
|
| 482 |
|
|
func (f *FlagSet) UintVar(p *uint, name string, value uint, usage string) {
|
| 483 |
|
|
f.Var(newUintValue(value, p), name, usage)
|
| 484 |
|
|
}
|
| 485 |
|
|
|
| 486 |
|
|
// UintVar defines a uint flag with specified name, default value, and usage string.
|
| 487 |
|
|
// The argument p points to a uint variable in which to store the value of the flag.
|
| 488 |
|
|
func UintVar(p *uint, name string, value uint, usage string) {
|
| 489 |
|
|
commandLine.Var(newUintValue(value, p), name, usage)
|
| 490 |
|
|
}
|
| 491 |
|
|
|
| 492 |
|
|
// Uint defines a uint flag with specified name, default value, and usage string.
|
| 493 |
|
|
// The return value is the address of a uint variable that stores the value of the flag.
|
| 494 |
|
|
func (f *FlagSet) Uint(name string, value uint, usage string) *uint {
|
| 495 |
|
|
p := new(uint)
|
| 496 |
|
|
f.UintVar(p, name, value, usage)
|
| 497 |
|
|
return p
|
| 498 |
|
|
}
|
| 499 |
|
|
|
| 500 |
|
|
// Uint defines a uint flag with specified name, default value, and usage string.
|
| 501 |
|
|
// The return value is the address of a uint variable that stores the value of the flag.
|
| 502 |
|
|
func Uint(name string, value uint, usage string) *uint {
|
| 503 |
|
|
return commandLine.Uint(name, value, usage)
|
| 504 |
|
|
}
|
| 505 |
|
|
|
| 506 |
|
|
// Uint64Var defines a uint64 flag with specified name, default value, and usage string.
|
| 507 |
|
|
// The argument p points to a uint64 variable in which to store the value of the flag.
|
| 508 |
|
|
func (f *FlagSet) Uint64Var(p *uint64, name string, value uint64, usage string) {
|
| 509 |
|
|
f.Var(newUint64Value(value, p), name, usage)
|
| 510 |
|
|
}
|
| 511 |
|
|
|
| 512 |
|
|
// Uint64Var defines a uint64 flag with specified name, default value, and usage string.
|
| 513 |
|
|
// The argument p points to a uint64 variable in which to store the value of the flag.
|
| 514 |
|
|
func Uint64Var(p *uint64, name string, value uint64, usage string) {
|
| 515 |
|
|
commandLine.Var(newUint64Value(value, p), name, usage)
|
| 516 |
|
|
}
|
| 517 |
|
|
|
| 518 |
|
|
// Uint64 defines a uint64 flag with specified name, default value, and usage string.
|
| 519 |
|
|
// The return value is the address of a uint64 variable that stores the value of the flag.
|
| 520 |
|
|
func (f *FlagSet) Uint64(name string, value uint64, usage string) *uint64 {
|
| 521 |
|
|
p := new(uint64)
|
| 522 |
|
|
f.Uint64Var(p, name, value, usage)
|
| 523 |
|
|
return p
|
| 524 |
|
|
}
|
| 525 |
|
|
|
| 526 |
|
|
// Uint64 defines a uint64 flag with specified name, default value, and usage string.
|
| 527 |
|
|
// The return value is the address of a uint64 variable that stores the value of the flag.
|
| 528 |
|
|
func Uint64(name string, value uint64, usage string) *uint64 {
|
| 529 |
|
|
return commandLine.Uint64(name, value, usage)
|
| 530 |
|
|
}
|
| 531 |
|
|
|
| 532 |
|
|
// StringVar defines a string flag with specified name, default value, and usage string.
|
| 533 |
|
|
// The argument p points to a string variable in which to store the value of the flag.
|
| 534 |
|
|
func (f *FlagSet) StringVar(p *string, name string, value string, usage string) {
|
| 535 |
|
|
f.Var(newStringValue(value, p), name, usage)
|
| 536 |
|
|
}
|
| 537 |
|
|
|
| 538 |
|
|
// StringVar defines a string flag with specified name, default value, and usage string.
|
| 539 |
|
|
// The argument p points to a string variable in which to store the value of the flag.
|
| 540 |
|
|
func StringVar(p *string, name string, value string, usage string) {
|
| 541 |
|
|
commandLine.Var(newStringValue(value, p), name, usage)
|
| 542 |
|
|
}
|
| 543 |
|
|
|
| 544 |
|
|
// String defines a string flag with specified name, default value, and usage string.
|
| 545 |
|
|
// The return value is the address of a string variable that stores the value of the flag.
|
| 546 |
|
|
func (f *FlagSet) String(name string, value string, usage string) *string {
|
| 547 |
|
|
p := new(string)
|
| 548 |
|
|
f.StringVar(p, name, value, usage)
|
| 549 |
|
|
return p
|
| 550 |
|
|
}
|
| 551 |
|
|
|
| 552 |
|
|
// String defines a string flag with specified name, default value, and usage string.
|
| 553 |
|
|
// The return value is the address of a string variable that stores the value of the flag.
|
| 554 |
|
|
func String(name string, value string, usage string) *string {
|
| 555 |
|
|
return commandLine.String(name, value, usage)
|
| 556 |
|
|
}
|
| 557 |
|
|
|
| 558 |
|
|
// Float64Var defines a float64 flag with specified name, default value, and usage string.
|
| 559 |
|
|
// The argument p points to a float64 variable in which to store the value of the flag.
|
| 560 |
|
|
func (f *FlagSet) Float64Var(p *float64, name string, value float64, usage string) {
|
| 561 |
|
|
f.Var(newFloat64Value(value, p), name, usage)
|
| 562 |
|
|
}
|
| 563 |
|
|
|
| 564 |
|
|
// Float64Var defines a float64 flag with specified name, default value, and usage string.
|
| 565 |
|
|
// The argument p points to a float64 variable in which to store the value of the flag.
|
| 566 |
|
|
func Float64Var(p *float64, name string, value float64, usage string) {
|
| 567 |
|
|
commandLine.Var(newFloat64Value(value, p), name, usage)
|
| 568 |
|
|
}
|
| 569 |
|
|
|
| 570 |
|
|
// Float64 defines a float64 flag with specified name, default value, and usage string.
|
| 571 |
|
|
// The return value is the address of a float64 variable that stores the value of the flag.
|
| 572 |
|
|
func (f *FlagSet) Float64(name string, value float64, usage string) *float64 {
|
| 573 |
|
|
p := new(float64)
|
| 574 |
|
|
f.Float64Var(p, name, value, usage)
|
| 575 |
|
|
return p
|
| 576 |
|
|
}
|
| 577 |
|
|
|
| 578 |
|
|
// Float64 defines a float64 flag with specified name, default value, and usage string.
|
| 579 |
|
|
// The return value is the address of a float64 variable that stores the value of the flag.
|
| 580 |
|
|
func Float64(name string, value float64, usage string) *float64 {
|
| 581 |
|
|
return commandLine.Float64(name, value, usage)
|
| 582 |
|
|
}
|
| 583 |
|
|
|
| 584 |
|
|
// DurationVar defines a time.Duration flag with specified name, default value, and usage string.
|
| 585 |
|
|
// The argument p points to a time.Duration variable in which to store the value of the flag.
|
| 586 |
|
|
func (f *FlagSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string) {
|
| 587 |
|
|
f.Var(newDurationValue(value, p), name, usage)
|
| 588 |
|
|
}
|
| 589 |
|
|
|
| 590 |
|
|
// DurationVar defines a time.Duration flag with specified name, default value, and usage string.
|
| 591 |
|
|
// The argument p points to a time.Duration variable in which to store the value of the flag.
|
| 592 |
|
|
func DurationVar(p *time.Duration, name string, value time.Duration, usage string) {
|
| 593 |
|
|
commandLine.Var(newDurationValue(value, p), name, usage)
|
| 594 |
|
|
}
|
| 595 |
|
|
|
| 596 |
|
|
// Duration defines a time.Duration flag with specified name, default value, and usage string.
|
| 597 |
|
|
// The return value is the address of a time.Duration variable that stores the value of the flag.
|
| 598 |
|
|
func (f *FlagSet) Duration(name string, value time.Duration, usage string) *time.Duration {
|
| 599 |
|
|
p := new(time.Duration)
|
| 600 |
|
|
f.DurationVar(p, name, value, usage)
|
| 601 |
|
|
return p
|
| 602 |
|
|
}
|
| 603 |
|
|
|
| 604 |
|
|
// Duration defines a time.Duration flag with specified name, default value, and usage string.
|
| 605 |
|
|
// The return value is the address of a time.Duration variable that stores the value of the flag.
|
| 606 |
|
|
func Duration(name string, value time.Duration, usage string) *time.Duration {
|
| 607 |
|
|
return commandLine.Duration(name, value, usage)
|
| 608 |
|
|
}
|
| 609 |
|
|
|
| 610 |
|
|
// Var defines a flag with the specified name and usage string. The type and
|
| 611 |
|
|
// value of the flag are represented by the first argument, of type Value, which
|
| 612 |
|
|
// typically holds a user-defined implementation of Value. For instance, the
|
| 613 |
|
|
// caller could create a flag that turns a comma-separated string into a slice
|
| 614 |
|
|
// of strings by giving the slice the methods of Value; in particular, Set would
|
| 615 |
|
|
// decompose the comma-separated string into the slice.
|
| 616 |
|
|
func (f *FlagSet) Var(value Value, name string, usage string) {
|
| 617 |
|
|
// Remember the default value as a string; it won't change.
|
| 618 |
|
|
flag := &Flag{name, usage, value, value.String()}
|
| 619 |
|
|
_, alreadythere := f.formal[name]
|
| 620 |
|
|
if alreadythere {
|
| 621 |
|
|
fmt.Fprintf(f.out(), "%s flag redefined: %s\n", f.name, name)
|
| 622 |
|
|
panic("flag redefinition") // Happens only if flags are declared with identical names
|
| 623 |
|
|
}
|
| 624 |
|
|
if f.formal == nil {
|
| 625 |
|
|
f.formal = make(map[string]*Flag)
|
| 626 |
|
|
}
|
| 627 |
|
|
f.formal[name] = flag
|
| 628 |
|
|
}
|
| 629 |
|
|
|
| 630 |
|
|
// Var defines a flag with the specified name and usage string. The type and
|
| 631 |
|
|
// value of the flag are represented by the first argument, of type Value, which
|
| 632 |
|
|
// typically holds a user-defined implementation of Value. For instance, the
|
| 633 |
|
|
// caller could create a flag that turns a comma-separated string into a slice
|
| 634 |
|
|
// of strings by giving the slice the methods of Value; in particular, Set would
|
| 635 |
|
|
// decompose the comma-separated string into the slice.
|
| 636 |
|
|
func Var(value Value, name string, usage string) {
|
| 637 |
|
|
commandLine.Var(value, name, usage)
|
| 638 |
|
|
}
|
| 639 |
|
|
|
| 640 |
|
|
// failf prints to standard error a formatted error and usage message and
|
| 641 |
|
|
// returns the error.
|
| 642 |
|
|
func (f *FlagSet) failf(format string, a ...interface{}) error {
|
| 643 |
|
|
err := fmt.Errorf(format, a...)
|
| 644 |
|
|
fmt.Fprintln(f.out(), err)
|
| 645 |
|
|
f.usage()
|
| 646 |
|
|
return err
|
| 647 |
|
|
}
|
| 648 |
|
|
|
| 649 |
|
|
// usage calls the Usage method for the flag set, or the usage function if
|
| 650 |
|
|
// the flag set is commandLine.
|
| 651 |
|
|
func (f *FlagSet) usage() {
|
| 652 |
|
|
if f == commandLine {
|
| 653 |
|
|
Usage()
|
| 654 |
|
|
} else if f.Usage == nil {
|
| 655 |
|
|
defaultUsage(f)
|
| 656 |
|
|
} else {
|
| 657 |
|
|
f.Usage()
|
| 658 |
|
|
}
|
| 659 |
|
|
}
|
| 660 |
|
|
|
| 661 |
|
|
// parseOne parses one flag. It returns whether a flag was seen.
|
| 662 |
|
|
func (f *FlagSet) parseOne() (bool, error) {
|
| 663 |
|
|
if len(f.args) == 0 {
|
| 664 |
|
|
return false, nil
|
| 665 |
|
|
}
|
| 666 |
|
|
s := f.args[0]
|
| 667 |
|
|
if len(s) == 0 || s[0] != '-' || len(s) == 1 {
|
| 668 |
|
|
return false, nil
|
| 669 |
|
|
}
|
| 670 |
|
|
num_minuses := 1
|
| 671 |
|
|
if s[1] == '-' {
|
| 672 |
|
|
num_minuses++
|
| 673 |
|
|
if len(s) == 2 { // "--" terminates the flags
|
| 674 |
|
|
f.args = f.args[1:]
|
| 675 |
|
|
return false, nil
|
| 676 |
|
|
}
|
| 677 |
|
|
}
|
| 678 |
|
|
name := s[num_minuses:]
|
| 679 |
|
|
if len(name) == 0 || name[0] == '-' || name[0] == '=' {
|
| 680 |
|
|
return false, f.failf("bad flag syntax: %s", s)
|
| 681 |
|
|
}
|
| 682 |
|
|
|
| 683 |
|
|
// it's a flag. does it have an argument?
|
| 684 |
|
|
f.args = f.args[1:]
|
| 685 |
|
|
has_value := false
|
| 686 |
|
|
value := ""
|
| 687 |
|
|
for i := 1; i < len(name); i++ { // equals cannot be first
|
| 688 |
|
|
if name[i] == '=' {
|
| 689 |
|
|
value = name[i+1:]
|
| 690 |
|
|
has_value = true
|
| 691 |
|
|
name = name[0:i]
|
| 692 |
|
|
break
|
| 693 |
|
|
}
|
| 694 |
|
|
}
|
| 695 |
|
|
m := f.formal
|
| 696 |
|
|
flag, alreadythere := m[name] // BUG
|
| 697 |
|
|
if !alreadythere {
|
| 698 |
|
|
if name == "help" || name == "h" { // special case for nice help message.
|
| 699 |
|
|
f.usage()
|
| 700 |
|
|
return false, ErrHelp
|
| 701 |
|
|
}
|
| 702 |
|
|
return false, f.failf("flag provided but not defined: -%s", name)
|
| 703 |
|
|
}
|
| 704 |
|
|
if fv, ok := flag.Value.(*boolValue); ok { // special case: doesn't need an arg
|
| 705 |
|
|
if has_value {
|
| 706 |
|
|
if err := fv.Set(value); err != nil {
|
| 707 |
|
|
f.failf("invalid boolean value %q for -%s: %v", value, name, err)
|
| 708 |
|
|
}
|
| 709 |
|
|
} else {
|
| 710 |
|
|
fv.Set("true")
|
| 711 |
|
|
}
|
| 712 |
|
|
} else {
|
| 713 |
|
|
// It must have a value, which might be the next argument.
|
| 714 |
|
|
if !has_value && len(f.args) > 0 {
|
| 715 |
|
|
// value is the next arg
|
| 716 |
|
|
has_value = true
|
| 717 |
|
|
value, f.args = f.args[0], f.args[1:]
|
| 718 |
|
|
}
|
| 719 |
|
|
if !has_value {
|
| 720 |
|
|
return false, f.failf("flag needs an argument: -%s", name)
|
| 721 |
|
|
}
|
| 722 |
|
|
if err := flag.Value.Set(value); err != nil {
|
| 723 |
|
|
return false, f.failf("invalid value %q for flag -%s: %v", value, name, err)
|
| 724 |
|
|
}
|
| 725 |
|
|
}
|
| 726 |
|
|
if f.actual == nil {
|
| 727 |
|
|
f.actual = make(map[string]*Flag)
|
| 728 |
|
|
}
|
| 729 |
|
|
f.actual[name] = flag
|
| 730 |
|
|
return true, nil
|
| 731 |
|
|
}
|
| 732 |
|
|
|
| 733 |
|
|
// Parse parses flag definitions from the argument list, which should not
|
| 734 |
|
|
// include the command name. Must be called after all flags in the FlagSet
|
| 735 |
|
|
// are defined and before flags are accessed by the program.
|
| 736 |
|
|
// The return value will be ErrHelp if -help was set but not defined.
|
| 737 |
|
|
func (f *FlagSet) Parse(arguments []string) error {
|
| 738 |
|
|
f.parsed = true
|
| 739 |
|
|
f.args = arguments
|
| 740 |
|
|
for {
|
| 741 |
|
|
seen, err := f.parseOne()
|
| 742 |
|
|
if seen {
|
| 743 |
|
|
continue
|
| 744 |
|
|
}
|
| 745 |
|
|
if err == nil {
|
| 746 |
|
|
break
|
| 747 |
|
|
}
|
| 748 |
|
|
switch f.errorHandling {
|
| 749 |
|
|
case ContinueOnError:
|
| 750 |
|
|
return err
|
| 751 |
|
|
case ExitOnError:
|
| 752 |
|
|
os.Exit(2)
|
| 753 |
|
|
case PanicOnError:
|
| 754 |
|
|
panic(err)
|
| 755 |
|
|
}
|
| 756 |
|
|
}
|
| 757 |
|
|
return nil
|
| 758 |
|
|
}
|
| 759 |
|
|
|
| 760 |
|
|
// Parsed reports whether f.Parse has been called.
|
| 761 |
|
|
func (f *FlagSet) Parsed() bool {
|
| 762 |
|
|
return f.parsed
|
| 763 |
|
|
}
|
| 764 |
|
|
|
| 765 |
|
|
// Parse parses the command-line flags from os.Args[1:]. Must be called
|
| 766 |
|
|
// after all flags are defined and before flags are accessed by the program.
|
| 767 |
|
|
func Parse() {
|
| 768 |
|
|
// Ignore errors; commandLine is set for ExitOnError.
|
| 769 |
|
|
commandLine.Parse(os.Args[1:])
|
| 770 |
|
|
}
|
| 771 |
|
|
|
| 772 |
|
|
// Parsed returns true if the command-line flags have been parsed.
|
| 773 |
|
|
func Parsed() bool {
|
| 774 |
|
|
return commandLine.Parsed()
|
| 775 |
|
|
}
|
| 776 |
|
|
|
| 777 |
|
|
// The default set of command-line flags, parsed from os.Args.
|
| 778 |
|
|
var commandLine = NewFlagSet(os.Args[0], ExitOnError)
|
| 779 |
|
|
|
| 780 |
|
|
// NewFlagSet returns a new, empty flag set with the specified name and
|
| 781 |
|
|
// error handling property.
|
| 782 |
|
|
func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet {
|
| 783 |
|
|
f := &FlagSet{
|
| 784 |
|
|
name: name,
|
| 785 |
|
|
errorHandling: errorHandling,
|
| 786 |
|
|
}
|
| 787 |
|
|
return f
|
| 788 |
|
|
}
|
| 789 |
|
|
|
| 790 |
|
|
// Init sets the name and error handling property for a flag set.
|
| 791 |
|
|
// By default, the zero FlagSet uses an empty name and the
|
| 792 |
|
|
// ContinueOnError error handling policy.
|
| 793 |
|
|
func (f *FlagSet) Init(name string, errorHandling ErrorHandling) {
|
| 794 |
|
|
f.name = name
|
| 795 |
|
|
f.errorHandling = errorHandling
|
| 796 |
|
|
}
|