title: Why is Cobra flags not working in this code
body: <p>How to get flags to work in Cobra using Local <a href="https://github.com/spf13/cobra/blob/master/user_guide.md#local-flags" rel="nofollow noreferrer">flags</a></p>
  <pre><code>package main
  
  import (
      &quot;fmt&quot;
      &quot;github.com/spf13/cobra&quot;
  )
  
  func main() {
      myCmd := &amp;cobra.Command{
          Use: &quot;myaction&quot;,
          Run: func(cmd *cobra.Command, args []string) {
              if len(args) == 1 {
                  flags := cmd.Flags()
                  var strTmp string
                  flags.StringVarP(&amp;strTmp, &quot;test&quot;, &quot;t&quot;, &quot;&quot;, &quot;Source directory to read from&quot;)
                  fmt.Println(strTmp)
              }
          },
      }
  
      myCmd.Execute()
  }
  </code></pre>
  <h3>Error</h3>
  <pre><code>go run main.go myaction --test=&quot;hello&quot;
  Error: unknown flag: --test
  Usage:
    myaction [flags]
  
  Flags:
    -h, --help   help for myaction
  </code></pre>