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

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands 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.

Creating the Command-Line Interface

00:00 Creating the Command-Line Interface. So far, you have a working function that allows you to check if a given website is online. At the end of this step, you’ll have a minimal command-line interface (CLI) that will allow you to run your website connectivity checker app from the command line.

00:19 The CLI will include options for taking a list of URLs at the command line and loading a list of URLs from a text file. The application will also display the connectivity check results with a user-friendly message.

00:34 To create the application’s CLI, you’ll use argparse from the Python standard library. This module allows you to build user-friendly CLIs without installing any external dependencies. To get started, you’ll write the required boilerplate code for working with argparse.

00:50 You’ll also code the option to read URLs from the command line.

00:57 To build the application’s CLI with argparse, you need to create an ArgumentParser instance so that you can pass arguments provided at the command line.

01:05 Once you have an argument parser, then you can start adding arguments and options to your app’s CLI.

01:13 Open the file in your editor and add the code seen on-screen. First, you import the argparse module. Next, you create read_user_cli_args() to keep the functionality related to the argument parser in a single place. To build the parser object, you use two arguments: prog defines the program’s name, and description provides a suitable description for the application.

01:43 This description will be displayed when you call the app with the --help option. After creating the argument parser, you add a first command-line argument using the .add_argument() method.

01:56 This argument will allow the user to enter one or more URLs at the command line. It’ll use the -u and --urls switches. The rest of the arguments to .add_argument() work as follows. metavar sets a name for the argument in usage or help messages.

02:14 nargs tells argparse to accept a list of command-line arguments after the -u or --urls switch. type sets the data type of the command-line arguments, which is str in this argument.

02:27 default sets the command-line argument to an empty list by default. help provides a help message for the user. Finally, the function returns the result of calling the .parse_args() method on the parser object.

02:43 This method returns a Namespace object containing the parsed arguments.

02:50 Another valuable option to implement in your site connectivity checker is the ability to load a list of URLs from a text file on your local machine. To do this, you can add a second command line argument, which will be activated with the -f or --input-file flags.

03:07 This is achieved by adding another argument to the function you’ve already created. Here, the .add_argument() method is used with almost the same arguments as seen previously. In this case, you are not using the nargs argument, because you want the application to accept only one input file at the command line.

03:32 An essential component of every application that interacts with the user through the command line is the application’s output. Your application needs to show the result of its operation to the user, so this feature is vital for ensuring a good user experience.

03:47 The site connectivity checker doesn’t need a very complex output. It just needs to inform the user about the current status of the checked websites. To implement this functionality, you’ll code a function called display_check_result().

04:03 Back in, add the function at the end. This function takes the connectivity check result, the checked URL, and an optional error message.

04:22 The conditional statement checks to see if result is true, in which case an "Online!" message is printed to the screen. If the result is false, then the else clause prints "Offline?" along with an error report about the actual problem that’s just occurred.

04:37 Note that the messages just seen on-screen involve Unicode thumbs-up and thumbs-down characters.

04:45 How you enter these characters depends on the operating system that you are using, but their Unicode numbers are seen on-screen, and they’re available in the course materials.

04:56 The website connectivity checker has a command-line interface to allow the user to interact with the application. Now it’s time to put everything together in the application’s entry-point script, and that’s what you’ll be doing in the next section of the course.

Become a Member to join the conversation.