Skip to content
Hyperoot edited this page May 9, 2023 · 3 revisions

hyper_cli

hyper_cli is a Python library that provides a command-line interface (CLI) framework for building interactive menu-driven programs. It simplifies the process of creating menus, handling user input, and executing functions based on the user's choices.

Installation

To use hyper_cli, you need to have Python installed on your system. You can install hyper_cli using pip:

pip install hyper_cli

Usage

Here is an example of how to use hyper_cli to create a simple menu-driven program:

from hypercli import cli
import webbrowser


def author_name():
    print("HYP3R00T")


def open_website():
    webbrowser.open("https://hyperoot.dev")


def greet(*args):
    if len(args) > 0:
        name = args[0]
        print(f"Hello, {name}!")
    else:
        print("Hello!")


def add_numbers(*args):
    if len(args) > 1:
        num1 = int(args[0])
        num2 = int(args[1])
        result = num1 + num2
        print(f"The sum of {num1} and {num2} is: {result}")
    else:
        print("Invalid input! Please provide two numbers.")


hyper = cli()

# Set up the banner
hyper.create_banner("hypercli", figlet_font='slant', banner_color='yellow', justify='center')

# Set up the intro
hyper.create_intro("Intro", "An elegant solution to interact\n with command line tools",
                   intro_title_color='blue', intro_color='white', justify='center')

# Set up the main menu
hyper.create_menu("Main Menu", "Enter your choice", context_color='cyan', option_color='magenta',
                   exit_color='red', show_border=True)

# Add options to the main menu
hyper.add_option("Main Menu", "Checkout the Sub Menu", "Sub Menu")
hyper.add_option("Main Menu", "Greet", greet, "John")
hyper.add_option("Main Menu", "Print Author Name", author_name)
hyper.add_option("Main Menu", "Perform Math Operations", "Math Operations")

# Set up the math operations menu
hyper.create_menu("Math Operations", "Select an operation", context_color='green', option_color='yellow',
                   exit_color='red', show_border=False)

# Add options to the math operations menu
hyper.add_option("Math Operations", "Addition", add_numbers, 10, 20)
hyper.add_option("Math Operations", "Multiplication", None)

# Set up the sub menu
hyper.create_menu("Sub Menu", "Enter your choice", context_color='blue', option_color='white',
                   exit_color='red', show_border=True)

# Add options to the sub menu
hyper.add_option("Sub Menu", "Go Back to Main Menu", "Main Menu")
hyper.add_option("Sub Menu", "Checkout the Website (hyperoot.dev)", open_website)

# Show the CLI
hyper.show_cli()

Class Reference

cli Class

The cli class is the main class of the hyper_cli library. It provides methods for creating menus, adding options, and showing the CLI.

create_banner(banner, figlet_font='big', banner_color='green', justify='center')

Creates a banner to be displayed at the top of the CLI.

  • banner: The text to be displayed in the banner.
  • figlet_font: The font to use for the banner. Defaults to 'big'.
  • banner_color: The color of the banner. Defaults to 'green'.
  • justify: The justification of the banner. Can be 'left', 'center', or 'right'. Defaults to 'center'.

create_intro(intro_title, intro, intro_title_color='red', intro_color='blue', justify='center')

Creates an introduction panel to be displayed before the menu.

  • intro_title: The title of the introduction panel.
  • intro: The text of the introduction panel.
  • intro_title_color: The color of the title. Defaults to 'red'.
  • intro_color: The color of the text. Defaults to 'blue'.
  • justify: The justification of the panel. Can be 'left', 'center', or 'right'. Defaults to 'center'.

create_menu(menu_name, detail=None, context_color="yellow", option_color="white", exit_color="red", show_border=False, show_exit=True)

Creates a menu with options.

  • menu_name: The name of the menu.
  • detail: The text that will be displayed at the top of the menu.
  • context_color: The color of the menu's context. Defaults to 'yellow'.
  • option_color: The color of the options in the menu. Defaults to 'white'.
  • exit_color: The color of the exit option. Defaults to 'red'.
  • show_border: If True, a border will be drawn around the menu. Defaults to False.
  • show_exit: If True, the exit option will be shown. Defaults to True.

add_option(menu_name, option, function=None, *args, **kwargs)

Adds an option to a menu.

  • menu_name: The name of the menu you want to add the option to.
  • option: The name of the option to add to the menu.
  • function: The function to be called when the option is selected.
  • args: Additional positional arguments for the function.
  • kwargs: Additional keyword arguments for the function.

show_cli(menu_name=None)

Prints a menu to the console.

  • menu_name: The name of the menu to show. If not provided, the first menu created will be shown.

Returns the value of the option that was selected.

enter_choice(menu_name, show_exit)

Takes a menu name and a boolean value, and returns a function based on the user's choice.

  • menu_name: The name of the menu you want to display.
  • show_exit: If True, the user can exit the menu by entering 0.

Returns the function that is being called.

Banner Class

The Banner class is used to create and display banners in the CLI.

__init__(self, banner, figlet_font, banner_color, justify)

Creates a new instance of the Banner class.

  • banner: The text to be displayed in the banner.
  • figlet_font: The font to use for the banner.
  • banner_color: The color of the banner.
  • justify: The justification of the banner.

show_banner()

Prints the banner to the screen in a fancy way.

Intro Class

The Intro class is used to create and display introduction panels in the CLI.

__init__(self, intro, intro_title, intro_title_color, intro_color, justify)

Creates a new instance of the Intro class.

  • intro: The text that will be displayed.
  • intro_title: The title of the intro.
  • intro_title_color: The color of the title of the intro.
  • intro_color: The color of the text in the intro.
  • justify: The justification of the panel.

show_intro()

Prints the introduction panel to the screen.

Common_func Class

The Common_func class provides common functions used in the CLI.

exit()

Clears the screen, prints a message, and exits the program.

Returns the exit function.

error()

Prints an error message and exits the program.

Returns the exit function.

clear()

Clears the screen based on the operating system. On Windows, it uses the cls command. On other systems, it uses the clear command.

Conclusion

hyper_cli is a versatile CLI library that simplifies the process of creating menu-driven programs. It provides an easy-to-use interface for creating menus, handling user input, and executing functions based on user choices. With its customizable banners, introductions, and menu styles, hyper_cli offers a great solution for building interactive command-line applications.