Introduction to CLIs
00:00 In the introduction, I talked a lot about the different features of command line interfaces that I was going to cover throughout this series but I never really told you what a command line interface is and how one works, so that’s the topic of this lesson. So, what is a CLI?
00:17 Well, a CLI is simply a way for a user to interact with a text-based shell interpreter, like Bash on Linux and Mac, or the Command Prompt or PowerShell on Windows.
00:30 And it might not be clear to you why that’s an important thing, but most of the internals of a computer and the closer to system-level processes can be directly interacted with through a terminal, and so that’s why command line interfaces remain so popular and so ubiquitous throughout the programming world and the world of computer science as a whole.
A command line interface is generally composed of a command—in this case,
ls, which lists the contents of a directory—zero or more arguments, documentation, and terminal output. So, you enter a command, you get some output. It’s that simple.
01:08 And then, there might be some extra arguments that change the behavior.
Command line arguments have several different types and functions. First, there are options, which are normally proceeded by a hyphen (
-) or two hyphens (
Options are arguments that modify the behavior of a particular command or program. So in this case, the program is
cp (copy) and it copies a file into a different file location.
It has options, for example, the
-i option, which makes the program a little bit more interactive, and then the
-v option, which makes the output more verbose. Then, you have operands, which are the source or destination data to be processed.
01:47 So, options don’t deal with data but operands are, in fact, the data sources and destinations, like in this little diagram here. And then finally, you might also have subcommands, which are simply a way of modularizing command line arguments so that you have sub-programs, essentially.
So, you have a command and then you might have subcommands. One example of this is
git—if you’re familiar with the
git version control system—you might have
git merge—all those things are subcommands.
02:16 And one thing I forgot to mention is that options might themselves have operands or arguments to those options, so you might have an option that takes in some parameter after that.
02:28 There are many different sets of standards for how command line interfaces should be structured. Some of the most common are the POSIX, GNU, and docopt standards, which come from the C and Linux and Windows programming communities.
02:43 Now, all of these have many different ideas about how a command line interface should be structured, and I won’t read all of these here, but generally, most modern command line interfaces actually synthesize the information from many different standards to come to an application that works in terms of all of these different standards.
POSIX says a program should be followed by options, option arguments, and operands, it says that a hyphen—or a minus—is what denotes an option, and it says option-arguments should not be optional. GNU accounts for these
--help standard options which—as you might guess—just generate the version and some help text.
03:21 They also say that you can have long-named options which are equivalent to single-letter Unix-style options. And then, finally, docopt allows stacking of short options and some different forms of long options.
03:33 So, all of these have different ideas about how these command line interfaces should look, and it’s really the synthesis of all this information that makes for a really modern, usable command line interface.
To show you an example of a command line interface I’ll use the
ls command, which is a simple command that just lists the contents of a current directory in a Bash shell. As you can see, this directory has the content
subdir/, which is itself a directory, and
test.txt. Of course, this works on the current directory without any arguments, but if I pass in the directory to
ls, that directory will be listed. It just has a few text files in it.
I can also pass in an option, like the
-l option, which makes the output into a list with a little bit more information. Now finally, I can show you the use of option-arguments by actually piping this command into
a new command—the
head command, which has an option
-n, which is a number of lines to output. Instead of outputting the four lines up here, I’ll just output
So, that’s an example of how an option-argument functions. You have an option, which itself takes in an argument. Now to demonstrate subcommands, I’ll have to use a more complex command line interface and I’ll choose
git, just for fun here. So first, I’ll show you the
--help output for
git --help, which shows all of these common
04:58 As you can see, these are all subcommands.
The usage of a subcommand is really simple. It’s pretty much just like a fancy argument or a fancy operand. I can pass in, say,
git init, and it will initialize an empty
.git/ repository in this directory.
05:17 So, that’s an example of one kind of subcommand, but subcommands—of course—can become much more complex, because each subcommand can itself take in many different kinds of arguments and options, and all of the things that I’ve been talking about.
05:29 You can even have sub-subcommands, if you really want to. In the next lesson, I’ll show you how Python command line interfaces work—that is, command line interfaces written in Python—and I’ll show you how that inherits directly from the C programming language.
head is a Unix command only available on macOS and Linux. I’m not sure if there’s a Windows equivalent.
Become a Member to join the conversation.
sheldon002 on Jan. 22, 2022
headcommand doesn’t work? Below is what I did and got in PyCharm Terminal window: