Signed-off-by: Félix Cantournet <felix.cantournet@cloudwatt.com>
| ... | ... |
@@ -2,83 +2,82 @@ |
| 2 | 2 |
// Use of this source code is governed by a BSD-style |
| 3 | 3 |
// license that can be found in the LICENSE file. |
| 4 | 4 |
|
| 5 |
-/* |
|
| 6 |
- Package flag implements command-line flag parsing. |
|
| 7 |
- |
|
| 8 |
- Usage: |
|
| 9 |
- |
|
| 10 |
- Define flags using flag.String(), Bool(), Int(), etc. |
|
| 5 |
+// Package mflag implements command-line flag parsing. |
|
| 6 |
+// |
|
| 7 |
+// Usage: |
|
| 8 |
+// |
|
| 9 |
+// Define flags using flag.String(), Bool(), Int(), etc. |
|
| 10 |
+// |
|
| 11 |
+// This declares an integer flag, -f or --flagname, stored in the pointer ip, with type *int. |
|
| 12 |
+// import "flag /github.com/docker/docker/pkg/mflag" |
|
| 13 |
+// var ip = flag.Int([]string{"f", "-flagname"}, 1234, "help message for flagname")
|
|
| 14 |
+// If you like, you can bind the flag to a variable using the Var() functions. |
|
| 15 |
+// var flagvar int |
|
| 16 |
+// func init() {
|
|
| 17 |
+// // -flaghidden will work, but will be hidden from the usage |
|
| 18 |
+// flag.IntVar(&flagvar, []string{"f", "#flaghidden", "-flagname"}, 1234, "help message for flagname")
|
|
| 19 |
+// } |
|
| 20 |
+// Or you can create custom flags that satisfy the Value interface (with |
|
| 21 |
+// pointer receivers) and couple them to flag parsing by |
|
| 22 |
+// flag.Var(&flagVal, []string{"name"}, "help message for flagname")
|
|
| 23 |
+// For such flags, the default value is just the initial value of the variable. |
|
| 24 |
+// |
|
| 25 |
+// You can also add "deprecated" flags, they are still usable, but are not shown |
|
| 26 |
+// in the usage and will display a warning when you try to use them. `#` before |
|
| 27 |
+// an option means this option is deprecated, if there is an following option |
|
| 28 |
+// without `#` ahead, then that's the replacement, if not, it will just be removed: |
|
| 29 |
+// var ip = flag.Int([]string{"#f", "#flagname", "-flagname"}, 1234, "help message for flagname")
|
|
| 30 |
+// this will display: `Warning: '-f' is deprecated, it will be replaced by '--flagname' soon. See usage.` or |
|
| 31 |
+// this will display: `Warning: '-flagname' is deprecated, it will be replaced by '--flagname' soon. See usage.` |
|
| 32 |
+// var ip = flag.Int([]string{"f", "#flagname"}, 1234, "help message for flagname")
|
|
| 33 |
+// will display: `Warning: '-flagname' is deprecated, it will be removed soon. See usage.` |
|
| 34 |
+// so you can only use `-f`. |
|
| 35 |
+// |
|
| 36 |
+// You can also group one letter flags, bif you declare |
|
| 37 |
+// var v = flag.Bool([]string{"v", "-verbose"}, false, "help message for verbose")
|
|
| 38 |
+// var s = flag.Bool([]string{"s", "-slow"}, false, "help message for slow")
|
|
| 39 |
+// you will be able to use the -vs or -sv |
|
| 40 |
+// |
|
| 41 |
+// After all flags are defined, call |
|
| 42 |
+// flag.Parse() |
|
| 43 |
+// to parse the command line into the defined flags. |
|
| 44 |
+// |
|
| 45 |
+// Flags may then be used directly. If you're using the flags themselves, |
|
| 46 |
+// they are all pointers; if you bind to variables, they're values. |
|
| 47 |
+// fmt.Println("ip has value ", *ip)
|
|
| 48 |
+// fmt.Println("flagvar has value ", flagvar)
|
|
| 49 |
+// |
|
| 50 |
+// After parsing, the arguments after the flag are available as the |
|
| 51 |
+// slice flag.Args() or individually as flag.Arg(i). |
|
| 52 |
+// The arguments are indexed from 0 through flag.NArg()-1. |
|
| 53 |
+// |
|
| 54 |
+// Command line flag syntax: |
|
| 55 |
+// -flag |
|
| 56 |
+// -flag=x |
|
| 57 |
+// -flag="x" |
|
| 58 |
+// -flag='x' |
|
| 59 |
+// -flag x // non-boolean flags only |
|
| 60 |
+// One or two minus signs may be used; they are equivalent. |
|
| 61 |
+// The last form is not permitted for boolean flags because the |
|
| 62 |
+// meaning of the command |
|
| 63 |
+// cmd -x * |
|
| 64 |
+// will change if there is a file called 0, false, etc. You must |
|
| 65 |
+// use the -flag=false form to turn off a boolean flag. |
|
| 66 |
+// |
|
| 67 |
+// Flag parsing stops just before the first non-flag argument |
|
| 68 |
+// ("-" is a non-flag argument) or after the terminator "--".
|
|
| 69 |
+// |
|
| 70 |
+// Integer flags accept 1234, 0664, 0x1234 and may be negative. |
|
| 71 |
+// Boolean flags may be 1, 0, t, f, true, false, TRUE, FALSE, True, False. |
|
| 72 |
+// Duration flags accept any input valid for time.ParseDuration. |
|
| 73 |
+// |
|
| 74 |
+// The default set of command-line flags is controlled by |
|
| 75 |
+// top-level functions. The FlagSet type allows one to define |
|
| 76 |
+// independent sets of flags, such as to implement subcommands |
|
| 77 |
+// in a command-line interface. The methods of FlagSet are |
|
| 78 |
+// analogous to the top-level functions for the command-line |
|
| 79 |
+// flag set. |
|
| 11 | 80 |
|
| 12 |
- This declares an integer flag, -f or --flagname, stored in the pointer ip, with type *int. |
|
| 13 |
- import "flag /github.com/docker/docker/pkg/mflag" |
|
| 14 |
- var ip = flag.Int([]string{"f", "-flagname"}, 1234, "help message for flagname")
|
|
| 15 |
- If you like, you can bind the flag to a variable using the Var() functions. |
|
| 16 |
- var flagvar int |
|
| 17 |
- func init() {
|
|
| 18 |
- // -flaghidden will work, but will be hidden from the usage |
|
| 19 |
- flag.IntVar(&flagvar, []string{"f", "#flaghidden", "-flagname"}, 1234, "help message for flagname")
|
|
| 20 |
- } |
|
| 21 |
- Or you can create custom flags that satisfy the Value interface (with |
|
| 22 |
- pointer receivers) and couple them to flag parsing by |
|
| 23 |
- flag.Var(&flagVal, []string{"name"}, "help message for flagname")
|
|
| 24 |
- For such flags, the default value is just the initial value of the variable. |
|
| 25 |
- |
|
| 26 |
- You can also add "deprecated" flags, they are still usable, but are not shown |
|
| 27 |
- in the usage and will display a warning when you try to use them. `#` before |
|
| 28 |
- an option means this option is deprecated, if there is an following option |
|
| 29 |
- without `#` ahead, then that's the replacement, if not, it will just be removed: |
|
| 30 |
- var ip = flag.Int([]string{"#f", "#flagname", "-flagname"}, 1234, "help message for flagname")
|
|
| 31 |
- this will display: `Warning: '-f' is deprecated, it will be replaced by '--flagname' soon. See usage.` or |
|
| 32 |
- this will display: `Warning: '-flagname' is deprecated, it will be replaced by '--flagname' soon. See usage.` |
|
| 33 |
- var ip = flag.Int([]string{"f", "#flagname"}, 1234, "help message for flagname")
|
|
| 34 |
- will display: `Warning: '-flagname' is deprecated, it will be removed soon. See usage.` |
|
| 35 |
- so you can only use `-f`. |
|
| 36 |
- |
|
| 37 |
- You can also group one letter flags, bif you declare |
|
| 38 |
- var v = flag.Bool([]string{"v", "-verbose"}, false, "help message for verbose")
|
|
| 39 |
- var s = flag.Bool([]string{"s", "-slow"}, false, "help message for slow")
|
|
| 40 |
- you will be able to use the -vs or -sv |
|
| 41 |
- |
|
| 42 |
- After all flags are defined, call |
|
| 43 |
- flag.Parse() |
|
| 44 |
- to parse the command line into the defined flags. |
|
| 45 |
- |
|
| 46 |
- Flags may then be used directly. If you're using the flags themselves, |
|
| 47 |
- they are all pointers; if you bind to variables, they're values. |
|
| 48 |
- fmt.Println("ip has value ", *ip)
|
|
| 49 |
- fmt.Println("flagvar has value ", flagvar)
|
|
| 50 |
- |
|
| 51 |
- After parsing, the arguments after the flag are available as the |
|
| 52 |
- slice flag.Args() or individually as flag.Arg(i). |
|
| 53 |
- The arguments are indexed from 0 through flag.NArg()-1. |
|
| 54 |
- |
|
| 55 |
- Command line flag syntax: |
|
| 56 |
- -flag |
|
| 57 |
- -flag=x |
|
| 58 |
- -flag="x" |
|
| 59 |
- -flag='x' |
|
| 60 |
- -flag x // non-boolean flags only |
|
| 61 |
- One or two minus signs may be used; they are equivalent. |
|
| 62 |
- The last form is not permitted for boolean flags because the |
|
| 63 |
- meaning of the command |
|
| 64 |
- cmd -x * |
|
| 65 |
- will change if there is a file called 0, false, etc. You must |
|
| 66 |
- use the -flag=false form to turn off a boolean flag. |
|
| 67 |
- |
|
| 68 |
- Flag parsing stops just before the first non-flag argument |
|
| 69 |
- ("-" is a non-flag argument) or after the terminator "--".
|
|
| 70 |
- |
|
| 71 |
- Integer flags accept 1234, 0664, 0x1234 and may be negative. |
|
| 72 |
- Boolean flags may be 1, 0, t, f, true, false, TRUE, FALSE, True, False. |
|
| 73 |
- Duration flags accept any input valid for time.ParseDuration. |
|
| 74 |
- |
|
| 75 |
- The default set of command-line flags is controlled by |
|
| 76 |
- top-level functions. The FlagSet type allows one to define |
|
| 77 |
- independent sets of flags, such as to implement subcommands |
|
| 78 |
- in a command-line interface. The methods of FlagSet are |
|
| 79 |
- analogous to the top-level functions for the command-line |
|
| 80 |
- flag set. |
|
| 81 |
-*/ |
|
| 82 | 81 |
package mflag |
| 83 | 82 |
|
| 84 | 83 |
import ( |
| ... | ... |
@@ -277,6 +276,7 @@ type Getter interface {
|
| 277 | 277 |
// ErrorHandling defines how to handle flag parsing errors. |
| 278 | 278 |
type ErrorHandling int |
| 279 | 279 |
|
| 280 |
+// ErrorHandling strategies available when a flag parsing error occurs |
|
| 280 | 281 |
const ( |
| 281 | 282 |
ContinueOnError ErrorHandling = iota |
| 282 | 283 |
ExitOnError |
| ... | ... |
@@ -358,28 +358,28 @@ func sortFlags(flags map[string]*Flag) []*Flag {
|
| 358 | 358 |
} |
| 359 | 359 |
|
| 360 | 360 |
// Name returns the name of the FlagSet. |
| 361 |
-func (f *FlagSet) Name() string {
|
|
| 362 |
- return f.name |
|
| 361 |
+func (fs *FlagSet) Name() string {
|
|
| 362 |
+ return fs.name |
|
| 363 | 363 |
} |
| 364 | 364 |
|
| 365 | 365 |
// Out returns the destination for usage and error messages. |
| 366 |
-func (f *FlagSet) Out() io.Writer {
|
|
| 367 |
- if f.output == nil {
|
|
| 366 |
+func (fs *FlagSet) Out() io.Writer {
|
|
| 367 |
+ if fs.output == nil {
|
|
| 368 | 368 |
return os.Stderr |
| 369 | 369 |
} |
| 370 |
- return f.output |
|
| 370 |
+ return fs.output |
|
| 371 | 371 |
} |
| 372 | 372 |
|
| 373 | 373 |
// SetOutput sets the destination for usage and error messages. |
| 374 | 374 |
// If output is nil, os.Stderr is used. |
| 375 |
-func (f *FlagSet) SetOutput(output io.Writer) {
|
|
| 376 |
- f.output = output |
|
| 375 |
+func (fs *FlagSet) SetOutput(output io.Writer) {
|
|
| 376 |
+ fs.output = output |
|
| 377 | 377 |
} |
| 378 | 378 |
|
| 379 | 379 |
// VisitAll visits the flags in lexicographical order, calling fn for each. |
| 380 | 380 |
// It visits all flags, even those not set. |
| 381 |
-func (f *FlagSet) VisitAll(fn func(*Flag)) {
|
|
| 382 |
- for _, flag := range sortFlags(f.formal) {
|
|
| 381 |
+func (fs *FlagSet) VisitAll(fn func(*Flag)) {
|
|
| 382 |
+ for _, flag := range sortFlags(fs.formal) {
|
|
| 383 | 383 |
fn(flag) |
| 384 | 384 |
} |
| 385 | 385 |
} |
| ... | ... |
@@ -392,8 +392,8 @@ func VisitAll(fn func(*Flag)) {
|
| 392 | 392 |
|
| 393 | 393 |
// Visit visits the flags in lexicographical order, calling fn for each. |
| 394 | 394 |
// It visits only those flags that have been set. |
| 395 |
-func (f *FlagSet) Visit(fn func(*Flag)) {
|
|
| 396 |
- for _, flag := range sortFlags(f.actual) {
|
|
| 395 |
+func (fs *FlagSet) Visit(fn func(*Flag)) {
|
|
| 396 |
+ for _, flag := range sortFlags(fs.actual) {
|
|
| 397 | 397 |
fn(flag) |
| 398 | 398 |
} |
| 399 | 399 |
} |
| ... | ... |
@@ -405,13 +405,13 @@ func Visit(fn func(*Flag)) {
|
| 405 | 405 |
} |
| 406 | 406 |
|
| 407 | 407 |
// Lookup returns the Flag structure of the named flag, returning nil if none exists. |
| 408 |
-func (f *FlagSet) Lookup(name string) *Flag {
|
|
| 409 |
- return f.formal[name] |
|
| 408 |
+func (fs *FlagSet) Lookup(name string) *Flag {
|
|
| 409 |
+ return fs.formal[name] |
|
| 410 | 410 |
} |
| 411 | 411 |
|
| 412 |
-// Indicates whether the specified flag was specified at all on the cmd line |
|
| 413 |
-func (f *FlagSet) IsSet(name string) bool {
|
|
| 414 |
- return f.actual[name] != nil |
|
| 412 |
+// IsSet indicates whether the specified flag is set in the given FlagSet |
|
| 413 |
+func (fs *FlagSet) IsSet(name string) bool {
|
|
| 414 |
+ return fs.actual[name] != nil |
|
| 415 | 415 |
} |
| 416 | 416 |
|
| 417 | 417 |
// Lookup returns the Flag structure of the named command-line flag, |
| ... | ... |
@@ -420,7 +420,7 @@ func Lookup(name string) *Flag {
|
| 420 | 420 |
return CommandLine.formal[name] |
| 421 | 421 |
} |
| 422 | 422 |
|
| 423 |
-// Indicates whether the specified flag was specified at all on the cmd line |
|
| 423 |
+// IsSet indicates whether the specified flag was specified at all on the cmd line. |
|
| 424 | 424 |
func IsSet(name string) bool {
|
| 425 | 425 |
return CommandLine.IsSet(name) |
| 426 | 426 |
} |
| ... | ... |
@@ -443,15 +443,15 @@ type nArgRequirement struct {
|
| 443 | 443 |
// The first parameter can be Exact, Max, or Min to respectively specify the exact, |
| 444 | 444 |
// the maximum, or the minimal number of arguments required. |
| 445 | 445 |
// The actual check is done in FlagSet.CheckArgs(). |
| 446 |
-func (f *FlagSet) Require(nArgRequirementType nArgRequirementType, nArg int) {
|
|
| 447 |
- f.nArgRequirements = append(f.nArgRequirements, nArgRequirement{nArgRequirementType, nArg})
|
|
| 446 |
+func (fs *FlagSet) Require(nArgRequirementType nArgRequirementType, nArg int) {
|
|
| 447 |
+ fs.nArgRequirements = append(fs.nArgRequirements, nArgRequirement{nArgRequirementType, nArg})
|
|
| 448 | 448 |
} |
| 449 | 449 |
|
| 450 | 450 |
// CheckArgs uses the requirements set by FlagSet.Require() to validate |
| 451 | 451 |
// the number of arguments. If the requirements are not met, |
| 452 | 452 |
// an error message string is returned. |
| 453 |
-func (f *FlagSet) CheckArgs() (message string) {
|
|
| 454 |
- for _, req := range f.nArgRequirements {
|
|
| 453 |
+func (fs *FlagSet) CheckArgs() (message string) {
|
|
| 454 |
+ for _, req := range fs.nArgRequirements {
|
|
| 455 | 455 |
var arguments string |
| 456 | 456 |
if req.N == 1 {
|
| 457 | 457 |
arguments = "1 argument" |
| ... | ... |
@@ -460,20 +460,20 @@ func (f *FlagSet) CheckArgs() (message string) {
|
| 460 | 460 |
} |
| 461 | 461 |
|
| 462 | 462 |
str := func(kind string) string {
|
| 463 |
- return fmt.Sprintf("%q requires %s%s", f.name, kind, arguments)
|
|
| 463 |
+ return fmt.Sprintf("%q requires %s%s", fs.name, kind, arguments)
|
|
| 464 | 464 |
} |
| 465 | 465 |
|
| 466 | 466 |
switch req.Type {
|
| 467 | 467 |
case Exact: |
| 468 |
- if f.NArg() != req.N {
|
|
| 468 |
+ if fs.NArg() != req.N {
|
|
| 469 | 469 |
return str("")
|
| 470 | 470 |
} |
| 471 | 471 |
case Max: |
| 472 |
- if f.NArg() > req.N {
|
|
| 472 |
+ if fs.NArg() > req.N {
|
|
| 473 | 473 |
return str("a maximum of ")
|
| 474 | 474 |
} |
| 475 | 475 |
case Min: |
| 476 |
- if f.NArg() < req.N {
|
|
| 476 |
+ if fs.NArg() < req.N {
|
|
| 477 | 477 |
return str("a minimum of ")
|
| 478 | 478 |
} |
| 479 | 479 |
} |
| ... | ... |
@@ -482,18 +482,18 @@ func (f *FlagSet) CheckArgs() (message string) {
|
| 482 | 482 |
} |
| 483 | 483 |
|
| 484 | 484 |
// Set sets the value of the named flag. |
| 485 |
-func (f *FlagSet) Set(name, value string) error {
|
|
| 486 |
- flag, ok := f.formal[name] |
|
| 485 |
+func (fs *FlagSet) Set(name, value string) error {
|
|
| 486 |
+ flag, ok := fs.formal[name] |
|
| 487 | 487 |
if !ok {
|
| 488 | 488 |
return fmt.Errorf("no such flag -%v", name)
|
| 489 | 489 |
} |
| 490 | 490 |
if err := flag.Value.Set(value); err != nil {
|
| 491 | 491 |
return err |
| 492 | 492 |
} |
| 493 |
- if f.actual == nil {
|
|
| 494 |
- f.actual = make(map[string]*Flag) |
|
| 493 |
+ if fs.actual == nil {
|
|
| 494 |
+ fs.actual = make(map[string]*Flag) |
|
| 495 | 495 |
} |
| 496 |
- f.actual[name] = flag |
|
| 496 |
+ fs.actual[name] = flag |
|
| 497 | 497 |
return nil |
| 498 | 498 |
} |
| 499 | 499 |
|
| ... | ... |
@@ -504,8 +504,8 @@ func Set(name, value string) error {
|
| 504 | 504 |
|
| 505 | 505 |
// PrintDefaults prints, to standard error unless configured |
| 506 | 506 |
// otherwise, the default values of all defined flags in the set. |
| 507 |
-func (f *FlagSet) PrintDefaults() {
|
|
| 508 |
- writer := tabwriter.NewWriter(f.Out(), 20, 1, 3, ' ', 0) |
|
| 507 |
+func (fs *FlagSet) PrintDefaults() {
|
|
| 508 |
+ writer := tabwriter.NewWriter(fs.Out(), 20, 1, 3, ' ', 0) |
|
| 509 | 509 |
home := homedir.Get() |
| 510 | 510 |
|
| 511 | 511 |
// Don't substitute when HOME is / |
| ... | ... |
@@ -514,11 +514,11 @@ func (f *FlagSet) PrintDefaults() {
|
| 514 | 514 |
} |
| 515 | 515 |
|
| 516 | 516 |
// Add a blank line between cmd description and list of options |
| 517 |
- if f.FlagCount() > 0 {
|
|
| 517 |
+ if fs.FlagCount() > 0 {
|
|
| 518 | 518 |
fmt.Fprintln(writer, "") |
| 519 | 519 |
} |
| 520 | 520 |
|
| 521 |
- f.VisitAll(func(flag *Flag) {
|
|
| 521 |
+ fs.VisitAll(func(flag *Flag) {
|
|
| 522 | 522 |
format := " -%s=%s" |
| 523 | 523 |
names := []string{}
|
| 524 | 524 |
for _, name := range flag.Names {
|
| ... | ... |
@@ -551,13 +551,13 @@ func PrintDefaults() {
|
| 551 | 551 |
} |
| 552 | 552 |
|
| 553 | 553 |
// defaultUsage is the default function to print a usage message. |
| 554 |
-func defaultUsage(f *FlagSet) {
|
|
| 555 |
- if f.name == "" {
|
|
| 556 |
- fmt.Fprintf(f.Out(), "Usage:\n") |
|
| 554 |
+func defaultUsage(fs *FlagSet) {
|
|
| 555 |
+ if fs.name == "" {
|
|
| 556 |
+ fmt.Fprintf(fs.Out(), "Usage:\n") |
|
| 557 | 557 |
} else {
|
| 558 |
- fmt.Fprintf(f.Out(), "Usage of %s:\n", f.name) |
|
| 558 |
+ fmt.Fprintf(fs.Out(), "Usage of %s:\n", fs.name) |
|
| 559 | 559 |
} |
| 560 |
- f.PrintDefaults() |
|
| 560 |
+ fs.PrintDefaults() |
|
| 561 | 561 |
} |
| 562 | 562 |
|
| 563 | 563 |
// NOTE: Usage is not just defaultUsage(CommandLine) |
| ... | ... |
@@ -578,12 +578,12 @@ var ShortUsage = func() {
|
| 578 | 578 |
} |
| 579 | 579 |
|
| 580 | 580 |
// FlagCount returns the number of flags that have been defined. |
| 581 |
-func (f *FlagSet) FlagCount() int { return len(sortFlags(f.formal)) }
|
|
| 581 |
+func (fs *FlagSet) FlagCount() int { return len(sortFlags(fs.formal)) }
|
|
| 582 | 582 |
|
| 583 | 583 |
// FlagCountUndeprecated returns the number of undeprecated flags that have been defined. |
| 584 |
-func (f *FlagSet) FlagCountUndeprecated() int {
|
|
| 584 |
+func (fs *FlagSet) FlagCountUndeprecated() int {
|
|
| 585 | 585 |
count := 0 |
| 586 |
- for _, flag := range sortFlags(f.formal) {
|
|
| 586 |
+ for _, flag := range sortFlags(fs.formal) {
|
|
| 587 | 587 |
for _, name := range flag.Names {
|
| 588 | 588 |
if name[0] != '#' {
|
| 589 | 589 |
count++ |
| ... | ... |
@@ -595,18 +595,18 @@ func (f *FlagSet) FlagCountUndeprecated() int {
|
| 595 | 595 |
} |
| 596 | 596 |
|
| 597 | 597 |
// NFlag returns the number of flags that have been set. |
| 598 |
-func (f *FlagSet) NFlag() int { return len(f.actual) }
|
|
| 598 |
+func (fs *FlagSet) NFlag() int { return len(fs.actual) }
|
|
| 599 | 599 |
|
| 600 | 600 |
// NFlag returns the number of command-line flags that have been set. |
| 601 | 601 |
func NFlag() int { return len(CommandLine.actual) }
|
| 602 | 602 |
|
| 603 | 603 |
// Arg returns the i'th argument. Arg(0) is the first remaining argument |
| 604 | 604 |
// after flags have been processed. |
| 605 |
-func (f *FlagSet) Arg(i int) string {
|
|
| 606 |
- if i < 0 || i >= len(f.args) {
|
|
| 605 |
+func (fs *FlagSet) Arg(i int) string {
|
|
| 606 |
+ if i < 0 || i >= len(fs.args) {
|
|
| 607 | 607 |
return "" |
| 608 | 608 |
} |
| 609 |
- return f.args[i] |
|
| 609 |
+ return fs.args[i] |
|
| 610 | 610 |
} |
| 611 | 611 |
|
| 612 | 612 |
// Arg returns the i'th command-line argument. Arg(0) is the first remaining argument |
| ... | ... |
@@ -616,21 +616,21 @@ func Arg(i int) string {
|
| 616 | 616 |
} |
| 617 | 617 |
|
| 618 | 618 |
// NArg is the number of arguments remaining after flags have been processed. |
| 619 |
-func (f *FlagSet) NArg() int { return len(f.args) }
|
|
| 619 |
+func (fs *FlagSet) NArg() int { return len(fs.args) }
|
|
| 620 | 620 |
|
| 621 | 621 |
// NArg is the number of arguments remaining after flags have been processed. |
| 622 | 622 |
func NArg() int { return len(CommandLine.args) }
|
| 623 | 623 |
|
| 624 | 624 |
// Args returns the non-flag arguments. |
| 625 |
-func (f *FlagSet) Args() []string { return f.args }
|
|
| 625 |
+func (fs *FlagSet) Args() []string { return fs.args }
|
|
| 626 | 626 |
|
| 627 | 627 |
// Args returns the non-flag command-line arguments. |
| 628 | 628 |
func Args() []string { return CommandLine.args }
|
| 629 | 629 |
|
| 630 | 630 |
// BoolVar defines a bool flag with specified name, default value, and usage string. |
| 631 | 631 |
// The argument p points to a bool variable in which to store the value of the flag. |
| 632 |
-func (f *FlagSet) BoolVar(p *bool, names []string, value bool, usage string) {
|
|
| 633 |
- f.Var(newBoolValue(value, p), names, usage) |
|
| 632 |
+func (fs *FlagSet) BoolVar(p *bool, names []string, value bool, usage string) {
|
|
| 633 |
+ fs.Var(newBoolValue(value, p), names, usage) |
|
| 634 | 634 |
} |
| 635 | 635 |
|
| 636 | 636 |
// BoolVar defines a bool flag with specified name, default value, and usage string. |
| ... | ... |
@@ -641,9 +641,9 @@ func BoolVar(p *bool, names []string, value bool, usage string) {
|
| 641 | 641 |
|
| 642 | 642 |
// Bool defines a bool flag with specified name, default value, and usage string. |
| 643 | 643 |
// The return value is the address of a bool variable that stores the value of the flag. |
| 644 |
-func (f *FlagSet) Bool(names []string, value bool, usage string) *bool {
|
|
| 644 |
+func (fs *FlagSet) Bool(names []string, value bool, usage string) *bool {
|
|
| 645 | 645 |
p := new(bool) |
| 646 |
- f.BoolVar(p, names, value, usage) |
|
| 646 |
+ fs.BoolVar(p, names, value, usage) |
|
| 647 | 647 |
return p |
| 648 | 648 |
} |
| 649 | 649 |
|
| ... | ... |
@@ -655,8 +655,8 @@ func Bool(names []string, value bool, usage string) *bool {
|
| 655 | 655 |
|
| 656 | 656 |
// IntVar defines an int flag with specified name, default value, and usage string. |
| 657 | 657 |
// The argument p points to an int variable in which to store the value of the flag. |
| 658 |
-func (f *FlagSet) IntVar(p *int, names []string, value int, usage string) {
|
|
| 659 |
- f.Var(newIntValue(value, p), names, usage) |
|
| 658 |
+func (fs *FlagSet) IntVar(p *int, names []string, value int, usage string) {
|
|
| 659 |
+ fs.Var(newIntValue(value, p), names, usage) |
|
| 660 | 660 |
} |
| 661 | 661 |
|
| 662 | 662 |
// IntVar defines an int flag with specified name, default value, and usage string. |
| ... | ... |
@@ -667,9 +667,9 @@ func IntVar(p *int, names []string, value int, usage string) {
|
| 667 | 667 |
|
| 668 | 668 |
// Int defines an int flag with specified name, default value, and usage string. |
| 669 | 669 |
// The return value is the address of an int variable that stores the value of the flag. |
| 670 |
-func (f *FlagSet) Int(names []string, value int, usage string) *int {
|
|
| 670 |
+func (fs *FlagSet) Int(names []string, value int, usage string) *int {
|
|
| 671 | 671 |
p := new(int) |
| 672 |
- f.IntVar(p, names, value, usage) |
|
| 672 |
+ fs.IntVar(p, names, value, usage) |
|
| 673 | 673 |
return p |
| 674 | 674 |
} |
| 675 | 675 |
|
| ... | ... |
@@ -681,8 +681,8 @@ func Int(names []string, value int, usage string) *int {
|
| 681 | 681 |
|
| 682 | 682 |
// Int64Var defines an int64 flag with specified name, default value, and usage string. |
| 683 | 683 |
// The argument p points to an int64 variable in which to store the value of the flag. |
| 684 |
-func (f *FlagSet) Int64Var(p *int64, names []string, value int64, usage string) {
|
|
| 685 |
- f.Var(newInt64Value(value, p), names, usage) |
|
| 684 |
+func (fs *FlagSet) Int64Var(p *int64, names []string, value int64, usage string) {
|
|
| 685 |
+ fs.Var(newInt64Value(value, p), names, usage) |
|
| 686 | 686 |
} |
| 687 | 687 |
|
| 688 | 688 |
// Int64Var defines an int64 flag with specified name, default value, and usage string. |
| ... | ... |
@@ -693,9 +693,9 @@ func Int64Var(p *int64, names []string, value int64, usage string) {
|
| 693 | 693 |
|
| 694 | 694 |
// Int64 defines an int64 flag with specified name, default value, and usage string. |
| 695 | 695 |
// The return value is the address of an int64 variable that stores the value of the flag. |
| 696 |
-func (f *FlagSet) Int64(names []string, value int64, usage string) *int64 {
|
|
| 696 |
+func (fs *FlagSet) Int64(names []string, value int64, usage string) *int64 {
|
|
| 697 | 697 |
p := new(int64) |
| 698 |
- f.Int64Var(p, names, value, usage) |
|
| 698 |
+ fs.Int64Var(p, names, value, usage) |
|
| 699 | 699 |
return p |
| 700 | 700 |
} |
| 701 | 701 |
|
| ... | ... |
@@ -707,8 +707,8 @@ func Int64(names []string, value int64, usage string) *int64 {
|
| 707 | 707 |
|
| 708 | 708 |
// UintVar defines a uint flag with specified name, default value, and usage string. |
| 709 | 709 |
// The argument p points to a uint variable in which to store the value of the flag. |
| 710 |
-func (f *FlagSet) UintVar(p *uint, names []string, value uint, usage string) {
|
|
| 711 |
- f.Var(newUintValue(value, p), names, usage) |
|
| 710 |
+func (fs *FlagSet) UintVar(p *uint, names []string, value uint, usage string) {
|
|
| 711 |
+ fs.Var(newUintValue(value, p), names, usage) |
|
| 712 | 712 |
} |
| 713 | 713 |
|
| 714 | 714 |
// UintVar defines a uint flag with specified name, default value, and usage string. |
| ... | ... |
@@ -719,9 +719,9 @@ func UintVar(p *uint, names []string, value uint, usage string) {
|
| 719 | 719 |
|
| 720 | 720 |
// Uint defines a uint flag with specified name, default value, and usage string. |
| 721 | 721 |
// The return value is the address of a uint variable that stores the value of the flag. |
| 722 |
-func (f *FlagSet) Uint(names []string, value uint, usage string) *uint {
|
|
| 722 |
+func (fs *FlagSet) Uint(names []string, value uint, usage string) *uint {
|
|
| 723 | 723 |
p := new(uint) |
| 724 |
- f.UintVar(p, names, value, usage) |
|
| 724 |
+ fs.UintVar(p, names, value, usage) |
|
| 725 | 725 |
return p |
| 726 | 726 |
} |
| 727 | 727 |
|
| ... | ... |
@@ -733,8 +733,8 @@ func Uint(names []string, value uint, usage string) *uint {
|
| 733 | 733 |
|
| 734 | 734 |
// Uint64Var defines a uint64 flag with specified name, default value, and usage string. |
| 735 | 735 |
// The argument p points to a uint64 variable in which to store the value of the flag. |
| 736 |
-func (f *FlagSet) Uint64Var(p *uint64, names []string, value uint64, usage string) {
|
|
| 737 |
- f.Var(newUint64Value(value, p), names, usage) |
|
| 736 |
+func (fs *FlagSet) Uint64Var(p *uint64, names []string, value uint64, usage string) {
|
|
| 737 |
+ fs.Var(newUint64Value(value, p), names, usage) |
|
| 738 | 738 |
} |
| 739 | 739 |
|
| 740 | 740 |
// Uint64Var defines a uint64 flag with specified name, default value, and usage string. |
| ... | ... |
@@ -745,9 +745,9 @@ func Uint64Var(p *uint64, names []string, value uint64, usage string) {
|
| 745 | 745 |
|
| 746 | 746 |
// Uint64 defines a uint64 flag with specified name, default value, and usage string. |
| 747 | 747 |
// The return value is the address of a uint64 variable that stores the value of the flag. |
| 748 |
-func (f *FlagSet) Uint64(names []string, value uint64, usage string) *uint64 {
|
|
| 748 |
+func (fs *FlagSet) Uint64(names []string, value uint64, usage string) *uint64 {
|
|
| 749 | 749 |
p := new(uint64) |
| 750 |
- f.Uint64Var(p, names, value, usage) |
|
| 750 |
+ fs.Uint64Var(p, names, value, usage) |
|
| 751 | 751 |
return p |
| 752 | 752 |
} |
| 753 | 753 |
|
| ... | ... |
@@ -759,8 +759,8 @@ func Uint64(names []string, value uint64, usage string) *uint64 {
|
| 759 | 759 |
|
| 760 | 760 |
// StringVar defines a string flag with specified name, default value, and usage string. |
| 761 | 761 |
// The argument p points to a string variable in which to store the value of the flag. |
| 762 |
-func (f *FlagSet) StringVar(p *string, names []string, value string, usage string) {
|
|
| 763 |
- f.Var(newStringValue(value, p), names, usage) |
|
| 762 |
+func (fs *FlagSet) StringVar(p *string, names []string, value string, usage string) {
|
|
| 763 |
+ fs.Var(newStringValue(value, p), names, usage) |
|
| 764 | 764 |
} |
| 765 | 765 |
|
| 766 | 766 |
// StringVar defines a string flag with specified name, default value, and usage string. |
| ... | ... |
@@ -771,9 +771,9 @@ func StringVar(p *string, names []string, value string, usage string) {
|
| 771 | 771 |
|
| 772 | 772 |
// String defines a string flag with specified name, default value, and usage string. |
| 773 | 773 |
// The return value is the address of a string variable that stores the value of the flag. |
| 774 |
-func (f *FlagSet) String(names []string, value string, usage string) *string {
|
|
| 774 |
+func (fs *FlagSet) String(names []string, value string, usage string) *string {
|
|
| 775 | 775 |
p := new(string) |
| 776 |
- f.StringVar(p, names, value, usage) |
|
| 776 |
+ fs.StringVar(p, names, value, usage) |
|
| 777 | 777 |
return p |
| 778 | 778 |
} |
| 779 | 779 |
|
| ... | ... |
@@ -785,8 +785,8 @@ func String(names []string, value string, usage string) *string {
|
| 785 | 785 |
|
| 786 | 786 |
// Float64Var defines a float64 flag with specified name, default value, and usage string. |
| 787 | 787 |
// The argument p points to a float64 variable in which to store the value of the flag. |
| 788 |
-func (f *FlagSet) Float64Var(p *float64, names []string, value float64, usage string) {
|
|
| 789 |
- f.Var(newFloat64Value(value, p), names, usage) |
|
| 788 |
+func (fs *FlagSet) Float64Var(p *float64, names []string, value float64, usage string) {
|
|
| 789 |
+ fs.Var(newFloat64Value(value, p), names, usage) |
|
| 790 | 790 |
} |
| 791 | 791 |
|
| 792 | 792 |
// Float64Var defines a float64 flag with specified name, default value, and usage string. |
| ... | ... |
@@ -797,9 +797,9 @@ func Float64Var(p *float64, names []string, value float64, usage string) {
|
| 797 | 797 |
|
| 798 | 798 |
// Float64 defines a float64 flag with specified name, default value, and usage string. |
| 799 | 799 |
// The return value is the address of a float64 variable that stores the value of the flag. |
| 800 |
-func (f *FlagSet) Float64(names []string, value float64, usage string) *float64 {
|
|
| 800 |
+func (fs *FlagSet) Float64(names []string, value float64, usage string) *float64 {
|
|
| 801 | 801 |
p := new(float64) |
| 802 |
- f.Float64Var(p, names, value, usage) |
|
| 802 |
+ fs.Float64Var(p, names, value, usage) |
|
| 803 | 803 |
return p |
| 804 | 804 |
} |
| 805 | 805 |
|
| ... | ... |
@@ -811,8 +811,8 @@ func Float64(names []string, value float64, usage string) *float64 {
|
| 811 | 811 |
|
| 812 | 812 |
// DurationVar defines a time.Duration flag with specified name, default value, and usage string. |
| 813 | 813 |
// The argument p points to a time.Duration variable in which to store the value of the flag. |
| 814 |
-func (f *FlagSet) DurationVar(p *time.Duration, names []string, value time.Duration, usage string) {
|
|
| 815 |
- f.Var(newDurationValue(value, p), names, usage) |
|
| 814 |
+func (fs *FlagSet) DurationVar(p *time.Duration, names []string, value time.Duration, usage string) {
|
|
| 815 |
+ fs.Var(newDurationValue(value, p), names, usage) |
|
| 816 | 816 |
} |
| 817 | 817 |
|
| 818 | 818 |
// DurationVar defines a time.Duration flag with specified name, default value, and usage string. |
| ... | ... |
@@ -823,9 +823,9 @@ func DurationVar(p *time.Duration, names []string, value time.Duration, usage st |
| 823 | 823 |
|
| 824 | 824 |
// Duration defines a time.Duration flag with specified name, default value, and usage string. |
| 825 | 825 |
// The return value is the address of a time.Duration variable that stores the value of the flag. |
| 826 |
-func (f *FlagSet) Duration(names []string, value time.Duration, usage string) *time.Duration {
|
|
| 826 |
+func (fs *FlagSet) Duration(names []string, value time.Duration, usage string) *time.Duration {
|
|
| 827 | 827 |
p := new(time.Duration) |
| 828 |
- f.DurationVar(p, names, value, usage) |
|
| 828 |
+ fs.DurationVar(p, names, value, usage) |
|
| 829 | 829 |
return p |
| 830 | 830 |
} |
| 831 | 831 |
|
| ... | ... |
@@ -841,26 +841,26 @@ func Duration(names []string, value time.Duration, usage string) *time.Duration |
| 841 | 841 |
// caller could create a flag that turns a comma-separated string into a slice |
| 842 | 842 |
// of strings by giving the slice the methods of Value; in particular, Set would |
| 843 | 843 |
// decompose the comma-separated string into the slice. |
| 844 |
-func (f *FlagSet) Var(value Value, names []string, usage string) {
|
|
| 844 |
+func (fs *FlagSet) Var(value Value, names []string, usage string) {
|
|
| 845 | 845 |
// Remember the default value as a string; it won't change. |
| 846 | 846 |
flag := &Flag{names, usage, value, value.String()}
|
| 847 | 847 |
for _, name := range names {
|
| 848 | 848 |
name = strings.TrimPrefix(name, "#") |
| 849 |
- _, alreadythere := f.formal[name] |
|
| 849 |
+ _, alreadythere := fs.formal[name] |
|
| 850 | 850 |
if alreadythere {
|
| 851 | 851 |
var msg string |
| 852 |
- if f.name == "" {
|
|
| 852 |
+ if fs.name == "" {
|
|
| 853 | 853 |
msg = fmt.Sprintf("flag redefined: %s", name)
|
| 854 | 854 |
} else {
|
| 855 |
- msg = fmt.Sprintf("%s flag redefined: %s", f.name, name)
|
|
| 855 |
+ msg = fmt.Sprintf("%s flag redefined: %s", fs.name, name)
|
|
| 856 | 856 |
} |
| 857 |
- fmt.Fprintln(f.Out(), msg) |
|
| 857 |
+ fmt.Fprintln(fs.Out(), msg) |
|
| 858 | 858 |
panic(msg) // Happens only if flags are declared with identical names |
| 859 | 859 |
} |
| 860 |
- if f.formal == nil {
|
|
| 861 |
- f.formal = make(map[string]*Flag) |
|
| 860 |
+ if fs.formal == nil {
|
|
| 861 |
+ fs.formal = make(map[string]*Flag) |
|
| 862 | 862 |
} |
| 863 |
- f.formal[name] = flag |
|
| 863 |
+ fs.formal[name] = flag |
|
| 864 | 864 |
} |
| 865 | 865 |
} |
| 866 | 866 |
|
| ... | ... |
@@ -876,26 +876,26 @@ func Var(value Value, names []string, usage string) {
|
| 876 | 876 |
|
| 877 | 877 |
// failf prints to standard error a formatted error and usage message and |
| 878 | 878 |
// returns the error. |
| 879 |
-func (f *FlagSet) failf(format string, a ...interface{}) error {
|
|
| 879 |
+func (fs *FlagSet) failf(format string, a ...interface{}) error {
|
|
| 880 | 880 |
err := fmt.Errorf(format, a...) |
| 881 |
- fmt.Fprintln(f.Out(), err) |
|
| 882 |
- if os.Args[0] == f.name {
|
|
| 883 |
- fmt.Fprintf(f.Out(), "See '%s --help'.\n", os.Args[0]) |
|
| 881 |
+ fmt.Fprintln(fs.Out(), err) |
|
| 882 |
+ if os.Args[0] == fs.name {
|
|
| 883 |
+ fmt.Fprintf(fs.Out(), "See '%s --help'.\n", os.Args[0]) |
|
| 884 | 884 |
} else {
|
| 885 |
- fmt.Fprintf(f.Out(), "See '%s %s --help'.\n", os.Args[0], f.name) |
|
| 885 |
+ fmt.Fprintf(fs.Out(), "See '%s %s --help'.\n", os.Args[0], fs.name) |
|
| 886 | 886 |
} |
| 887 | 887 |
return err |
| 888 | 888 |
} |
| 889 | 889 |
|
| 890 | 890 |
// usage calls the Usage method for the flag set, or the usage function if |
| 891 | 891 |
// the flag set is CommandLine. |
| 892 |
-func (f *FlagSet) usage() {
|
|
| 893 |
- if f == CommandLine {
|
|
| 892 |
+func (fs *FlagSet) usage() {
|
|
| 893 |
+ if fs == CommandLine {
|
|
| 894 | 894 |
Usage() |
| 895 |
- } else if f.Usage == nil {
|
|
| 896 |
- defaultUsage(f) |
|
| 895 |
+ } else if fs.Usage == nil {
|
|
| 896 |
+ defaultUsage(fs) |
|
| 897 | 897 |
} else {
|
| 898 |
- f.Usage() |
|
| 898 |
+ fs.Usage() |
|
| 899 | 899 |
} |
| 900 | 900 |
} |
| 901 | 901 |
|
| ... | ... |
@@ -934,25 +934,25 @@ func trimQuotes(str string) string {
|
| 934 | 934 |
} |
| 935 | 935 |
|
| 936 | 936 |
// parseOne parses one flag. It reports whether a flag was seen. |
| 937 |
-func (f *FlagSet) parseOne() (bool, string, error) {
|
|
| 938 |
- if len(f.args) == 0 {
|
|
| 937 |
+func (fs *FlagSet) parseOne() (bool, string, error) {
|
|
| 938 |
+ if len(fs.args) == 0 {
|
|
| 939 | 939 |
return false, "", nil |
| 940 | 940 |
} |
| 941 |
- s := f.args[0] |
|
| 941 |
+ s := fs.args[0] |
|
| 942 | 942 |
if len(s) == 0 || s[0] != '-' || len(s) == 1 {
|
| 943 | 943 |
return false, "", nil |
| 944 | 944 |
} |
| 945 | 945 |
if s[1] == '-' && len(s) == 2 { // "--" terminates the flags
|
| 946 |
- f.args = f.args[1:] |
|
| 946 |
+ fs.args = fs.args[1:] |
|
| 947 | 947 |
return false, "", nil |
| 948 | 948 |
} |
| 949 | 949 |
name := s[1:] |
| 950 | 950 |
if len(name) == 0 || name[0] == '=' {
|
| 951 |
- return false, "", f.failf("bad flag syntax: %s", s)
|
|
| 951 |
+ return false, "", fs.failf("bad flag syntax: %s", s)
|
|
| 952 | 952 |
} |
| 953 | 953 |
|
| 954 | 954 |
// it's a flag. does it have an argument? |
| 955 |
- f.args = f.args[1:] |
|
| 955 |
+ fs.args = fs.args[1:] |
|
| 956 | 956 |
hasValue := false |
| 957 | 957 |
value := "" |
| 958 | 958 |
if i := strings.Index(name, "="); i != -1 {
|
| ... | ... |
@@ -961,44 +961,44 @@ func (f *FlagSet) parseOne() (bool, string, error) {
|
| 961 | 961 |
name = name[:i] |
| 962 | 962 |
} |
| 963 | 963 |
|
| 964 |
- m := f.formal |
|
| 964 |
+ m := fs.formal |
|
| 965 | 965 |
flag, alreadythere := m[name] // BUG |
| 966 | 966 |
if !alreadythere {
|
| 967 | 967 |
if name == "-help" || name == "help" || name == "h" { // special case for nice help message.
|
| 968 |
- f.usage() |
|
| 968 |
+ fs.usage() |
|
| 969 | 969 |
return false, "", ErrHelp |
| 970 | 970 |
} |
| 971 | 971 |
if len(name) > 0 && name[0] == '-' {
|
| 972 |
- return false, "", f.failf("flag provided but not defined: -%s", name)
|
|
| 972 |
+ return false, "", fs.failf("flag provided but not defined: -%s", name)
|
|
| 973 | 973 |
} |
| 974 | 974 |
return false, name, ErrRetry |
| 975 | 975 |
} |
| 976 | 976 |
if fv, ok := flag.Value.(boolFlag); ok && fv.IsBoolFlag() { // special case: doesn't need an arg
|
| 977 | 977 |
if hasValue {
|
| 978 | 978 |
if err := fv.Set(value); err != nil {
|
| 979 |
- return false, "", f.failf("invalid boolean value %q for -%s: %v", value, name, err)
|
|
| 979 |
+ return false, "", fs.failf("invalid boolean value %q for -%s: %v", value, name, err)
|
|
| 980 | 980 |
} |
| 981 | 981 |
} else {
|
| 982 | 982 |
fv.Set("true")
|
| 983 | 983 |
} |
| 984 | 984 |
} else {
|
| 985 | 985 |
// It must have a value, which might be the next argument. |
| 986 |
- if !hasValue && len(f.args) > 0 {
|
|
| 986 |
+ if !hasValue && len(fs.args) > 0 {
|
|
| 987 | 987 |
// value is the next arg |
| 988 | 988 |
hasValue = true |
| 989 |
- value, f.args = f.args[0], f.args[1:] |
|
| 989 |
+ value, fs.args = fs.args[0], fs.args[1:] |
|
| 990 | 990 |
} |
| 991 | 991 |
if !hasValue {
|
| 992 |
- return false, "", f.failf("flag needs an argument: -%s", name)
|
|
| 992 |
+ return false, "", fs.failf("flag needs an argument: -%s", name)
|
|
| 993 | 993 |
} |
| 994 | 994 |
if err := flag.Value.Set(value); err != nil {
|
| 995 |
- return false, "", f.failf("invalid value %q for flag -%s: %v", value, name, err)
|
|
| 995 |
+ return false, "", fs.failf("invalid value %q for flag -%s: %v", value, name, err)
|
|
| 996 | 996 |
} |
| 997 | 997 |
} |
| 998 |
- if f.actual == nil {
|
|
| 999 |
- f.actual = make(map[string]*Flag) |
|
| 998 |
+ if fs.actual == nil {
|
|
| 999 |
+ fs.actual = make(map[string]*Flag) |
|
| 1000 | 1000 |
} |
| 1001 |
- f.actual[name] = flag |
|
| 1001 |
+ fs.actual[name] = flag |
|
| 1002 | 1002 |
for i, n := range flag.Names {
|
| 1003 | 1003 |
if n == fmt.Sprintf("#%s", name) {
|
| 1004 | 1004 |
replacement := "" |
| ... | ... |
@@ -1009,9 +1009,9 @@ func (f *FlagSet) parseOne() (bool, string, error) {
|
| 1009 | 1009 |
} |
| 1010 | 1010 |
} |
| 1011 | 1011 |
if replacement != "" {
|
| 1012 |
- fmt.Fprintf(f.Out(), "Warning: '-%s' is deprecated, it will be replaced by '-%s' soon. See usage.\n", name, replacement) |
|
| 1012 |
+ fmt.Fprintf(fs.Out(), "Warning: '-%s' is deprecated, it will be replaced by '-%s' soon. See usage.\n", name, replacement) |
|
| 1013 | 1013 |
} else {
|
| 1014 |
- fmt.Fprintf(f.Out(), "Warning: '-%s' is deprecated, it will be removed soon. See usage.\n", name) |
|
| 1014 |
+ fmt.Fprintf(fs.Out(), "Warning: '-%s' is deprecated, it will be removed soon. See usage.\n", name) |
|
| 1015 | 1015 |
} |
| 1016 | 1016 |
} |
| 1017 | 1017 |
} |
| ... | ... |
@@ -1022,11 +1022,11 @@ func (f *FlagSet) parseOne() (bool, string, error) {
|
| 1022 | 1022 |
// include the command name. Must be called after all flags in the FlagSet |
| 1023 | 1023 |
// are defined and before flags are accessed by the program. |
| 1024 | 1024 |
// The return value will be ErrHelp if -help was set but not defined. |
| 1025 |
-func (f *FlagSet) Parse(arguments []string) error {
|
|
| 1026 |
- f.parsed = true |
|
| 1027 |
- f.args = arguments |
|
| 1025 |
+func (fs *FlagSet) Parse(arguments []string) error {
|
|
| 1026 |
+ fs.parsed = true |
|
| 1027 |
+ fs.args = arguments |
|
| 1028 | 1028 |
for {
|
| 1029 |
- seen, name, err := f.parseOne() |
|
| 1029 |
+ seen, name, err := fs.parseOne() |
|
| 1030 | 1030 |
if seen {
|
| 1031 | 1031 |
continue |
| 1032 | 1032 |
} |
| ... | ... |
@@ -1037,13 +1037,13 @@ func (f *FlagSet) Parse(arguments []string) error {
|
| 1037 | 1037 |
if len(name) > 1 {
|
| 1038 | 1038 |
err = nil |
| 1039 | 1039 |
for _, letter := range strings.Split(name, "") {
|
| 1040 |
- f.args = append([]string{"-" + letter}, f.args...)
|
|
| 1041 |
- seen2, _, err2 := f.parseOne() |
|
| 1040 |
+ fs.args = append([]string{"-" + letter}, fs.args...)
|
|
| 1041 |
+ seen2, _, err2 := fs.parseOne() |
|
| 1042 | 1042 |
if seen2 {
|
| 1043 | 1043 |
continue |
| 1044 | 1044 |
} |
| 1045 | 1045 |
if err2 != nil {
|
| 1046 |
- err = f.failf("flag provided but not defined: -%s", name)
|
|
| 1046 |
+ err = fs.failf("flag provided but not defined: -%s", name)
|
|
| 1047 | 1047 |
break |
| 1048 | 1048 |
} |
| 1049 | 1049 |
} |
| ... | ... |
@@ -1051,10 +1051,10 @@ func (f *FlagSet) Parse(arguments []string) error {
|
| 1051 | 1051 |
continue |
| 1052 | 1052 |
} |
| 1053 | 1053 |
} else {
|
| 1054 |
- err = f.failf("flag provided but not defined: -%s", name)
|
|
| 1054 |
+ err = fs.failf("flag provided but not defined: -%s", name)
|
|
| 1055 | 1055 |
} |
| 1056 | 1056 |
} |
| 1057 |
- switch f.errorHandling {
|
|
| 1057 |
+ switch fs.errorHandling {
|
|
| 1058 | 1058 |
case ContinueOnError: |
| 1059 | 1059 |
return err |
| 1060 | 1060 |
case ExitOnError: |
| ... | ... |
@@ -1067,46 +1067,48 @@ func (f *FlagSet) Parse(arguments []string) error {
|
| 1067 | 1067 |
} |
| 1068 | 1068 |
|
| 1069 | 1069 |
// ParseFlags is a utility function that adds a help flag if withHelp is true, |
| 1070 |
-// calls cmd.Parse(args) and prints a relevant error message if there are |
|
| 1070 |
+// calls fs.Parse(args) and prints a relevant error message if there are |
|
| 1071 | 1071 |
// incorrect number of arguments. It returns error only if error handling is |
| 1072 | 1072 |
// set to ContinueOnError and parsing fails. If error handling is set to |
| 1073 | 1073 |
// ExitOnError, it's safe to ignore the return value. |
| 1074 |
-func (cmd *FlagSet) ParseFlags(args []string, withHelp bool) error {
|
|
| 1074 |
+func (fs *FlagSet) ParseFlags(args []string, withHelp bool) error {
|
|
| 1075 | 1075 |
var help *bool |
| 1076 | 1076 |
if withHelp {
|
| 1077 |
- help = cmd.Bool([]string{"#help", "-help"}, false, "Print usage")
|
|
| 1077 |
+ help = fs.Bool([]string{"#help", "-help"}, false, "Print usage")
|
|
| 1078 | 1078 |
} |
| 1079 |
- if err := cmd.Parse(args); err != nil {
|
|
| 1079 |
+ if err := fs.Parse(args); err != nil {
|
|
| 1080 | 1080 |
return err |
| 1081 | 1081 |
} |
| 1082 | 1082 |
if help != nil && *help {
|
| 1083 |
- cmd.SetOutput(os.Stdout) |
|
| 1084 |
- cmd.Usage() |
|
| 1083 |
+ fs.SetOutput(os.Stdout) |
|
| 1084 |
+ fs.Usage() |
|
| 1085 | 1085 |
os.Exit(0) |
| 1086 | 1086 |
} |
| 1087 |
- if str := cmd.CheckArgs(); str != "" {
|
|
| 1088 |
- cmd.SetOutput(os.Stderr) |
|
| 1089 |
- cmd.ReportError(str, withHelp) |
|
| 1090 |
- cmd.ShortUsage() |
|
| 1087 |
+ if str := fs.CheckArgs(); str != "" {
|
|
| 1088 |
+ fs.SetOutput(os.Stderr) |
|
| 1089 |
+ fs.ReportError(str, withHelp) |
|
| 1090 |
+ fs.ShortUsage() |
|
| 1091 | 1091 |
os.Exit(1) |
| 1092 | 1092 |
} |
| 1093 | 1093 |
return nil |
| 1094 | 1094 |
} |
| 1095 | 1095 |
|
| 1096 |
-func (cmd *FlagSet) ReportError(str string, withHelp bool) {
|
|
| 1096 |
+// ReportError is a utility method that prints a user-friendly message |
|
| 1097 |
+// containing the error that occured during parsing and a suggestion to get help |
|
| 1098 |
+func (fs *FlagSet) ReportError(str string, withHelp bool) {
|
|
| 1097 | 1099 |
if withHelp {
|
| 1098 |
- if os.Args[0] == cmd.Name() {
|
|
| 1100 |
+ if os.Args[0] == fs.Name() {
|
|
| 1099 | 1101 |
str += ".\nSee '" + os.Args[0] + " --help'" |
| 1100 | 1102 |
} else {
|
| 1101 |
- str += ".\nSee '" + os.Args[0] + " " + cmd.Name() + " --help'" |
|
| 1103 |
+ str += ".\nSee '" + os.Args[0] + " " + fs.Name() + " --help'" |
|
| 1102 | 1104 |
} |
| 1103 | 1105 |
} |
| 1104 |
- fmt.Fprintf(cmd.Out(), "docker: %s.\n", str) |
|
| 1106 |
+ fmt.Fprintf(fs.Out(), "docker: %s.\n", str) |
|
| 1105 | 1107 |
} |
| 1106 | 1108 |
|
| 1107 |
-// Parsed reports whether f.Parse has been called. |
|
| 1108 |
-func (f *FlagSet) Parsed() bool {
|
|
| 1109 |
- return f.parsed |
|
| 1109 |
+// Parsed reports whether fs.Parse has been called. |
|
| 1110 |
+func (fs *FlagSet) Parsed() bool {
|
|
| 1111 |
+ return fs.parsed |
|
| 1110 | 1112 |
} |
| 1111 | 1113 |
|
| 1112 | 1114 |
// Parse parses the command-line flags from os.Args[1:]. Must be called |
| ... | ... |
@@ -1139,9 +1141,9 @@ func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet {
|
| 1139 | 1139 |
// Init sets the name and error handling property for a flag set. |
| 1140 | 1140 |
// By default, the zero FlagSet uses an empty name and the |
| 1141 | 1141 |
// ContinueOnError error handling policy. |
| 1142 |
-func (f *FlagSet) Init(name string, errorHandling ErrorHandling) {
|
|
| 1143 |
- f.name = name |
|
| 1144 |
- f.errorHandling = errorHandling |
|
| 1142 |
+func (fs *FlagSet) Init(name string, errorHandling ErrorHandling) {
|
|
| 1143 |
+ fs.name = name |
|
| 1144 |
+ fs.errorHandling = errorHandling |
|
| 1145 | 1145 |
} |
| 1146 | 1146 |
|
| 1147 | 1147 |
type mergeVal struct {
|
| ... | ... |
@@ -1161,6 +1163,9 @@ func (v mergeVal) IsBoolFlag() bool {
|
| 1161 | 1161 |
return false |
| 1162 | 1162 |
} |
| 1163 | 1163 |
|
| 1164 |
+// Merge is an helper function that merges n FlagSets into a single dest FlagSet |
|
| 1165 |
+// In case of name collision between the flagsets it will apply |
|
| 1166 |
+// the destination FlagSet's errorHandling behaviour. |
|
| 1164 | 1167 |
func Merge(dest *FlagSet, flagsets ...*FlagSet) error {
|
| 1165 | 1168 |
for _, fset := range flagsets {
|
| 1166 | 1169 |
for k, f := range fset.formal {
|
| ... | ... |
@@ -1190,6 +1195,7 @@ func Merge(dest *FlagSet, flagsets ...*FlagSet) error {
|
| 1190 | 1190 |
return nil |
| 1191 | 1191 |
} |
| 1192 | 1192 |
|
| 1193 |
-func (f *FlagSet) IsEmpty() bool {
|
|
| 1194 |
- return len(f.actual) == 0 |
|
| 1193 |
+// IsEmpty reports if the FlagSet is actually empty. |
|
| 1194 |
+func (fs *FlagSet) IsEmpty() bool {
|
|
| 1195 |
+ return len(fs.actual) == 0 |
|
| 1195 | 1196 |
} |