Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Cmdparse module in Python
Python's cmdparse module provides a powerful framework for building interactive command-line interfaces (CLIs). This module allows developers to create structured, user-friendly command-line applications with features like command grouping, aliases, and argument handling.
Installation and Setup
Installing Cmdparse
Install the cmdparse module using pip ?
pip install cmdparse
Basic Import
Import the module in your Python script ?
import cmdparse
Creating a Basic CLI
Let's create a simple greeting CLI that demonstrates the core functionality ?
import cmdparse
class GreetingCLI(cmdparse.CmdParse):
def do_greet(self, argv):
if argv:
print(f"Hello, {argv[0]}!")
else:
print("Hello there!")
def do_goodbye(self, argv):
if argv:
print(f"Goodbye, {argv[0]}!")
else:
print("Goodbye!")
if __name__ == '__main__':
cli = GreetingCLI()
cli.parse()
In this example, we define a class that inherits from cmdparse.CmdParse. Methods starting with do_ automatically become available commands.
Command Aliases
Add alternative names for commands using aliases ?
import cmdparse
class GreetingCLI(cmdparse.CmdParse):
def do_greet(self, argv):
if argv:
print(f"Hello, {argv[0]}!")
else:
print("Hello there!")
if __name__ == '__main__':
cli = GreetingCLI()
cli.add_alias('greet', 'hello')
cli.add_alias('greet', 'hi')
cli.parse()
Now users can type greet, hello, or hi to execute the same command.
Command Groups
Organize related commands into logical groups ?
import cmdparse
class AdvancedCLI(cmdparse.CmdParse):
def do_greet(self, argv):
name = argv[0] if argv else "there"
print(f"Hello, {name}!")
def do_farewell(self, argv):
name = argv[0] if argv else "there"
print(f"Farewell, {name}!")
def do_calculate(self, argv):
if len(argv) >= 3:
a, op, b = float(argv[0]), argv[1], float(argv[2])
if op == '+':
print(f"Result: {a + b}")
elif op == '-':
print(f"Result: {a - b}")
else:
print("Usage: calculate <num1> <operator> <num2>")
if __name__ == '__main__':
cli = AdvancedCLI()
# Create command groups
social_group = cli.add_group('social')
math_group = cli.add_group('math')
# Add commands to groups
social_group.add_command('greet', cli.do_greet)
social_group.add_command('farewell', cli.do_farewell)
math_group.add_command('calc', cli.do_calculate)
cli.parse()
Key Features Summary
| Feature | Method | Purpose |
|---|---|---|
| Command Definition | do_commandname() |
Define CLI commands |
| Command Aliases | add_alias() |
Alternative command names |
| Command Groups | add_group() |
Organize related commands |
| Argument Handling |
argv parameter |
Process command arguments |
Best Practices
When building CLIs with cmdparse ?
- Always validate arguments before processing them
- Provide helpful error messages for invalid input
- Use command groups to organize complex applications
- Add aliases for frequently used commands
- Include help documentation for each command
Conclusion
The cmdparse module provides an excellent foundation for building interactive command-line applications in Python. With features like command grouping, aliases, and structured argument handling, you can create professional CLIs that are both powerful and user-friendly.
