Go Packages for Your Next CLI App

Command Line Interfaces (CLIs) are still popular even in the face of Graphical User Interfaces (GUIs) and their interactive visual components. CLIs are still in use due to their simplicity, ease, speed, and fewer resources required to set up a CLI application.

Sign up forfree

Forgot your password?

Create an account

*Required: 8 chars, 1 capital letter, 1 number

By continuing, you agree to thePrivacy PolicyandTerms of Use.You also agree to receive our newsletters, you may opt-out any time.

4

You can build CLIs with popular languages from Python to C++, Rust, and Go. Most languages provide packages for building CLI apps without external dependencies.

Go and Command Line Applications

One of the intended use cases for the Go programming language is building performant interactive CLIs. Over the years, Go has gained traction in the field.

Go powers popular CLI applications like Docker CLI, Hugo, GitHub CLI, and more. Go is one of the preferred languages for building CLI apps, given its first-class support for them alongside its performance.

MacBook and a Dell laptop running ZorinOS next to each other

There’s aflagpackage for command-line parsing, but it lacks much functionality you’ll need to build modern command-line apps. Also, developing CLI apps with theflagpackage can be complex as the size of your app increases and you incorporate more features.

The Go ecosystem has many reputable packages for building CLIs, with features to help reduce your development time.

firefox logo with yellow warning symbol

The Cobra Package

Cobrais one of the most popular packages for building CLIs in the Go ecosystem and beyond. Cobra is a package for creating interactive, modern CLI apps in any environment.

Cobra provides a simple interface for building CLIs of industry-standard similar to Go tools and Git. Cobra powers many CLI applications you’ve interacted with, including Docker, Kubernetes, Twitch, Hugo, and a host of others.

Dell monitor showing Windows 10 desktop

Cobra is an easy-to-use, subcommand-based tool. It supports POSIX-compliant flags, nested subcommands, and global, local, and cascading flags.

Cobra also supports intelligent suggestions, automatic help generation, flag recognition, and command aliases. It allows for shell autocompletion on Bash, Powershell, Fish, and Zsh, and integration with theviperpackage for 12-factor apps.

A close up of the Mac command keyboard button

Run this command in the terminal of your working directory to install the latest version of the Cobra package and its dependencies.

Cobra provides a CLI app for faster and easier development. The Cobra—cli package generates boilerplate code that you’re able to configure to add new commands to your app without hassles.

Run this command in the terminal of your working directory to install the latest version of the Cobra-cli package.

You’ll need to set your GOPATH variable to your working directory to use the Cobra generator (Cobra-cli) and other Go command line tools.

After a successful installation, you may initialize a Cobra app in your project with theinitcommand.

After initializing a Cobra app, you can use theaddsubcommand with thecobra-clicommand to create and add a new command to your Cobra app.

The command creates a new file with the code you need for the command. it’s possible to edit the file based on your operation.

Here’s a simple command handler function for a command generated with the Cobra-cli command.

You can access additional arguments with theargsargument of the function. The function above utilizes theifstatement to check if there’s an additional argument from the terminal and prints the argument.

The CLI Package

The Cli packageis an expressivity-themed minimalist package for building fast, distributable CLI apps in Go.

The Cli package supports command line arguments, flags, and subcommands with categories. It has functionality to cater for short options,Bash completions, generated help, and versioning.

The Cli package is interoperable with Go’s built-inflagandcontextpackages, and you may use most of the features in theflagpackage with the Cli.

Run these commands in the terminal of your working directory to add the Graphql-go package and its dependencies to your project.

Here’s how you’re able to add a command to your CLI app with the Cli package.

In themainfunction, theappvariable is an instance of a Cli app and the application’sActionfield is the app’s handler function. Here, thenewcommand would print the text from the function or return an error if there are any errors from the app.

The Cli package is helpful for smaller projects without much complexity or features.

The Go-Arg Package

The Go-arg packageis a struct-based package for parsing CLI arguments in Go. With the Go-arg package, you may declare structs for CLI arguments and operate on the arguments as regular Go data structures. The Go-arg package provides required arguments, positional arguments, environment variables, usage strings, default values, and many other features.

The package supports basic types like integer, floating point, string, and boolean. It also supports more complex types like URLs, time durations, email addresses,MAC addresses, pointers, slices, maps, and theanytype.

You can use thegetcommand to add the Go-arg package to your package’s dependencies.

The Go-arg package is simple and intuitive to use. Here’s how you can accept arguments from the command line and print them to the command line console.

Theargsstruct is the struct instance for the CLI application. TheMustParsemethod parses the arguments from the command line into the struct instance.

You can easily run the program with theruncommand and specify the arguments after the file name.

it’s possible to also use thebuildcommand to build your app into an executable before running the executable and specifying the arguments.

There Are Significant Differences Between GUIs and CLIs You’ll Need to Consider

During development, if you have to choose between building a CLI or GUI for your users, you’ll need to consider many factors to make a choice.

Using CLIs over GUIs results in faster programs, easy automation, and lower resource utilization. If any of those advantages aren’t crucial to your application, you might be better off with a GUI since they’re more popular with general users.

Do you use a GUI? How about the CLI? What do these acronyms even mean, anyway?

Not all true crime is about hacking, slashing, and gore.

This small feature makes a massive difference.

I plugged random USB devices into my phone and was pleasantly surprised by how many actually worked.

Lose your laptop without this feature, and you’ll wish you had turned it on.

If an AI can roast you, it can also prep you for emergencies.

Technology Explained

PC & Mobile