Skip to content

CLI Calculator Module

src.cli.calculator

This module provides CLI commands for performing basic arithmetic operations.

It uses the typer library to define commands for addition, subtraction, multiplication, and division. Each command takes two numbers as input and prints the result to the console.

The module relies on the core.calculator module for the actual arithmetic operations.

add(a, b)

Add two numbers.

PARAMETER DESCRIPTION
a

The first number to add.

TYPE: float

b

The second number to add.

TYPE: float

Source code in src/cli/calculator.py
@app.command()
def add(a: float, b: float) -> None:
    """
    Add two numbers.

    Args:
        a (float): The first number to add.
        b (float): The second number to add.
    """
    result = calculator.add(a, b)
    typer.echo(f"The result of addition is: {result}")

subtract(a, b)

Subtract two numbers.

PARAMETER DESCRIPTION
a

The number to subtract from.

TYPE: float

b

The number to subtract.

TYPE: float

Source code in src/cli/calculator.py
@app.command()
def subtract(a: float, b: float) -> None:
    """
    Subtract two numbers.

    Args:
        a (float): The number to subtract from.
        b (float): The number to subtract.
    """
    result = calculator.subtract(a, b)
    typer.echo(f"The result of subtraction is: {result}")

multiply(a, b)

Multiply two numbers.

PARAMETER DESCRIPTION
a

The first number to multiply.

TYPE: float

b

The second number to multiply.

TYPE: float

Source code in src/cli/calculator.py
@app.command()
def multiply(a: float, b: float) -> None:
    """
    Multiply two numbers.

    Args:
        a (float): The first number to multiply.
        b (float): The second number to multiply.
    """
    result = calculator.multiply(a, b)
    typer.echo(f"The result of multiplication is: {result}")

divide(a, b)

Divide two numbers. Args: a (float): The numerator. b (float): The denominator.

Source code in src/cli/calculator.py
@app.command()
def divide(a: float, b: float) -> None:
    """
    Divide two numbers.
    Args:
        a (float): The numerator.
        b (float): The denominator.
    """
    try:
        result = calculator.divide(a, b)
        typer.echo(f"The result of division is: {result}")
    except ValueError as e:
        typer.echo(f"Error: {e}")

main(version=typer.Option(None, '--version', '-v', help="Show the application's version and exit.", callback=_version_callback, is_eager=True))

Handles the --version flag

Source code in src/cli/calculator.py
@app.callback()
def main(
    version: Optional[bool] = typer.Option(
        None,
        "--version",
        "-v",
        help="Show the application's version and exit.",
        callback=_version_callback,
        is_eager=True,
    ),
) -> None:
    """Handles the --version flag"""
    return