Skip to main content

PromptWithOptions

Command line input with options for Python

Example

from promptwithoptions import (
    set_prompt_defaults,
    promptwithoptions,
)

clr_yellow = "\u001b[33m"
clr_l_green = "\u001b[32;1m"
clr_blue = "\u001b[34m"
clr_l_blue = "\u001b[34;1m"

my_options = ('one', 'two', 'three')

set_prompt_defaults(options_line_color=clr_yellow, options_number_color=clr_l_green, input_line_color=clr_blue, confirm_line_color=clr_l_blue)

promptwithoptions('How many', options=my_options, default=1, show_confirmation=True)

Available settings

These are all optional named arguments (in this order) of set_prompt_defaults and promptwithoptions.

prompt: prompt string - this appears as a question with a ? appended as long as hide_questionmark is not True

options: list of available options, without this

data_type: a callable that raises an exception if the input is invalid (e.g. int, bool)

default: default in case of empty input

allow_empty: boolean, if True then prompt doesn't repeat with empty input

allow_multiple: boolean, if True then comma-separated values are accepted and the return value is always a list (in fact a tuple)

allow_repetitive: boolean, if True then the same values can be added multiple times - only considered if allow_multiple is True

show_confirmation: boolean, if True then the accepted input is reprinted with the selected option (if options are given)

hide_key: boolean, if True the first item of each option doesn't get printed on the screen (if an option has multiple items like keys and values)

hide_questionmark: boolean, if True then '?' is not attached to prompt text (neither a ':' to the confirmation if shown)

hide_mandatory_sign: boolean, if True then '*' doesn't appear after the question if allow_empty is not True

hide_multiple_choice_sign: boolean, if True then '…' doesn't appear after the question if allow_multiple is True

no_interaction: boolean, if True default is applied automatically (if given) and no input is required (this is to apply --yes)

options_line_color: if given, options are displayed as a numbered list - this is the colour of the list items

options_number_color: if given, options are displayed as a numbered list - this is the colour of the numbers

input_line_color: as is

confirm_line_color: as is

Setting and resetting defaults

set_prompt_defaults() can be called multiple times.
A default value can be removed (set back to None) when _None_ is passed down like set_prompt_defaults(allow_empty="_None_"). Argument defaults can be removed at once by calling reset_defaults (from promptwithoptions import reset_defaults).

Options list

It's a plain list of strings, a list of keys and values or a dict.
If there are keys and values only a key is returned at the end. In that case keys can be stopped from printing by hide_key.

Multiple Choice

When allow_multiple is True then multiple values can be added if separated by ,s. Commas can be escaped by quotation marks similarly to CSV escape rules.

Entering empty value when default is given

Use '-' to explicitely get empty even when default is given and allow_empty=True.
Escape it as '-' if you need a literal hyphen.

Metadata

Release files for promptwithoptions 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for promptwithoptions 1.0.3
File Size Uploaded Provenance Actions
promptwithoptions-1.0.3.tar.gz 8.2 kB Without provenance Details for promptwithoptions-1.0.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for promptwithoptions 1.0.3
File Interpreter ABI Platform Actions
promptwithoptions-1.0.3-py3-none-any.whl Python 3 none any Details for promptwithoptions-1.0.3-py3-none-any.whl

Total release size: 15.4 kB

Release files / promptwithoptions-1.0.3.tar.gz

Download URL promptwithoptions-1.0.3.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
97797be8652d449e8ef2b65c0bd9202de85f2d1a884d1f0c0e64abbbaa31de14
BLAKE2b-256 checksum
How to use checksums
eed255863085d90a4321b4ec07cf5eac19e94de707626eca67b7c1bd519cd961
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.8.6 Linux/5.10.10-051010-generic

Release files / promptwithoptions-1.0.3-py3-none-any.whl

Download URL promptwithoptions-1.0.3-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ac64e56be230c612c89d6a5bf43e869629f9b4b57ab53f097198a71c636a88a9
BLAKE2b-256 checksum
How to use checksums
ec840ed3bdd1da579b0f4be2571b6d60dd43ef78174add09eb6b89333a92e1be
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.8.6 Linux/5.10.10-051010-generic

Security for promptwithoptions 1.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page