Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

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.

A Simple CLI Prototype

00:00 In this lesson, I’ll take you through a really simple command line interface prototype. That’s kind of what you might call a naive implementation because it only uses Python list comprehensions and conditional logic to do its parsing, and it doesn’t include any kind of type validation.

00:17 The first script I’m going to show you is, which is a simple command line interface that takes in any number of arguments along with one of three options.

00:27 It then transforms the arguments and prints them out according to the following rule. -c option means that all of the arguments printed out will be capitalized, whereas the -u option makes them uppercase, and the -l option makes them lowercase.

00:42 Now, this naive script separates the options and the arguments in the following way. It uses list comprehensions to filter the options as any argument passed in from the command line that starts with a hyphen.

00:55 Then, it gets the operands—or the arguments, which I’ve just named them for visual distinction from opts—are just anything that doesn’t start with a hyphen.

01:05 So, this gets the options and the arguments apart quite easily. Then, the logic to decide what to do based on which option was included can be simply if the "-c" option is in options then print out the space-joined version of [arg.capitalize() for arg in args].

01:28 So, just print out all the args and make them capitalized. The other two will work in exactly the same way. So if "-u" is in the opts, then you print out the uppercase version of all the args, and if "-l" is in the opts, you print out the lowercase version of all of the args.

01:45 So, let’s see it run. python and I’ll pass in the capitalize flag for a few arguments—and it capitalizes

01:56 them. So that works just great. And then I’ll pass in a -u here and—I wrote .uppercase(),

02:04 but I should have just written .upper() and .lower(). That happens sometimes. It prints out A FEW ARGUMENTS in the uppercase version, and then I can do lowercase and I can say A Few ARGUMENTS, and it will print them out all in lowercase. So, it works just fine. That’s good to hear. Now, you’re probably already noticing that this has a lot of limitations. First off, this separation strategy, which separates options and arguments based on purely a list comprehension, isn’t great because, A, it doesn’t enforce an ordering—so, # Enforces no orderingso you can put in these options and arguments in any order that you want.

02:43 And then also, it has no support for option arguments.

02:49 So, this isn’t great because it’s not very flexible. There’s not really much you can do with this, aside from just have simple options that will apply to every argument. And then, of course, this logic, here, actually makes the behavior of the final product a little bit unpredictable. So for example, if I say python and then I pass in the lowercase option and then I say A Few ARGUMENTS, but then I pass in the uppercase option, it’s actually the uppercase option that prevails.

03:17 And that’s only because, as I said earlier, this enforces no ordering, and then "-u" happens to come before "-l" in this list of conditional logic.

03:26 So, even though this simple script really works well for its purpose, it has some interesting edge cases in behavior that you wouldn’t quite expect, because it doesn’t have a sophisticated enough parsing strategy. In the next lesson, I’ll show you how you can use regular expressions to get a little bit more in-depth with parsing and enforcing some of these things that this script doesn’t actually do.

Become a Member to join the conversation.