Skip to main content

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

Project description

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

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

argparse_prompt-0.0.4.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

argparse_prompt-0.0.4-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

Details for the file argparse_prompt-0.0.4.tar.gz.

File metadata

  • Download URL: argparse_prompt-0.0.4.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6

File hashes

Hashes for argparse_prompt-0.0.4.tar.gz
Algorithm Hash digest
SHA256 1790ac301e469d0929d38b28d8187cab52be219580cf18dadcf36e2efea7ee47
MD5 d7d83b35ef30a832dbe9f40596b8de9a
BLAKE2b-256 3715912789b79310d36d1d9ca624aac608467c65b93b99d08f4b79e85dcccd60

See more details on using hashes here.

File details

Details for the file argparse_prompt-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: argparse_prompt-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 16.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6

File hashes

Hashes for argparse_prompt-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3afa1dd5704d7a579e0729cdb9fb31abf4b40b40d52d622ba6a20a52e25a85da
MD5 13fb8d0d2bb11c4a5a0b32d3ac3e8bce
BLAKE2b-256 bf5d7a3bc97f5b0533b371fa11d56cd6034f04f60ea91c21bd2b8878ff228db1

See more details on using hashes here.

Supported by

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