Coursera Learner working on a presentation with Coursera logo and
Coursera Learner working on a presentation with Coursera logo and

One of the qualities of Python is that it accompanies batteries included: it has a rich and adaptable standard library that makes it outstanding amongst other programming dialects for composing contents for the direct line. In any case, in the event that you compose contents for the order line, at that point you likewise need to give a decent direction line interface, which you can make with the Python argparse library. 

What Is a Direction Line Interface? 

The order line interface (otherwise called CLI) is a way to collaborate with a direct line content. Python accompanies a few distinct libraries that enable you to compose an order line interface for your contents, however, the standard route for making a CLI in Python is right now the Python argparse library. 

The Python argparse library was discharged as a major aspect of the standard library with Python 3.2 on February twentieth, 2011. It was presented with Python Upgrade Proposition 389 and is currently the standard method to make a CLI in Python, both in 2.7 and 3.2+ forms. 

This new module was discharged as a trade for the more established getopt and optparse modules since they were inadequate with regards to some significant highlights. 

The Python argparse library: 

$ ls

dcdb_20180201.sg4    mastro35.sg4 openings.sg4

dcdb_20180201.si4    mastro35.si4 openings.si4

dcdb_20180201.sn4    mastro35.sn4 openings.sn4

Permits the utilization of positional contentions 

Permits the customization of the prefix burns 

Supports variable quantities of parameters for a solitary choice 

Supports subcommands (A fundamental order line parser can utilize other direction line parsers relying upon certain contentions.) 

Before beginning, you have to realize how an order line interface functions, so open a terminal on your PC and execute the order ls to get the rundown of the records contained in the present index this way: 

As should be obvious, there are a lot of documents in the present catalog, however, the direction didn’t restore a great deal of data about these records. 

Fortunately, you don’t have to search for another program to have a more extravagant rundown of the documents contained in the flow catalog. You likewise don’t have to alter the ls order yourself, since it receives a direction line interface, that is only a lot of tokens (called contentions) that you can use to design the conduct of this direction. 

Presently attempt to execute the order ls once more, yet with adding the – l choice to the direction line as in the model underneath:

$ ls -l

total 641824

-rw——-  1 dave staff  204558286 5 Mar  2018 dcdb_20180201.sg4

-rw——-  1 dave staff  110588409 5 Mar  2018 dcdb_20180201.si4

-rw——-  1 dave staff    2937516 5 Mar 2018 dcdb_20180201.sn4

-rw——-  1 dave staff     550127 27 Mar 2018 mastro35.sg4

-rw——-  1 dave staff      15974 11 Gen 17:01 mastro35.si4

-rw——-  1 dave staff       3636 27 Mar 2018 mastro35.sn4

-rw——-  1 dave staff      29128 17 Apr 2018 openings.sg4

-rw——-  1 dave staff        276 17 Apr 2018 openings.si4

-rw——-  1 dave staff         86 18 Apr 2018 openings.sn4

The yield is altogether different at this point. The order was restored a ton of data about the consents, proprietor, gathering, and size of each document and the all-out registry occupation on the plate. 

This is on the grounds that you utilized the order line interface of the ls direction and determined the – l choice that empowers the long arrangement, an extraordinary configuration that profits significantly more data for each and every document recorded. 

So as to acquaint yourself with this theme, you’re going to peruse a ton about contentions, alternatives, and parameters, so we should explain the phrasing immediately: 

The contention is a solitary part of a direct line, delimited by spaces. 

An alternative is a specific kind of contention (or a part of a contention) that can change the conduct of the order line. 

A parameter is a specific kind of contention that gives extra data to a solitary choice or order. 

Think about the accompanying direction:

$ ls – l – s – k/var/log 

In this model, you have five unique contentions: 

ls: the name of the direction you are executing 

– l: a choice to empower the considerable rundown position 

– s: a choice to print the allotted size of each record 

– k: a choice to have the size in kilobytes 

/var/log: a parameter that gives extra data (the way to incline) to the direction 

Note that, on the off chance that you have different alternatives in a solitary order line, at that point you can join them into a solitary contention like this: 

$ ls – lsk/var/log 

Here you have just three contentions: 

ls : the name of the order you are executing 

– lsk: the three distinct choices you need to empower (a mix of – l, – s, and – k) 

/var/log: a parameter that gives extra data (the way to incline) to the order

When to Utilize a Direction Line Interface 

Since you comprehend what a direction line interface is, you might be pondering when it’s a smart thought to execute one in your projects. The general guideline is that, in the event that you need to give an easy to use way to deal with designing your program, at that point you ought to consider a direction line interface, and the standard method to do it is by utilizing the Python argparse library. 

Regardless of whether you’re making an intricate order line program that needs an arrangement record to work, in the event that you need to give your client a chance to indicate which design document to utilize, it’s a smart thought to acknowledge this incentive by making a direct line interface with the Python argparse library. 

The most effective method to Utilize the Python argparse Library to Make a Direction Line Interface 

Utilizing the Python argparse library has four stages: 

Import the Python argparse library 

Make the parser 

Add discretionary and positional contentions to the parser 

Execute .parse_args() 

After you execute .parse_args(), what you get is a Namespace object that contains a straightforward property for each information contention got from the direction line. 

So as to see these four stages in detail with a model, how about we imagine you’re making a program named that rundowns the records contained in the present catalog. Here’s a potential usage of your order line interface without utilizing the Python argparse library:


import os

import sys

if len(sys.argv) > 2:

    print(‘You have specified too many arguments’)


if len(sys.argv) < 2:

    print(‘You need to specify the path to be listed’)


input_path = sys.argv[1]

if not os.path.isdir(input_path):

    print(‘The path specified does not exist’)



This is a possible implementation of the command line interface for your program that doesn’t use the Python argparse library, but if you try to execute it, then you’ll see that it works:

$ python

You need to specify the path to be listed

$ python /mnt /proc /dev

You have specified too many arguments

$ python /mnt



As you can see, the script does work, but the output is quite different from the output you’d expect from a standard built-in command.

Now, let’s see how the Python argparse library can improve this code:


# Import the argparse library

import argparse

import os

import sys

# Create the parser

my_parser = argparse.ArgumentParser(description=’List the content of a folder’)

# Add the arguments




                       help=’the path to list’)

# Execute the parse_args() method

args = my_parser.parse_args()

input_path = args.Path

if not os.path.isdir(input_path):

    print(‘The path specified does not exist’)



The code has changed a lot with the introduction of the Python argparse library.