1
0
Fork 0
digitalstudium.com/content/en/python-lifehacks/how-to-easily-write-linux-c...

140 lines
3.3 KiB
Markdown
Raw Normal View History

2023-04-09 13:36:53 +00:00
---
title: "Python: How to easily write a CLI tool for Linux using Fire"
2023-04-23 14:56:27 +00:00
date: 2023-04-09
2023-04-09 13:36:53 +00:00
---
I want to share the easiest way I know to write a CLI tool for Linux administration
using python and Fire.
2023-05-16 10:49:25 +00:00
<!--more-->
2023-04-09 13:36:53 +00:00
## Step 1: Install Fire
```bash
pip install fire
```
## Step 2. Create a simple CLI tool
Here is an example of a CLI tool that prints the Linux version to the terminal:
```python
#!/usr/bin/env python3
import fire
import platform
class SysInfo:
"""A CLI tool for getting system information about Linux server"""
def kernel(self):
"""A method for getting kernel version"""
version = platform.release()
return f"Kernel version: {version}"
if __name__ == "__main__":
obj = SysInfo()
fire.Fire(obj)
```
Paste this code into a file called `my-cli-tool` and give it permission to execute:
```bash
chmod +x my-cli-tool
```
Then put this file in the path `/usr/local/bin`:
```bash
sudo cp ./my-cli-tool /usr/local/bin
```
To use this tool, just type the command:
```bash
my-cli-tool kernel
```
You will see output like this:
2023-07-23 12:55:12 +00:00
```
2023-04-09 13:36:53 +00:00
my-cli-tool kernel
Kernel version: 6.2.2-060202-generic
```
As you can see, it is enough to create a class, a method(s) in it, and pass the class object to the fire.Fire() function - and the cli tool is ready!
This will automatically generate a help page, which can be called using the `--help` flag:
```bash
my-cli-tool --help
```
You will get this output:
2023-07-23 12:55:12 +00:00
```
2023-04-09 13:36:53 +00:00
NAME
my-cli-tool - A CLI tool for getting system information about Linux server
SYNOPSIS
my-cli-tool COMMAND
DESCRIPTION
A CLI tool for getting system information about Linux server
COMMANDS
COMMAND is one of the following:
kernel
A method for getting kernel version
```
2023-04-09 17:34:41 +00:00
## Step 3. Making the tool more complex
2023-04-09 13:36:53 +00:00
For example, we also want our tool to be able to print the kernel version in short form, like this: `6.2.2`.
We rewrite the code as follows:
```python
#!/usr/bin/env python3
import fire
import platform
class SysInfo:
"""A CLI tool for getting system information about Linux server"""
def kernel(self, format: ("short", "full") = "full"):
"""A method for getting kernel version"""
version = platform.release()
if format == "short":
return version.split("-")[0]
return f"Kernel version: {version}"
if __name__ == "__main__":
obj = SysInfo()
fire.Fire(obj)
```
Now we can type the following command:
```bash
my-cli-tool kernel --format short
```
Output:
2023-07-23 12:55:12 +00:00
```
2023-04-09 13:36:53 +00:00
6.2.2
```
This will also automatically update the help page, adding the `--format` flag and its possible values:
```bash
my-cli-tool kernel --help
```
Output:
2023-07-23 12:55:12 +00:00
```
2023-04-09 13:36:53 +00:00
NAME
my-cli-tool kernel - A method for getting kernel version
SYNOPSIS
my-cli-tool kernel <flags>
DESCRIPTION
A method for getting kernel version
FLAGS
-f, --format=FORMAT
Type: ('short', 'full')
Default: 'full'
2023-04-09 17:34:41 +00:00
```
## Step 4. Create a binary file
First install `pyinstaller`:
```bash
pip install pytinstaller
```
Then we run the command:
2023-04-23 21:06:03 +00:00
```bash
2023-04-09 17:34:41 +00:00
pyinstaller my-cli-tool --onefile
```
2023-04-23 21:06:03 +00:00
A folder `dist` should appear, and a binary file `my-cli-tool` inside it with all dependencies, which can be used even on servers,
2023-04-09 17:34:41 +00:00
which do not have python or fire installed. Just put this file in the path `/usr/local/bin` and `my-cli-tool` can be used!