python-inquirer

repository·main·Indexed 22 days ago

https://github.com/magmax/python-inquirer

A collection of interactive command line user interface components based on Inquirer.js. It simplifies asking questions, parsing and validating answers, and managing hierarchical prompts in CLI applications. Supported question types include Text, Editor, Password, Confirm, List, Checkbox, and Path. The library provides the `inquirer.prompt()` function for managing question lists and shortcut functions for one-off prompts.

Tokens
7K
Snippets
31
Records
41
Agent score
72%

What's inside python-inquirer

  1. Overview of Inquirer's capabilities

    main

    Inquirer is a Python library designed to simplify interactive command-line interfaces. It focuses on easing the process of:

    • Asking questions: Presenting various prompt types to the end user.
    • Parsing: Converting user input into usable data types.
    • Validating: Ensuring answers meet specific criteria before proceeding.
    • Managing hierarchical prompts: Handling complex, nested question flows.
    • Providing error feedback: Communicating issues with user input clearly.
  2. Apply themes to the terminal prompt

    main

    You can change the colorscheme and icons by passing a theme object from inquirer.themes to the prompt.

    Pre-defined themes include:

    • Default
    • GreenPassion
    • BlueComposure

    Note: Not all terminals support all colors or Unicode characters. Test your theme in your target environment.

  3. How to use python-inquirer

    main

    To use inquirer, follow these two steps:

    1. Create an array (list) of Question objects.
    2. Call inquirer.prompt(questions).

    The prompt function will interact with the user and return a dictionary where the keys are the question names and the values are the user's responses.

    import inquirer
    from inquirer import Text
    
    questions = [
        Text('name', message="What's your name?"),
        Text('surname', message="What's your surname, {name}")
    ]
    
    answers = inquirer.prompt(questions)
    # answers will be something like: {'name': 'John', 'surname': 'Doe'}
    import inquirer
    from inquirer import Text
    
    questions = [
        Text('name', message="What's your name?"),
        Text('surname', message="What's your surname, {name}")
    ]
    
    answers = inquirer.prompt(questions)
  4. How ConsoleRender handles user input formatting

    main
    When using ConsoleRender for interactive prompts, the renderer automatically escapes curly braces { and } in the current user input. This prevents ValueError or formatting errors when the renderer attempts to interpolate the current value into the question header template. Developers do not need to manually escape these characters in their input validation or custom rendering logic.
  5. How TaggedValue works for choice labels

    main

    When defining choices for List or Checkbox questions, you can provide a tuple in the format (value, label).

    Inquirer uses the TaggedValue abstraction to handle these. The value is what will be returned in the final answers dictionary, while the label is what the user actually sees in the terminal interface. This allows you to present human-readable strings while working with programmatic identifiers (like IDs or enums) in your code.

    from inquirer import List
    
    # The user sees 'Option A', but the result will be 1
    question = List(
        "choice", 
        message="Pick one", 
        choices=[(1, "Option A"), (2, "Option B")]
    )
  6. Use Checkbox questions for multiple selection

    main

    Use inquirer.Checkbox to allow users to select one or more options from a list.

    Options:

    • choices: A list of strings representing the available options.
    • carousel: If set to True, the selection will rotate (wrapping from the last choice back to the first and vice versa).
    • locked: A list of choices that cannot be removed by the user. This is useful for enforcing that certain options must remain selected.
    import inquirer
    
    questions = [
      inquirer.Checkbox('interests',
                        message="What are you interested in?",
                        choices=['Computers', 'Books', 'Science', 'Nature', 'Fantasy', 'History'],
                        ),
    ]
    answers = inquirer.prompt(questions)
  7. Use Editor questions for large text inputs

    main

    Use inquirer.Editor when you need to collect large amounts of text. This opens the user's external text editor.

    Inquirer determines the editor using the following priority:

    1. The $VISUAL environment variable.
    2. The $EDITOR environment variable.
    3. System fallbacks in order: vim -> emacs -> nano.
    import inquirer
    
    questions = [
      inquirer.Editor('long_text', message="Provide long text")
    ]
    answers = inquirer.prompt(questions)
  8. Use the checkbox question type

    main

    The checkbox question type allows users to select multiple options from a list.

    Keyboard Shortcuts

    In addition to standard controls, checkbox supports:

    • Ctrl + A: Select all choices.
    • Ctrl + R: Un-select all choices (including defaults).
    • Ctrl + I: Invert/toggle all choices.

    Using Tagged Choices

    You can provide a list of tuples to the choices parameter instead of plain strings. The first element of the tuple is the label displayed to the user, and the second element is the actual value returned by the question. This is useful when you want the user to select a human-readable string but your code needs a different data type (like an ID or object).

    # Example of tagged choices (see examples/checkbox_tagged.py)
    # choices = [("Label 1", "value1"), ("Label 2", "value2")]