Skip to main content

Decorator to prompt input for missing arguments.

Project description

Decorator to prompt input for missing arguments.

Install

pip install prompt_args

Usage

from prompt_args import Prompt

print("Test automatic prompting")
@Prompt.decorate
def my_func(first_name: str, last_name: str = "", status: str = "well"):
    print(f"Hello, {first_name} {last_name}, I am {status}")

my_func()


print("\nTest controlled prompting")
@Prompt.decorate(
    first_name=Prompt("First Name: "),
    last_name=Prompt("Last Name: ", default=""),
    status=Prompt("How are you? ", default="well")
)
def my_func(first_name: str, last_name: str, **kwargs):
    print(f"Hello, {first_name} {last_name}, I am {kwargs.get('status')}")

my_func()
my_func("Jane", "Doe", status="great")

# Test error when a prompt is given, but the function argument does not exist
print("\nTest controlled prompt error")
try:
    @Prompt.decorate(
        first_name=Prompt("First Name: "),
        last_name=Prompt("Last Name: ", default=""),
        status=Prompt("How are you? ", default="well")
    )
    def my_func(first_name: str, last_name: str):
        print(f"Hello, {first_name} {last_name}")
except ValueError as e:
    assert "no corresponding function argument" in str(e)

Output

Test automatic prompting
Enter First Name: John
Enter Last Name ['']:
Enter Status ['well']:
Hello, John , I am well

Test controlled prompting
First Name: John
Last Name ['']: Doe
How are you ['well']? just fine
Hello, John Doe, I am just fine
Hello, Jane Doe, I am great

Test controlled prompt error

Register prompt helpers (“Enter … [{prompt_help}]: “).

from prompt_args import Prompt

# Test built in bool converter with Y/n prompt help info
print("Testing builtin type converter")
@Prompt.decorate
def my_func(x: int, confirm: bool = False):
    print(f"Change x to {x}? {confirm}")

# Test no
my_func()

# Test y
my_func()


# Test custom type converter
print("\nTesting custom type converter")
def long_confirm(value: str) -> bool:
    return value == "Yes I am sure"

Prompt.register_type_converter(bool, long_confirm, prompt_help="Yes I am sure/no")

@Prompt.decorate
def my_super_serious_func(x: int, confirm: bool = False):
    print(f"Change x to {x}? {confirm}")

# Test invalid y
my_super_serious_func()

# Test full Yes I am sure
my_super_serious_func()

Output:

Testing builtin type converter
Enter X: 10
Enter Confirm [Y/n]: n
Change x to 10? False
Enter X: 12
Enter Confirm [Y/n]: y
Change x to 12? True

Testing custom type converter
Enter X: 20
Enter Confirm [Yes I am sure/no]: y
Change x to 20? False
Enter X: 25
Enter Confirm [Yes I am sure/no]: Yes I am sure
Change x to 25? True

Do not prompt for some arguments

from prompt_args import Prompt

print("Test no prompting for some arguments")
@Prompt.decorate(status=Prompt.no_prompt)
def my_func(first_name: str, last_name: str = "", status: str = "well"):
    print(f"Hello, {first_name} {last_name}, I am {status}")

# Status should not be prompted
my_func()

Output

Test no prompting for some arguments
Enter First Name: John
Enter Last Name ['']: Doe
Hello, John Doe, I am well

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

prompt_args-1.0.1.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

prompt_args-1.0.1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file prompt_args-1.0.1.tar.gz.

File metadata

  • Download URL: prompt_args-1.0.1.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for prompt_args-1.0.1.tar.gz
Algorithm Hash digest
SHA256 89fea68cca908be7ba84e254eb10bb531dc2faa8c2f3058255871c5c6400cf38
MD5 100ac0fd39702d07e119009e6c6f73a4
BLAKE2b-256 dbed58d37aafae13f1045237036cdf978e9dab8639e83a3300884cfa1aac89a3

See more details on using hashes here.

File details

Details for the file prompt_args-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: prompt_args-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for prompt_args-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 90a685b35b42eda6cafa2fb2e03773fae92771877bbfb02da75dbe070f3a8340
MD5 9cb27b6959a6206364b90b6d8237cd3d
BLAKE2b-256 dec9677d4e6dca2ffa9f038515280b1c4cddd9b155e2e21e50ac84d862edecdd

See more details on using hashes here.

Supported by

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