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.
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
cli.py 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.
This argument will allow the user to enter one or more URLs at the command line. It’ll use the
--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.
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.
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
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.
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
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: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.