CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-clint

Python Command Line Interface Tools for colored output, progress bars, text formatting, and argument handling

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

prompts.mddocs/

User Prompts and Input

Interactive user prompts with validation support for creating CLI applications that require user input. The prompt module provides yes/no questions, text input with validation, and multiple choice options with built-in batch mode support for automated testing.

Capabilities

Yes/No Prompts

Simple yes/no prompts with configurable defaults and batch mode support for automated testing.

def yn(prompt, default='y', batch=False):
    """
    Display a yes/no prompt to the user.
    
    Args:
        prompt (str): The question to ask the user
        default (str): Default choice ('y' or 'n'), defaults to 'y'
        batch (bool): If True, automatically returns default without user input
    
    Returns:
        bool: True if user confirms, False otherwise
    """

Usage Example:

from clint.textui import prompt

# Basic yes/no prompt
if prompt.yn('Do you want to continue?'):
    print('Continuing...')

# With custom default
result = prompt.yn('Delete all files?', default='n')

# Batch mode (for automated scripts)
result = prompt.yn('Proceed?', batch=True)  # Uses default

Text Input Queries

General text input prompts with validation support, default values, and batch mode capability.

def query(prompt, default='', validators=None, batch=False):
    """
    Prompt user for text input with validation.
    
    Args:
        prompt (str): The input prompt to display
        default (str): Default value if user provides no input
        validators (list): List of validator objects to validate input
        batch (bool): If True, automatically returns default without user input
    
    Returns:
        str: Validated user input or default value
    """

Usage Example:

from clint.textui import prompt
from clint.textui.validators import RegexValidator, IntegerValidator

# Simple text input
name = prompt.query('Enter your name:')

# With default value
email = prompt.query('Email address:', default='user@example.com')

# With validation
age = prompt.query('Enter age:', validators=[IntegerValidator()])

# Email validation
email = prompt.query('Email:', validators=[
    RegexValidator(r'^[^@]+@[^@]+\.[^@]+$', 'Enter a valid email')
])

# Batch mode
name = prompt.query('Name:', default='Anonymous', batch=True)

Multiple Choice Options

Multiple choice prompts supporting both simple lists and complex option dictionaries with custom selectors and return values.

def options(prompt, options, default=None, batch=False):
    """
    Display multiple choice options to the user.
    
    Args:
        prompt (str): The question to ask the user
        options (list): List of strings or dictionaries defining options
        default (str): Default selector value
        batch (bool): If True, automatically returns default without user input
    
    Returns:
        Various: Selected option value (depends on option configuration)
    """

Option Formats:

# Simple string list format
options = ['Option 1', 'Option 2', 'Option 3']

# Dictionary format for advanced control
options = [
    {
        'selector': '1',           # What user types to select
        'prompt': 'First option',  # Display text (optional)
        'return': 'value1'         # Return value (optional)
    },
    {
        'selector': 'a',
        'prompt': 'Alternative option',
        'return': {'key': 'value'}
    }
]

Usage Examples:

from clint.textui import prompt

# Simple string options (automatically numbered)
choice = prompt.options('Choose a color:', ['Red', 'Green', 'Blue'])
# Displays: [1] Red, [2] Green, [3] Blue
# Returns: 1, 2, or 3

# Custom dictionary options
choice = prompt.options('Select environment:', [
    {'selector': 'dev', 'prompt': 'Development', 'return': 'development'},
    {'selector': 'prod', 'prompt': 'Production', 'return': 'production'},
    {'selector': 'test', 'prompt': 'Testing', 'return': 'testing'}
])
# User types 'dev', function returns 'development'

# With default
choice = prompt.options('Environment:', 
    ['Development', 'Production'], 
    default='1'
)

# Batch mode
choice = prompt.options('Select:', ['A', 'B'], default='1', batch=True)

Integration with Validators

All prompt functions integrate with the validation system from clint.textui.validators:

from clint.textui import prompt
from clint.textui.validators import RegexValidator, PathValidator, IntegerValidator

# Multiple validators
port = prompt.query('Port number:', validators=[
    IntegerValidator('Must be a number'),
    RegexValidator(r'^[1-9]\d{3,4}$', 'Must be 1000-65535')
])

# Path validation
config_path = prompt.query('Config file:', validators=[
    PathValidator('Directory must exist')
])

Error Handling

When validation fails, prompts display error messages in yellow and repeat the question:

# If user enters invalid input, they see:
# Enter a valid number.
# Port number: [prompt repeats]

Batch Mode

Batch mode is designed for automated scripts and testing:

# In batch mode, prompts display but don't wait for input
result = prompt.yn('Continue?', batch=True)         # Uses default
name = prompt.query('Name:', default='Test', batch=True)  # Returns 'Test'
choice = prompt.options('Pick:', ['A', 'B'], default='1', batch=True)  # Returns 1

Install with Tessl CLI

npx tessl i tessl/pypi-clint

docs

arguments.md

colored-text.md

english.md

index.md

progress.md

prompts.md

resources.md

text-output.md

utilities.md

validation.md

tile.json