Skip to content

A simple package for generating menus for cli interactive programs where user input is required.

License

Notifications You must be signed in to change notification settings

Abishevs/MenuCraft

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cli Menu helper/utility

I got tired of using while loops over and over again to validate user inputs in simple programs that I have in school. I wanted to focus on the core logic there rather that spend time reinventing the same looking menus.

How it usualy goes. You have a list of menus u want to print out pretty. Usualy with numbers in front

That would look like something like this

Example:

Available commands: 
1) menu_item 
2) menu_item
>> 

or

other title
1) menu_item 2) menu_item
>> 

No problem if you just have one menu. But if you have multiple menus and if your user has the option to come back to slightly diffrent menu? It's a hassle and simply put annoying. Because u need to validate input, map it either to match the string you specified or simply use numbers. And what if you want to use both- numbers and the actual name as user input?

Backstory

When I was building interactive fiction game in Python I came up with a solution. Why not create an Menu object? And have all creating(printing) and validating logic inside there? Yo you could even add extra function calls within those elements that are not even present by them selves in the menu. Like in interactive fictions "hint" option.

After I was done with my game and got my hands on new interactive menu programs, like simple book store I created and uploaded my package on pypi so that I could use it whenever I want.

So here is my solution :)

Usage:

Initalization

pip install menucraft

Takes user options as an python list like this(mandatory to be inside an list):

from menucraft.menu import Menu 

instance_name = Menu(["option_1", "option_2"])

⚠️ Must be writen in lower case!

Method show(inline, title)

Method to print out options. Takes arguments: title:str, inline:bool

  1. Title: available commands (default value). inline = True(default)
Available commands: 1) option_1 2) option_2
  1. title="Custom title :)", inline = False
Custom title :)
1) option_1 
2) option_2

Method index(menu_item_index)

Method to use in if statements, simply useses the list items index Remebember: list start with number 0. so first option_1 would be index(0) Return the element of the index.

instance_name = Menu(["option_1", "option_2"])
instance_name.show()
choice = instance_name.validator()

if choice == instance_name.index(0): #This will be choice_1
   print("do something")
else:
   print("can only be the last element") # this will be choice_2 or last element in the list

Method validator()

choice = instance_name.validator()

choice is the variable that will be used in if statements to compare to instance_name.index(index)

  • Takes an user input can be both numbers and full word, input is not case sensitive (human error handled)
  • error messages
  • returns an validated user option

Method all()

syntax

choice = instance_name.all()

uses instance_name.show()

  1. prints out the options
  2. Returns an validated choice from the options initiliized

TODO

  • come up with better way of adding menu items. Instead of passing them as [item...]
  • Make so that you can pass in functions that require to pass in user input that is validated inside there
  • More customization for example to be able to change ">> " to smth else?

About

A simple package for generating menus for cli interactive programs where user input is required.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages