Let's be honest! True Linux users thrive in the command-line environment. That's why Linux has such a great CLI application ecosystem. If, as a developer, your core focus is on building CLI applications for the Linux platform, I've handpicked some of the best libraries for the same. No matter the type of CLI application you are making, these libraries give you the raw power to include different types of handy features without any hassle. I've included short working examples for each library to help you understand their usage. Let's get started and check out these powerful CLI libraries for Linux.
These libraries will speed up the development time by many folds. You can handpick the one you think can fulfill your application development needs. There's no steep learning curve for these libraries.
Earlier, I used C with its standard library to build CLI applications. However, these libraries have simplified the entire process, making it easy for almost any tech-savvy user to build their own CLI application.
1. argparse (Python)
If you are a Python developer, use argparse which is a powerful CLI parsing module of the standard library. It enables you to easily command-line parse arguments, flags, and much more.
You can use it for:
- Basic utilities
- Custom automation scripts
- Tools that have no external dependencies
Installation
Because it is part of the standard library, no installation is required.
Usage Example
import argparse
parser = argparse.ArgumentParser(description="Simple CLI app")
parser.add_argument("name", help="Your name")
parser.add_argument("--age", type=int, help="Your age")
args = parser.parse_args()
print(f"Hello {args.name}")
if args.age:
print(f"You are {args.age} years old")
Run
python app.py Alex --age 28
Output
Hello Alex
You are 28 years old
Reasons to Use argparse:
Let's quickly go through the reasons you may consider this library for your CLI apps.
Pros
- No installation required if Python is already available.
- No external dependencies.
- Extensive documentation for new users.
- Supports both flags and positional arguments.
Cons
- Syntax is verbose.
- Nested commands can get messy.
2. Click (Python)
One of the popular options to create full-fledged CLI applications in Python is the Click library. It has decorators with a powerful API suite to help you build complex CLI applications in no time.
Installation
pip install click
Usage Example
import click
@click.command()
@click.option('--name', prompt='Your name')
def hello(name):
click.echo(f"Hello {name}")
if __name__ == '__main__':
hello()
Run
python app.py
Output
Your name: Alex
Hello Alex
Here are some of the reasons you may want to use this library.
Pros
- User-friendly and clutter-free syntax.
- Subcommands are also supported.
- The help system is built in.
- Large community of developers.
Cons
- It may feel a bit heavy when compared to the
argparsemodule.
3. Typer (Python)
This powerful CLI library is more or less similar to FastAPI. It's a wrapper around the Click library to give you a much better development workflow.
Installation
pip install typer
Usage Example
import typer
def main(name: str, age: int = 18):
print(f"Hello {name}")
print(f"Age: {age}")
if __name__ == "__main__":
typer.run(main)
Run
python app.py Alex --age 22
Output
Hello Alex
Age: 22
Why should you use this library?
Pros
- Clean and easy-to-pick API.
- Help is generated automatically.
- Supports autocompletion out of the box.
- CLI arguments are type-safe.
Cons
- Should have at least Python 3.7
4. docopt (Python)
This one is unique as it generates a CLI parser from the user-specified help text (docstring). Have you ever had to code the parsing logic? Simply describe it in the help text, and this library takes care of it.
Installation
pip install docopt
Usage Example
"""Example CLI
Usage:
app.py greet <name>
app.py --help
"""
from docopt import docopt
args = docopt(__doc__)
if args["greet"]:
print("Hello", args["<name>"])
Run
python app.py greet Alex
Output
Hello Alex
Here are the pros and cons of this library:
Pros
- Easy to follow CLI specification.
- Light and minimal.
Cons
- Not suitable for complex applications.
5. Clap (Rust)
Developers interested in creating high-performance CLI tools can opt for the Rust-powered Clap library. It enables you to easily add subcommands, add argument structure, and include flags to the CLI tool.
Installation
Add the following to the Cargo.toml file.
clap = { version = "4", features = ["derive"] }
Usage Example
use clap::Parser;
#[derive(Parser)]
struct Args {
name: String,
#[arg(short, long)]
age: Option<u32>,
}
fn main() {
let args = Args::parse();
println!("Hello {}", args.name);
if let Some(age) = args.age {
println!("Age: {}", age);
}
}
Run
cargo run -- Alex --age 24
Let's see why this library may be the best choice for a specific type of CLI tools.
Pros
- Blazing fast performance.
- Memory-safe at compile time.
- Extensive documentation for beginners.
- Made specifically for building CLI tools.
Cons
- Requires knowledge of Rust, which has a steep learning curve.
6. Cobra (Go)
This one, too, is quite popular among developers for building CLI tools. It enables you to quickly create a prototype you can expand easily to create powerful CLI tools in no time.
Installation
go get github.com/spf13/cobra
Usage Example
package main
import (
"fmt"
"github.com/spf13/cobra"
)
func main() {
var rootCmd = &cobra.Command{
Use: "hello",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Hello Alex")
},
}
rootCmd.Execute()
}
Run
go run main.go
Output
Hello Alex
This library has the following advantages when building large CLI tools.
Pros
- Best-suited for large and complex CLI applications.
- Excellent command hierarchy.
- Rich documentation.
Cons
- Can be a bit complex for shorter scripts.
7. GNU getopt (C)
And last but not least is the getopt "C" function one can use to parse command-line flags while building CLI applications. That's what every developer was using in olden times.
Usage Example
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
int opt;
while((opt = getopt(argc, argv, "n:")) != -1) {
switch(opt) {
case 'n':
printf("Hello %s\n", optarg);
break;
}
}
return 0;
}
Compile
gcc app.c -o app
Run
./app -n Alex
Output
Hello Alex
The following are the reasons you may consider using it.
Pros
- Native Unix way to parse command-line arguments.
- Lightweight and blazing-fast.
- Tons of existing applications use it.
Cons
- You have to manually parse everything while coding.
- Not so developer-friendly.
Conclusion
For Linux, command-line interface (CLI) tools continue to be a really effective method for creating developer utilities and automation tools. A decent CLI library is helpful because it lets you:
- Easily process arguments
- Create help documentation
- Incorporate subcommands
- Make things better for users.
When picking the best library, here's what matters:
- Which programming language are you using?
- How fast does it need to be?
- How complicated is your CLI?
If you need to write something quickly, Python is a great option. But if you want to create binaries that can be distributed, modern CLI development is dominated by Rust and Go.