Table of Contents
- Introduction
- Prerequisites
- Setup
- Creating the CLI Tool
- Defining Commands
- Executing Commands
- Adding Arguments
- Handling Errors
- Conclusion
Introduction
In this tutorial, we will learn how to build our own Command Line Interface (CLI) tool using Python on a Linux system. A CLI tool allows users to interact with a program or script through a command-line interface, providing a more efficient and flexible way of executing tasks. By the end of this tutorial, you will have a good understanding of how to create a CLI tool and perform various operations using it.
Prerequisites
Before starting this tutorial, you should have:
- Basic knowledge of Python programming language
- Familiarity with the Linux operating system
- Python installed on your Linux system
Setup
To begin, let’s ensure that you have Python installed on your Linux system. Open the terminal and run the following command to check the Python version:
python --version
If Python is not installed, you can install it using the package manager of your Linux distribution. For example, on Ubuntu, you can use the following command:
sudo apt install python
Once you have Python installed, we can proceed to create our CLI tool.
Creating the CLI Tool
- Open a text editor and create a new file called
cli_tool.py
. -
Start the file with a shebang line to specify the interpreter:
#!/usr/bin/env python
-
Import the necessary modules:
import sys import argparse
-
Define a main function that will be the entry point of our CLI tool:
def main(): # Your code here pass if __name__ == '__main__': main()
- Save the file.
Defining Commands
Now, let’s define the commands that our CLI tool will support. We’ll use the argparse
module to define and parse command-line arguments.
-
Inside the
main
function, create an instance of theargparse.ArgumentParser
class:def main(): parser = argparse.ArgumentParser(description='CLI Tool')
-
Add a command using the
add_subparsers
method of the parser object:def main(): parser = argparse.ArgumentParser(description='CLI Tool') # Add a subparser for the command 'hello' hello_parser = parser.add_subparsers(dest='command') hello_parser.add_parser('hello', help='Print hello message')
-
Save the file.
Executing Commands
Next, let’s implement the execution logic for each command.
-
Inside the
main
function, add anif
statement to check the value of thecommand
variable:def main(): parser = argparse.ArgumentParser(description='CLI Tool') hello_parser = parser.add_subparsers(dest='command') hello_parser.add_parser('hello', help='Print hello message') args = parser.parse_args() if args.command == 'hello': print('Hello, world!')
-
Save the file.
Adding Arguments
Now, let’s add arguments to our commands to make them more flexible.
-
Inside the
hello
subparser, add an argument using theadd_argument
method:def main(): parser = argparse.ArgumentParser(description='CLI Tool') hello_parser = parser.add_subparsers(dest='command') hello = hello_parser.add_parser('hello', help='Print hello message') hello.add_argument('name', help='Name of the person to greet')
-
Modify the execution logic to use the value of the
name
argument:def main(): parser = argparse.ArgumentParser(description='CLI Tool') hello_parser = parser.add_subparsers(dest='command') hello = hello_parser.add_parser('hello', help='Print hello message') hello.add_argument('name', help='Name of the person to greet') args = parser.parse_args() if args.command == 'hello': print(f'Hello, {args.name}!')
-
Save the file.
Handling Errors
Let’s handle errors when the user provides invalid or missing arguments.
-
Add an
if
statement to check if thename
argument is provided:def main(): parser = argparse.ArgumentParser(description='CLI Tool') hello_parser = parser.add_subparsers(dest='command') hello = hello_parser.add_parser('hello', help='Print hello message') hello.add_argument('name', help='Name of the person to greet') args = parser.parse_args() if args.command == 'hello': if args.name: print(f'Hello, {args.name}!') else: print('Please provide a name.')
-
Save the file.
Conclusion
Congratulations! You’ve created a basic CLI tool in Python. This tutorial guided you through defining and executing commands, adding arguments, and error handling. This serves as a foundation for building more complex CLI tools. Keep practicing and experimenting with different commands and functionalities to enhance your skills. Happy coding!