DEV Community

Cover image for Understanding Command-line (CLI) syntax for Gophers
Josh Duffney
Josh Duffney

Posted on

Understanding Command-line (CLI) syntax for Gophers

The 2022 Go Developer Survey reveals that building CLI applications ranks as the second most popular application of Go, with an impressive majority of over 60% of respondents expressing their preference for writing in Go.

What this data means is, if you want to become a productive Go developer, learning how to build excellent and user friendly CLI tools is necessary. But, before you begin diving into the depths of os.Args, the flags package, and the Cobra framework, it essential that you have a mental model for how command-line applications word and the terminology used to describe them.

In this article, you'll dig into the world of CLI syntax, unravel its complexities, and empower you to navigate the command line with confidence.

Natural language comparison

Natural language comparison is a term used to describe a user's ability to interact with a CLI application by expressing their intentions in familiar, human-like language, which allows for a more intuitive and user-friendly experience.

The Cobra Framework, the most popular CLI framework written in Go, suggests the following pattern in their concept's documentation.

Natrual language comparison

In this example: APPNAME is the executable, NOUN is the argument, VERB is a command, and ADJECTIVE is an option, also called a flag.

If you used CLIs for any length of time you'll have noticed that not every CLI follows this exact pattern, another common implementation of the natural language comparison is verb noun, which PowerShell, a popular scripting language uses. With that said, noun verb is a much more common pattern among CLIs.

Now that you've begun to build your mental model for building CLIs with natural language. Let's dive deeper into what exactly arguments are.

Understanding Arguments

Arguments are the nouns or things that commands act upon.

They are the values that provide data or instruction to a command when it's invoked. Influencing the commands behavior or determining the task to that's performed.

For example, in the command touch file.txt, "touch" is the command, and "file.txt" is the argument or noun, indicating the file to be acted upon.

touch command example

Note: "argument" and "parameter" are sometimes used interchangeably, but they have distinct meanings.

Arguments are values passed during command execution, while parameters are variables defined within functions or methods.

Exploring Commands

Commands are - verbs - the main actions in a CLI application.

You use them by typing the executable's name followed by arguments or options\flags. Which allows users to perform specific operations.

For example, the following commands each represent different actions that can be executed within the respective CLI applications.

ls
[Command==Verb]

mkdir
[Command==Verb]

git commit
[Command] [Verb]
Enter fullscreen mode Exit fullscreen mode

For simple command-line tools like ls and mkdir the names of the commands themselves are the verbs.

But that's not the case for more complex CLIs like git and npm. With more complex CLIs the verb shifts to the subcommands.

Subcommands create a hierarchical structure within a CLI, grouping related actions together.

They allow users to navigate and access different sets of functionality based on the context or domain they're working with.

For example, within git, there are subcommands like "git commit," "git pull," or "git branch."

> git commit
> git pull
> git branch
Enter fullscreen mode Exit fullscreen mode

Each subcommand focuses on a specific aspect of version control within the overall "git" command.

Understanding Flags

Flags, also known as options, modify the behavior of a command or subcommand.

They act as adjectives, enabling users to customize the command's operation by specifying specific settings, enabling, or disabling features, or providing additional information.

Flags are typically represented by prefixed characters (short-flag) or words (long-flag) that indicate their purpose.

go -v #short-flag
go --version #long-flag
Enter fullscreen mode Exit fullscreen mode

In the above command, the -v and --version flag instruct the go command to return version information.

Note: Short flags are reserved for the most commonly used flags only. Not every option or flag gets a short flag assigned to it.

Conclusion

In this post, you explored the components of natural language comparison: arguments, commands, and flags.

Arguments represent the nouns or entities acted upon by commands, while commands themselves are the verbs or actions performed within the CLI. Flags, acting as adjectives, modify the behavior of commands or subcommands, allowing users to customize their operations.

Understanding these components is crucial for building effective and user-friendly CLI applications. By leveraging natural language comparison, CLI developers can create intuitive and accessible interfaces that streamline command execution and enhance the overall user experience.

Top comments (0)