DEV Community

perymerdeka for Zetta

Posted on

Create Beautiful CLI Tools using Typer

In this post i will discuss about how to create beautiful CLI Tools using library call typer with this library we can create amazing CLI Tools with that

Background

on my case i want to create interactive Scraping tools based on python without make confuse my client, then i look helper from my bash terminal, then i search tools on the internet and found the cool library.

Task

create sample tools using typer, the case is creating simple greeting apps with object oriented paradigm

Getting Started

first create repo on github and save your source code and install poetry for dependency management here

and then install the dependencies

poetry add typer
Enter fullscreen mode Exit fullscreen mode

and then create class Greetings on greet.py

from typing import Optional
from pprint import pprint

class Greetings(object):
    def __init__(self, name: str, address: Optional[str]=None, city: Optional[str]=None, state: Optional[str]=None,):
        self.name = name
        self.address = address
        self.city = city
        self.state = state

    def intro(self):
        """introduce yourself in informal event
        """
        print("Hello My name is {}, Nice to meet You".format(self.name))

    def formal_event(self):
        """introduce youself in formal event
        """
        if self.address is not None:
            print("Hello Nice to meet you, my name is {} i live in {}, {}, {}".format(self.name, self.address, self.city, self.state))
        else:
            print("Hello Nice to meet you, my name is {} Nice to Meet Yo Bro")

    def identity(self):
        """Print your identity
        """

        data_dict: dict[str, str] = {
            "name": self.name,
            "address": self.address,
            "city": self.city,
            "state": self.state,
        }
        print("here your identity")
        pprint(data_dict)

    ]
Enter fullscreen mode Exit fullscreen mode

then on main.py adding typer configuration

from typer import Typer, Option
from typing import Optional

from greet import Greetings

app = Typer()

@app.command()
def main(
    name: str = Option("", help="Enter Your Name"),
    mode: Optional[str] = Option(
        "unformal", help="help you print greeting text with unformal format"
    ),
    address: Optional[str] = Option("", help="Your Address"),
    city: Optional[str] = Option("", help="Your City"),
    state: Optional[str] = Option("", help="Your State Address"),
):
    greeting: Greetings = Greetings(name=name, address=address, city=city, state=state)
    if mode == "unformal":
        greeting.intro()
    elif mode == "formal":
        greeting.intro()
    elif mode == "unformal":
        greeting.formal_event()
    elif mode == "indentity":
        greeting.identity()

if __name__ == "__main__":
    app()
Enter fullscreen mode Exit fullscreen mode

here is an help pages

python src/main.py --help       
Usage: main.py [OPTIONS]

Options:
  --name TEXT           Enter Your Name
  --mode TEXT           help you print greeting text with unformal format
                        [default: unformal]
  --address TEXT        Your Address
  --city TEXT           Your City
  --state TEXT          Your State Address
  --install-completion  Install completion for the current shell.
  --show-completion     Show completion for the current shell, to copy it or
                        customize the installation.
  --help                Show this message and exit.
Enter fullscreen mode Exit fullscreen mode

Examples

here is an examples results

python src/main.py --name="Siti"
Hello My name is Siti, Nice to meet You # <- the results
Enter fullscreen mode Exit fullscreen mode

Conclusion

in this post just demonstrate usage of typer packages, so explore more at docs, see you in the next post :)

Note: Source Code Available at here

Top comments (0)