Skip to main content

ArgparsePrompt

ArgparsePrompt is a wrapper for the built-in Argparse, allowing missing command-line arguments to be filled in by the user via interactive prompts

Installation

ArgparsePrompt can be installed using pip:

pip install argparse_prompt

Usage

Basic Usage

The only public interface of this module is the PromptParser class, which is a subclass of Python's ArgumentParser. Use this class in exactly the same way that you would use ArgumentParser, except that, if any argument does not have a specified default value, and a value is not provided for it on the commandline, the PromptParser will prompt for a value for this argument using input(), which is read from stdin.

Consider the code below (taken from one of the unit tests):

from argparse_prompt import PromptParser

parser = PromptParser()
parser.add_argument('--argument', '-a', help='An argument you could provide', default='foo')
print(parser.parse_args().argument)

If you run this script with a value for argument, the parsing will run as normal:

$ python test/default_parser.py --argument 12
12

However if you don't specify a value for arg, the parser will prompt you for one

$ python test/default_parser.py
argument: An argument you could provide
> (foo) car
car

Default Values

Since this argument has a default value, you can also just hit enter and this value will be used automatically:

python test/default_parser.py
argument: An argument you could provide
> (foo) 
foo

Type Checking

You can also specify a type for the argument in the normal way:

from argparse_prompt import PromptParser

parser = PromptParser()
parser.add_argument('--argument', '-a', help='An argument you could provide', type=int)
print(parser.parse_args().argument)

If you do, this type checking will be used for the value you enter at the prompt:

$ python test/typed_parser.py  
argument: An argument you could provide
abc
Argument "argument" was given a value not of type <class 'int'>

Secure Values

For arguments that need to be secure, such as passwords, secret keys etc, you can use secure=True when defining your argument. This will cause whatever the user inputs for that field to be hidden from the terminal, in the same way that git, or ssh hides the password input.

For example:

parser.add_argument('--password', '-p', help='A very secret password', secure=True)

Situationally Disabling the Prompt

If you use the prompt argument to add_argument, parsing will be disabled:

parser.add_argument('--argument', '-a', help='An argument you could provide', default='foo', prompt=False)

Also, if you want to disable all prompting (for an automated script, for example), just set the ARGPARSE_PROMPT_AUTO to a truthy value:

ARGPARSE_PROMPT_AUTO=1 python test/default_parser.py

Release files for argparse-prompt 0.0.5

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

Source distribution (sdist)

Source distribution for argparse-prompt 0.0.5
File Size Uploaded
argparse_prompt-0.0.5.tar.gz 3.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for argparse-prompt 0.0.5
File Interpreter ABI Platform
argparse_prompt-0.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 19.9 kB

Release files / argparse_prompt-0.0.5.tar.gz

Download URL argparse_prompt-0.0.5.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
3bdfab80fdfe200c1f3fc091021d847cd64df5104a465fc7ac14c2e78c9686c5
BLAKE2b-256 checksum
How to use checksums
e33f20f0a59513a65a280c533de318c6432f26d13d6e87f33a530d4acce3aa85
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.20.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.9

Release files / argparse_prompt-0.0.5-py3-none-any.whl

Download URL argparse_prompt-0.0.5-py3-none-any.whl
Size 16.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
64ff13b95b36fa0bee53b1ec61fe592013635b181678355c1526c2e8705ce9a9
BLAKE2b-256 checksum
How to use checksums
77c7519218fff3922f1e62b164c5850520c402cca475831612d7efd0c45b8895
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.20.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.9

Release history Release notifications | RSS feed

This release

0.0.5 This release

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

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