title: Cobra: how to set flags programmatically in tests
body: <p>I'm using <code>cobra</code> to build a CLI and want to simulate a command being run with different sets of options/flags. I've been trying to figure out how I can use the cobra APIs to set flags within my test but haven't really gotten it yet.</p>
  
  <p>I have this:</p>
  
  <pre><code>// NewFooCmd returns a cobra.Command fitted to print output to the buffer for easier testing.
  buf := &amp;bytes.Buffer{}
  cmd := package.NewFooCmd(buf)
  
  cmd.Execute()
  
  // some validations on the content of buf
  </code></pre>
  
  <p>So far the closest thing I've found is:</p>
  
  <p><code>cmd.Flags().Set(name string, value string)</code></p>
  
  <p>...but this doesn't seem right because while the names of flags are all strings, they don't all take strings as values. Also it just doesn't seem to work even if I have an <code>int</code> flag and pass <code>string(1)</code>.</p>
  
  <p>Is there something simple I'm missing here?</p>