Join us
@trujillo9616 ă» Dec 18,2021 ă» 5 min read ă» 1099 views ă» Originally posted on medium.com
This past week I learned how to implement a CLI application so I thought about sharing this knowledge with you. But first, what exactly is a CLI?
The Command Line Interface (CLI) is a text-based interface that is used to operate software and operating systems while allowing the user to respond to visual prompts by typing single commands into the interface and receiving a reply in the same way.
CLI allows a user to perform tasks by entering commands. Its working mechanism is very easy, but it is not user friendly. Users enter the specific command, press âEnterâ, and then wait for a response. After receiving the command, the CLI processes it accordingly and shows the output/result on the same screen; command line interpreter is used for this purpose.
Why use a CLI instead of a GUI?
Most people will never use command line interfaces, however there are some reasons why you could use CLI over a traditional GUI application.
CLI Parsers in Python
Building a CLI
For this tutorial I decided to use Argparse because it is Pythonâs standard library module for implementing CLI applications, however, the majority of the CLI libraries and frameworks work in a similar manner.
Step 1: Import argparse and create an ArgumentParser object.
These are some of the most common parameters passed to the object. You can search the documentation for more options that can be passed, but these will be enough for most of the applications.
prog â The programâs name.
description â A short description of your application.
epilog â An epilogue to be printed at the end of the help flag.
argument_default â The global default value for arguments.
add-help â Adds a help flag that prints the list of flags and commands that can be specified.
Step 2: Add Arguments
To add arguments of flags you must use the add_argument method from the ArgumentParser object. This will allow you to specify commands and their respective actions that must be done when called. These are some of the common parameters you will want to specify when declaring a new argument.
name or flag â The first and most important parameter is the name of your command or flag. Choose something that is clear to understand and representative of the action that will be performed when called.
action â The type of action that will be performed when called. The most typical is store, this will store the argumentâsvalue.
nargs â Used to specify the number of arguments your command will handle. Works best when specifying a constant number of arguments, but if you need a more flexible approach for variable input, â*â or â+â can be used instead.
default â The default value of the argument.
choices â You can restrict the command line arguments that can be selected by specifying it with a list of accepted choices.
required â Specify whether or not an argument is required.
help â Text that will be printed to describe the argument when the help function is called.
Step 3: Parsing the Arguments
After you have defined all of your commands and flags, like the example above, you will have to parse these commands. This can be done in a single line:
This will allow you to access the inputted arguments and then you can access that information to run functions or whatever action you want to perform. You can access this information by using args.ânameofcommandâ, for example, using the argument we parsed in the previous step we could have something like this to run a different function depending on the inputted value for the command.
Join other developers and claim your FAUN account now!
Influence
Total Hits
Posts
Only registered users can post comments. Please, login or signup.