Source file src/cmd/go/internal/cmdflag/flag.go

     1  // Copyright 2017 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  // Package cmdflag handles flag processing common to several go tools.
     6  package cmdflag
     7  
     8  import (
     9  	"errors"
    10  	"flag"
    11  	"fmt"
    12  	"strings"
    13  )
    14  
    15  // The flag handling part of go commands such as test is large and distracting.
    16  // We can't use the standard flag package because some of the flags from
    17  // our command line are for us, and some are for the binary we're running,
    18  // and some are for both.
    19  
    20  // ErrFlagTerminator indicates the distinguished token "--", which causes the
    21  // flag package to treat all subsequent arguments as non-flags.
    22  var ErrFlagTerminator = errors.New("flag terminator")
    23  
    24  // A FlagNotDefinedError indicates a flag-like argument that does not correspond
    25  // to any registered flag in a FlagSet.
    26  type FlagNotDefinedError struct {
    27  	RawArg   string // the original argument, like --foo or -foo=value
    28  	Name     string
    29  	HasValue bool   // is this the -foo=value or --foo=value form?
    30  	Value    string // only provided if HasValue is true
    31  }
    32  
    33  func (e FlagNotDefinedError) Error() string {
    34  	return fmt.Sprintf("flag provided but not defined: -%s", e.Name)
    35  }
    36  
    37  // A NonFlagError indicates an argument that is not a syntactically-valid flag.
    38  type NonFlagError struct {
    39  	RawArg string
    40  }
    41  
    42  func (e NonFlagError) Error() string {
    43  	return fmt.Sprintf("not a flag: %q", e.RawArg)
    44  }
    45  
    46  // ParseOne sees if args[0] is present in the given flag set and if so,
    47  // sets its value and returns the flag along with the remaining (unused) arguments.
    48  //
    49  // ParseOne always returns either a non-nil Flag or a non-nil error,
    50  // and always consumes at least one argument (even on error).
    51  //
    52  // Unlike (*flag.FlagSet).Parse, ParseOne does not log its own errors.
    53  func ParseOne(fs *flag.FlagSet, args []string) (f *flag.Flag, remainingArgs []string, err error) {
    54  	// This function is loosely derived from (*flag.FlagSet).parseOne.
    55  
    56  	raw, args := args[0], args[1:]
    57  	arg := raw
    58  	if strings.HasPrefix(arg, "--") {
    59  		if arg == "--" {
    60  			return nil, args, ErrFlagTerminator
    61  		}
    62  		arg = arg[1:] // reduce two minuses to one
    63  	}
    64  
    65  	switch arg {
    66  	case "-?", "-h", "-help":
    67  		return nil, args, flag.ErrHelp
    68  	}
    69  	if len(arg) < 2 || arg[0] != '-' || arg[1] == '-' || arg[1] == '=' {
    70  		return nil, args, NonFlagError{RawArg: raw}
    71  	}
    72  
    73  	name := arg[1:]
    74  	hasValue := false
    75  	value := ""
    76  	if i := strings.Index(name, "="); i >= 0 {
    77  		value = name[i+1:]
    78  		hasValue = true
    79  		name = name[0:i]
    80  	}
    81  
    82  	f = fs.Lookup(name)
    83  	if f == nil {
    84  		return nil, args, FlagNotDefinedError{
    85  			RawArg:   raw,
    86  			Name:     name,
    87  			HasValue: hasValue,
    88  			Value:    value,
    89  		}
    90  	}
    91  
    92  	// Use fs.Set instead of f.Value.Set below so that any subsequent call to
    93  	// fs.Visit will correctly visit the flags that have been set.
    94  
    95  	failf := func(format string, a ...any) (*flag.Flag, []string, error) {
    96  		return f, args, fmt.Errorf(format, a...)
    97  	}
    98  
    99  	if fv, ok := f.Value.(boolFlag); ok && fv.IsBoolFlag() { // special case: doesn't need an arg
   100  		if hasValue {
   101  			if err := fs.Set(name, value); err != nil {
   102  				return failf("invalid boolean value %q for -%s: %v", value, name, err)
   103  			}
   104  		} else {
   105  			if err := fs.Set(name, "true"); err != nil {
   106  				return failf("invalid boolean flag %s: %v", name, err)
   107  			}
   108  		}
   109  	} else {
   110  		// It must have a value, which might be the next argument.
   111  		if !hasValue && len(args) > 0 {
   112  			// value is the next arg
   113  			hasValue = true
   114  			value, args = args[0], args[1:]
   115  		}
   116  		if !hasValue {
   117  			return failf("flag needs an argument: -%s", name)
   118  		}
   119  		if err := fs.Set(name, value); err != nil {
   120  			return failf("invalid value %q for flag -%s: %v", value, name, err)
   121  		}
   122  	}
   123  
   124  	return f, args, nil
   125  }
   126  
   127  type boolFlag interface {
   128  	IsBoolFlag() bool
   129  }
   130  

View as plain text