github.com/jerryclinesmith/packer@v0.3.7/common/command/build_flags.go (about)

     1  package command
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"strings"
     7  )
     8  
     9  // BuildOptionFlags sets the proper command line flags needed for
    10  // build options.
    11  func BuildOptionFlags(fs *flag.FlagSet, f *BuildOptions) {
    12  	fs.Var((*SliceValue)(&f.Except), "except", "build all builds except these")
    13  	fs.Var((*SliceValue)(&f.Only), "only", "only build the given builds by name")
    14  	fs.Var((*userVarValue)(&f.UserVars), "var", "specify a user variable")
    15  	fs.Var((*AppendSliceValue)(&f.UserVarFiles), "var-file", "file with user variables")
    16  }
    17  
    18  // userVarValue is a flag.Value that parses out user variables in
    19  // the form of 'key=value' and sets it on this map.
    20  type userVarValue map[string]string
    21  
    22  func (v *userVarValue) String() string {
    23  	return ""
    24  }
    25  
    26  func (v *userVarValue) Set(raw string) error {
    27  	idx := strings.Index(raw, "=")
    28  	if idx == -1 {
    29  		return fmt.Errorf("No '=' value in arg: %s", raw)
    30  	}
    31  
    32  	if *v == nil {
    33  		*v = make(map[string]string)
    34  	}
    35  
    36  	key, value := raw[0:idx], raw[idx+1:]
    37  	(*v)[key] = value
    38  	return nil
    39  }