Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

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.

00:52 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.

01:12 Command line arguments have several different types and functions. First, there are options, which are normally proceeded by a hyphen (-) or two hyphens (--).

01:21 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.

01:33 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.

02:04 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 add, git commit, 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.

03:01 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 --version and --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.

03:44 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.

04:12 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

04:27 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 2.

04:37 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, git --help, which shows all of these common git commands.

04:58 As you can see, these are all subcommands.

05:03 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.

Become a Member to join the conversation.