title: How do I unit test command line flags in Go?
body: <p>I would like a unit test that verifies a particular command line flag is within an enumeration.</p>
  
  <p>Here is the code I would like to write tests against:</p>
  
  <pre><code>var formatType string
  
  const (
      text = "text"
      json = "json"
      hash = "hash"
  )
  
  func init() {
      const (
          defaultFormat = "text"
          formatUsage   = "desired output format"
      )
  
      flag.StringVar(&amp;formatType, "format", defaultFormat, formatUsage)
      flag.StringVar(&amp;formatType, "f", defaultFormat, formatUsage+" (shorthand)")
  
  }
  
  func main() {
      flag.Parse()
  }
  </code></pre>
  
  <p>The desired test would pass only if -format equalled one of the const values given above. This value would be available in formatType. An example correct call would be: program -format text </p>
  
  <p>What is the best way to test the desired behaviors?</p>
  
  <p><strong>Note:</strong> Perhaps I have phrased this poorly, but the displayed code it not the unit test itself, but the code I want to write unit tests against. This is a simple example from the tool I am writing and wanted to ask if there were a good way to test valid inputs to the tool.</p>