Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Building a CLI with Cobra | Go Programming Bootcamp lesson | BraveProgrammer
  1. Courses
  2. /
  3. Go Programming Bootcamp

Lesson 23 of 60

·Go

Building a CLI with Cobra

20 minutes

Course progress38%

Cobra CLI Framework

This lesson explains the main concept clearly with beginner-friendly examples and background so you can learn why the code works.

Cobra powers kubectl, git, and many other tools.

package main

import (
    "fmt"
    "github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{ Use: , Short:  }

 helloCmd = &cobra.Command{
    Use:   ,
    Short: ,
    Args:  cobra.ExactArgs(),
    Run:  {
        fmt.Printf(, args[])
    },
}

 { rootCmd.AddCommand(helloCmd) }

 { rootCmd.Execute() }
"app"
"Demo CLI"
var
"hello [name]"
"Print a greeting"
1
func(cmd *cobra.Command, args []string)
"Hello, %s! "
0
func init()
func main()

Run it:

$ go run main.go hello World
Hello, World!

Persistent flags

rootCmd.PersistentFlags().BoolP("verbose", "v", false, "Enable verbose output")

Info

Cobra automatically generates help (--help) and completion scripts.
Make it stickPractice what you've learned1 quiz · 3 practical tests · Optional activities from Go Programming BootcampBrowse activities
Previous lessonDependency Management with Go ModulesContinue to lesson 24Working with Databases – `database/sql` and DriversBack to Go Programming Bootcamp curriculum