Skip to main content

Library for quick CLI user prompts, input, and menus.

Project description

License Build Status

Introduction

This project provides a Python 2.7/3.x library that allows the user to quickly create CLI prompts for user input. The main features are the following:

  • Simple multi-entry menus.

  • Prompt for yes/no response.

  • Prompt for integer response.

  • Prompt for float response.

  • Optional default value.

  • Optional validity check.

  • Should work on any platform without additional dependencies.

Status

Currently, this project is in the development release stage. While this project is suitable for use, please note that there may be incompatibilities in new releases.

Requirements

Qprompt should run on any Python 2.7/3.x interpreter without additional dependencies.

Installation

Qprompt can be installed with pip using the following command: pip install qprompt

Additionally, Qprompt can be installed from source by running: python setup.py install

Usage

Start by importing Qprompt into your Python script:

import qprompt

You can prompt the user for various input types:

qprompt.ask_yesno()
qprompt.ask_int()
qprompt.ask_float()
qprompt.ask_str()

All prompts requiring user input will start with [?]:

qprompt.ask_int()
# [?] Enter an integer:

At any prompt, the user can enter the ? character to show valid entries:

qprompt.ask_yesno()
# [?] Proceed?: ?
# ['N', 'NO', 'Y', 'YES', 'n', 'no', 'y', 'yes']

The default prompt message can be changed:

qprompt.ask_str("Enter your name")
# [?] Enter your name:

An optional default value can be supplied:

qprompt.ask_yesno(dft="y")
# [?] Proceed? [y]:

Optional validity checks can be added:

qprompt.ask_int(vld=[1,2,3])
# [?] Enter an integer: 4
# [?] Enter an integer: 1

qprompt.ask_str(vld=lambda x: x.startswith("spa"))
# [?] Enter a string: foo
# [?] Enter a string: spam

qprompt.ask_str("Enter a path", vld=lambda x: os.path.exists(x))
# [?] Enter a path: C:\Windows

Robot problem? Try using a captcha:

qprompt.ask_captcha()
# [?] Enter the following letters, "kslg":

qprompt.ask_captcha(length=6)
# [?] Enter the following letters, "dkixzp":

Menus are easy to make:

menu = qprompt.Menu()
menu.add("p", "Previous")
menu.add("n", "Next")
menu.add("e", "Exit")
choice = menu.show()
# ** MENU **
#   (p) Previous
#   (n) Next
#   (e) Exit
# [?] Enter menu selection:

Your menus can do cool stuff by registering functions:

def foo(a, b):
    print(a + b)
menu.add("f", "foo", foo, [1, 2])

Some print-like functions:

qprompt.echo("foo")
# foo

qprompt.alert("bar")
# [!] bar

qprompt.warn("baz")
# [WARNING] baz

qprompt.error("qux")
# [ERROR] qux

Got a function that takes a while? Show that it is running with status which can be used as a function or decorator:

qprompt.status("Doing stuff...", time.sleep, [1])
# [!] Doing stuff... DONE.

@qprompt.status("Doing more stuff...")
def do_stuff():
    time.sleep(1)
do_stuff()
# [!] Doing more stuff... DONE.

Additional convenience functions:

qprompt.pause()
# Press ENTER to continue...

qprompt.hrule(width=10)
# ----------

qprompt.wrap("hello world", "hi", width=10)
# /-- hi ---
# hello world
# \---------

Check out the following additional examples of Qprompt; more can be found here:

Documentation

The full documentation for this project can be found here on Read the Docs.

Similar

The following projects are similar and may be worth checking out:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

qprompt-0.8.2.tar.gz (7.6 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page