Introduction to cmdu
The cmdu
library is a command-line utility designed to simplify tasks and enhance productivity. This guide delves into its functionalities, offering a plethora of API examples to help you leverage its capabilities.
API Examples
Initialization
To get started with cmdu
, you need to initialize it:
import cmdu
cmd = cmdu.Cmd()
Adding Commands
You can register commands with ease:
@cmd.command() def hello():
print("Hello, World!")
@cmd.command() def greet(name: str):
print(f"Hello, {name}!")
Running Commands
Executing the registered commands is straightforward:
if __name__ == "__main__":
cmd.run()
Using Arguments
Handling command-line arguments is seamless:
@cmd.command() def add(a: int, b: int):
print(a + b)
Command Descriptions
Provide descriptions for commands to make them self-explanatory:
@cmd.command(description="This command prints a greeting message.") def greet(name: str):
print(f"Hello, {name}!")
Application Example
To demonstrate the full capability of cmdu
, here is an example app:
import cmdu
cmd = cmdu.Cmd()
@cmd.command(description="Prints a simple greeting.") def hello():
print("Hello, World!")
@cmd.command(description="Greets a user by name.") def greet(name: str):
print(f"Hello, {name}!")
@cmd.command(description="Adds two numbers and returns the result.") def add(a: int, b: int):
print(f"The sum of {a} and {b} is {a + b}")
if __name__ == "__main__":
cmd.run()
This example showcases the fundamental aspects of cmdu
.
Conclusion
cmdu
is a powerful tool for creating command-line utilities with minimal effort. By following this guide and using the provided examples, you can efficiently build robust CLI applications.
Hash: f85f57a2ce18cdc1cb00e5b042bb7e3c7b3dc0828408de9caa9e6b6c770a437c