DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,503 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for Build CLI with Hype
riyu
riyu

Posted on

Build CLI with Hype

Hype is a newly release Python library for creating CLI with ease by using decorator without relying on any 3rd party library. In this post, I'll show you and teach you the basics and the features of Hype as well as why should you use it.

First thing first, the installation of Hype is easy!
Just run

$ pip install hypecli
Enter fullscreen mode Exit fullscreen mode

and you are good to go!

After that, let's take a look at the basic example of Hype πŸ‘€:

from hype import Hype #: Import Hype

app = Hype() #: Create hype app instance

@app.command() #: Create hype command
def greet(name: str):
    """"
    Greet the user
    """
    app.echo('Hello, {}'.format(name.capitalize()))

if __name__ == "__main__":
    app.run() #: Now, run it!
Enter fullscreen mode Exit fullscreen mode

As you can see, Hype is based on modern Python type hints. It was inspired Typer a different Python library. However Typer was built on the top of click, which is an awesome library as well!

Color Support

As I mentioned earlier, Hype doesn't rely on any third-party library but then there are some plugins that third-party library powered Hype. For example, the color printing that is powered by colorama.

Printing colors with Hype is easier than you thought. It uses a BBCode parser to parse color tags, here is the example:

from hype import print #: Print is a wrapper for 
#: standard printing and color printing

#: Note: If you want to print colored text, 
#: make sure to install the color plugin. 
#: `pip install hypecli[color]`
#: More information: https://hype.serum.studio/getting-started

print('[red]This is red[/red]') #: This will print a color red text.
Enter fullscreen mode Exit fullscreen mode

With simple BBCode parser, it makes the printing looks good and faster. For more information about colors, kindly refer to the
Documention about handling colors.

User Interface Support

Hype Comes with UI support like: Spinner, Table and Progress Bar. It is like all in one library.

Here is an example for Spinners:

from hype.ui import Spinner

with Spinner('Loading', 'arc') as spinner:
    #: Some heavy task here
    spinner.stop()
Enter fullscreen mode Exit fullscreen mode

For Table:

from hype.ui import Table

table = Table(headers=['Name', 'Age', 'Hobby'])
table.add_row(['Zenqi', '5', 'Reading'])
print(table(background_color='red')) #: or #: print(table.render())
Enter fullscreen mode Exit fullscreen mode

Ang last for the progressbar:

from hype.ui import progressbar
import time

with progressbar(100) as bar:
    time.sleep(.01)
    bar()
Enter fullscreen mode Exit fullscreen mode

More features are coming with Hype. Just read the Documentation for more information

Top comments (1)

Collapse
 
riyuzenn profile image
riyu Author

Did You Know?

Hype is way more lightweight on other libraries like Fire, Click, and Typer.

Here are some size comparisons between the libraries:

Last Edited: Aug 6, 2021.

I'm not saying this library is bad, although its size differs. The only difference is that hype is lightweight and the others are good and ready for production as well.

Join the One Year Club

You can earn this badge by being a registered member of the DEV Community for at least one year. Create an account and get started today.