Wrapper for the built-in Argparse, allowing missing command-line arguments to be filled in by the user via interactive prompts
Project description
# ArgparsePrompt
Wrapper for the built-in Argparse, allowing missing command-line arguments to be filled in by the user via interactive prompts
# Installation
ArgparsePrompt has not yet been published to PYPI, but in the meantime, you can install it using pip+git:
```bash
pip install git+https://github.com/MelbourneGenomics/ArgparsePrompt
```
# Usage
## Basic Usage
The only public interface of this module is the `PromptParser` class, which is a subclass of Python's
[ArgumentParser](https://docs.python.org/3/library/argparse.html). 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):
```python
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:
```python
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'>
```
## Situationally Disabling the Prompt
If you use the `prompt` argument to `add_argument`, parsing will be disabled:
```python
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:
```bash
ARGPARSE_PROMPT_AUTO=1 python test/default_parser.py
```
Wrapper for the built-in Argparse, allowing missing command-line arguments to be filled in by the user via interactive prompts
# Installation
ArgparsePrompt has not yet been published to PYPI, but in the meantime, you can install it using pip+git:
```bash
pip install git+https://github.com/MelbourneGenomics/ArgparsePrompt
```
# Usage
## Basic Usage
The only public interface of this module is the `PromptParser` class, which is a subclass of Python's
[ArgumentParser](https://docs.python.org/3/library/argparse.html). 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):
```python
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:
```python
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'>
```
## Situationally Disabling the Prompt
If you use the `prompt` argument to `add_argument`, parsing will be disabled:
```python
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:
```bash
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
Built Distribution
Close
Hashes for argparse_prompt-0.0.3.linux-x86_64.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 44d70a22c2b8223439d5f60152590cd3a72844c684b35f5a9dd7b29f6c72c321 |
|
MD5 | 10b0758eabe6d463e901bb5bd67cf367 |
|
BLAKE2b-256 | 2d84cdd025d04a1831f6cfb76a9822fde00560161526b8a021458747e63e0efe |
Close
Hashes for argparse_prompt-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 271ad0cc4aa5a01e0820b7fa89584501d03a7c927b2e834699328fbc67d9590d |
|
MD5 | fe878bfff801471251ab957f52d561e0 |
|
BLAKE2b-256 | 7e5e38737fd8d696a01877ef950b7067abd7319952c048230639cdaeb53cff24 |