github.com/mweagle/Sparta@v1.15.0/docs_source/content/reference/application/custom_flags.md (about)

     1  ---
     2  date: 2016-03-09T19:56:50+01:00
     3  title: Custom Flags
     4  weight: 10
     5  ---
     6  
     7  Some commands (eg: `provision`) may require additional options.  For instance, your application's provision logic may require VPC [subnets](https://aws.amazon.com/blogs/aws/new-access-resources-in-a-vpc-from-your-lambda-functions/) or EC2 [SSH Key Names](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html).
     8  
     9  The default Sparta command line option flags may be extended and validated by building on the exposed [Cobra](https://github.com/spf13/cobra) command objects.
    10  
    11  ## Adding Flags
    12  
    13  To add a flag, use one of the [pflag](https://github.com/spf13/pflag) functions to register your custom flag with one of the standard [CommandLineOption](https://github.com/mweagle/Sparta/blob/master/sparta_main.go#L17) values.
    14  
    15  For example:
    16  
    17  ```go
    18  
    19  // SSHKeyName is the SSH KeyName to use when provisioning new EC2 instance
    20  var SSHKeyName string
    21  
    22  func main() {
    23    // And add the SSHKeyName option to the provision step
    24    sparta.CommandLineOptions.Provision.Flags().StringVarP(&SSHKeyName,
    25      "key",
    26      "k",
    27      "",
    28      "SSH Key Name to use for EC2 instances")
    29  }
    30  ```
    31  
    32  ## Validating Input
    33  
    34  Flags may be used to conditionalize which Sparta lambda functions are provided and/or their content.  In this case, your application may first need to parse and validate the command line input before calling `sparta.Main()`.
    35  
    36  To validate user input, define a [CommandLineOptionsHook](https://godoc.org/github.com/mweagle/Sparta#CommandLineOptionsHook) function and provide it to [sparta.ParseOptions](https://godoc.org/github.com/mweagle/Sparta#ParseOptions).  This function is called after the _pflag_ bindings are invoked so that your application can validate user input.
    37  
    38  The `ParseOptions` result is the optional error returned from your _CommandLineOptionsHook_ function. If there is an error, your application can then exit with an application specific exit code.  For instance:
    39  
    40  ```go
    41  // Define a validation hook s.t. we can verify the SSHKey is valid
    42  validationHook := func(command *cobra.Command) error {
    43    if command.Name() == "provision" && len(SSHKeyName) <= 0 {
    44      return fmt.Errorf("SSHKeyName option is required")
    45    }
    46    return nil
    47    }
    48  }
    49  // Extract & validate the SSH Key
    50  parseErr := sparta.ParseOptions(validationHook)
    51  if nil != parseErr {
    52    os.Exit(3)
    53  }
    54  ```
    55  
    56  Sparta itself uses the [govalidator](https://github.com/asaskevich/govalidator/) package to simplify validating command line arguments.  See [sparta_main.go](https://github.com/mweagle/Sparta/blob/master/sparta_main.go) for an example.