Python CLI Application with Random Number Generation, Countdown, and Rich Formatting

  • Share this:

Code introduction


代码含义解释[英文]


Technology Stack : 代码所使用到的包和技术栈[英文]

Code Type : The type of code

Code Difficulty :


                
                    
from typer import Typer, Argument, Option
import random
import time
import rich.console

def generate_random_number(min_value, max_value):
    """Generate a random number between min_value and max_value."""
    return random.randint(min_value, max_value)

def count_down(seconds):
    """Count down from seconds and print each second."""
    for i in range(seconds, 0, -1):
        print(i)
        time.sleep(1)

def display_message(message):
    """Display a message with rich formatting."""
    console = rich.console.Console()
    console.print(message, style="bold red")

app = Typer()

@app.command()
def random_number(min_value, max_value):
    """Generate a random number between min_value and max_value."""
    num = generate_random_number(min_value, max_value)
    print(f"The random number is: {num}")

@app.command()
def countdown(seconds):
    """Count down from seconds and print each second."""
    count_down(seconds)

@app.command()
def rich_message(message):
    """Display a message with rich formatting."""
    display_message(message)

@app.command()
def all_features():
    """Show all available features."""
    print("Available features:")
    for command in app.commands:
        print(f"- {command.name}")

if __name__ == "__main__":
    app()