Simple CLI with Addition and Word Reversal

  • Share this:

Code introduction


This code uses the Click library to create a simple command-line interface, including two commands: one for adding two numbers, and another for reversing a word.


Technology Stack : The code uses the Click library to create a simple command-line interface, including two commands: one for adding two numbers, and another for reversing a word.

Code Type : The type of code

Code Difficulty :


                
                    
import click
import random

@click.group()
def cli():
    """A command-line interface with random functionality."""
    pass

@click.command()
@click.argument('number')
def add(number):
    """Add two numbers together."""
    number = int(number)
    result = number + random.randint(1, 10)
    click.echo(f"The result is {result}")

@click.command()
@click.argument('word')
def reverse(word):
    """Reverse the letters in a word."""
    reversed_word = word[::-1]
    click.echo(f"Reversed word: {reversed_word}")

cli.add_command(add)
cli.add_command(reverse)

if __name__ == '__main__':
    cli()