pyinput-cli 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyinput_cli-0.1.0/LICENSE +21 -0
- pyinput_cli-0.1.0/PKG-INFO +63 -0
- pyinput_cli-0.1.0/README.md +26 -0
- pyinput_cli-0.1.0/pyinput/__init__.py +5 -0
- pyinput_cli-0.1.0/pyinput/pyinput.py +129 -0
- pyinput_cli-0.1.0/pyinput_cli.egg-info/PKG-INFO +63 -0
- pyinput_cli-0.1.0/pyinput_cli.egg-info/SOURCES.txt +10 -0
- pyinput_cli-0.1.0/pyinput_cli.egg-info/dependency_links.txt +1 -0
- pyinput_cli-0.1.0/pyinput_cli.egg-info/requires.txt +3 -0
- pyinput_cli-0.1.0/pyinput_cli.egg-info/top_level.txt +1 -0
- pyinput_cli-0.1.0/pyproject.toml +29 -0
- pyinput_cli-0.1.0/setup.cfg +4 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Pedro Alberto Rosquete Ares
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pyinput-cli
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A package for managing user input through the cli, and consistently across projects.
|
|
5
|
+
Author-email: Pedro Alberto Rosquete Ares <rosquetearespedro06@gmail.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2026 Pedro Alberto Rosquete Ares
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Documentation, https://github.com/prares-dev/pyinput-cli.git
|
|
29
|
+
Project-URL: Homepage, https://github.com/prares-dev/pyinput-cli.git
|
|
30
|
+
Project-URL: Repository, https://github.com/prares-dev/pyinput-cli.git
|
|
31
|
+
Requires-Python: >=3.9
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
License-File: LICENSE
|
|
34
|
+
Provides-Extra: dev
|
|
35
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
|
|
38
|
+
# pyinput-cli
|
|
39
|
+
**The definitive input handler for your python cli programs.**
|
|
40
|
+
|
|
41
|
+
pyinput-cli is a package that provides functions to handle user input in a consistent way across any project.
|
|
42
|
+
|
|
43
|
+
- **get_str**: reads a string and can validate its length or its format as a name.
|
|
44
|
+
- **get_num**: reads an integer or float and validates minimum and maximum values.
|
|
45
|
+
- **yes_no**: asks a yes/no question and returns the boolean result.
|
|
46
|
+
|
|
47
|
+
## Example usage
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
from pyinput import get_num, get_str, yes_no
|
|
51
|
+
|
|
52
|
+
name = get_str("Name: ", name=True, max_length=30)
|
|
53
|
+
age = get_num("Age: ", min_val=0, max_val=120)
|
|
54
|
+
continue_ = yes_no("Continue?")
|
|
55
|
+
|
|
56
|
+
print(name, age, continue_)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Notes
|
|
60
|
+
|
|
61
|
+
- Empty strings are rejected by default for `get_str`.
|
|
62
|
+
- `get_num` rejects invalid input and values outside the configured bounds.
|
|
63
|
+
- The functions are designed for small interactive CLI flows and can be reused in larger projects.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# pyinput-cli
|
|
2
|
+
**The definitive input handler for your python cli programs.**
|
|
3
|
+
|
|
4
|
+
pyinput-cli is a package that provides functions to handle user input in a consistent way across any project.
|
|
5
|
+
|
|
6
|
+
- **get_str**: reads a string and can validate its length or its format as a name.
|
|
7
|
+
- **get_num**: reads an integer or float and validates minimum and maximum values.
|
|
8
|
+
- **yes_no**: asks a yes/no question and returns the boolean result.
|
|
9
|
+
|
|
10
|
+
## Example usage
|
|
11
|
+
|
|
12
|
+
```python
|
|
13
|
+
from pyinput import get_num, get_str, yes_no
|
|
14
|
+
|
|
15
|
+
name = get_str("Name: ", name=True, max_length=30)
|
|
16
|
+
age = get_num("Age: ", min_val=0, max_val=120)
|
|
17
|
+
continue_ = yes_no("Continue?")
|
|
18
|
+
|
|
19
|
+
print(name, age, continue_)
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Notes
|
|
23
|
+
|
|
24
|
+
- Empty strings are rejected by default for `get_str`.
|
|
25
|
+
- `get_num` rejects invalid input and values outside the configured bounds.
|
|
26
|
+
- The functions are designed for small interactive CLI flows and can be reused in larger projects.
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import math
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from typing import Any, Callable, Optional, Tuple
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@dataclass
|
|
7
|
+
class Validator:
|
|
8
|
+
""" Generic object for reusable validation flow and harden input handling. """
|
|
9
|
+
parser: Callable[[str], Any]
|
|
10
|
+
is_valid: Callable[[Any], Tuple[True, Any] | Tuple[False, str]]
|
|
11
|
+
error_message: str = "Invalid input"
|
|
12
|
+
max_attempts: Optional[int] = None
|
|
13
|
+
|
|
14
|
+
def prompt(self, prompt_text: str):
|
|
15
|
+
attempts = 0
|
|
16
|
+
|
|
17
|
+
while True:
|
|
18
|
+
if self.max_attempts is not None and attempts >= self.max_attempts:
|
|
19
|
+
raise ValueError("Maximum number of attempts reached.")
|
|
20
|
+
|
|
21
|
+
try:
|
|
22
|
+
raw = input(prompt_text)
|
|
23
|
+
except (EOFError, KeyboardInterrupt):
|
|
24
|
+
raise
|
|
25
|
+
|
|
26
|
+
attempts += 1
|
|
27
|
+
|
|
28
|
+
try:
|
|
29
|
+
value = self.parser(raw)
|
|
30
|
+
except (TypeError, ValueError, OverflowError):
|
|
31
|
+
print(self.error_message)
|
|
32
|
+
continue
|
|
33
|
+
|
|
34
|
+
valid, result = self.is_valid(value)
|
|
35
|
+
if valid:
|
|
36
|
+
return result
|
|
37
|
+
|
|
38
|
+
print(result if isinstance(result, str) else self.error_message)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
# ==================== STRING ====================
|
|
42
|
+
def get_str(prompt="Text: ", *, max_length=50, name=False, allow_empty=False):
|
|
43
|
+
"""
|
|
44
|
+
Asks the user for a string until a valid one is given.
|
|
45
|
+
|
|
46
|
+
If name=True, the input is normalized as a title-cased name and restricted
|
|
47
|
+
to letters and spaces. If allow_empty=True, blank values are accepted.
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
def parser(raw):
|
|
51
|
+
return " ".join(raw.split())
|
|
52
|
+
|
|
53
|
+
def is_valid(value):
|
|
54
|
+
value = " ".join(value.split())
|
|
55
|
+
|
|
56
|
+
if not value and not allow_empty:
|
|
57
|
+
return False, "Input cannot be empty. Try Again."
|
|
58
|
+
|
|
59
|
+
if len(value) > max_length:
|
|
60
|
+
return False, f"You exceeded the maximum length ({max_length}). Try Again."
|
|
61
|
+
|
|
62
|
+
if name:
|
|
63
|
+
normalized = value.title()
|
|
64
|
+
if _has_invalid_characters(normalized):
|
|
65
|
+
return False, "Invalid characters detected, use only letters and whitespaces. Try Again."
|
|
66
|
+
return True, normalized
|
|
67
|
+
|
|
68
|
+
return True, value
|
|
69
|
+
|
|
70
|
+
return Validator(parser=parser, is_valid=is_valid).prompt(prompt)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
# helper function
|
|
74
|
+
def _has_invalid_characters(text):
|
|
75
|
+
"""Check if text contains non-alphabetic characters (except spaces)."""
|
|
76
|
+
return any(char != " " and not char.isalpha() for char in text)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
# ==================== NUMBER (int or float) ====================
|
|
80
|
+
def get_num(prompt, *, min_val=None, max_val=None, floating=False):
|
|
81
|
+
"""
|
|
82
|
+
Asks the user for a number until a valid one is given.
|
|
83
|
+
It validates in the range:
|
|
84
|
+
(-Infinite - max_val) if min_val is None
|
|
85
|
+
(min_val - Infinite) if max_val is None
|
|
86
|
+
(min_val - max_val) if both are given
|
|
87
|
+
If floating is True, it accepts floating point numbers, otherwise only integers.
|
|
88
|
+
"""
|
|
89
|
+
|
|
90
|
+
def parser(raw):
|
|
91
|
+
return float(raw) if floating else int(raw)
|
|
92
|
+
|
|
93
|
+
def is_valid(value):
|
|
94
|
+
if floating and not math.isfinite(value):
|
|
95
|
+
return False, f"You didn't insert a valid {'float' if floating else 'integer'}. Try again."
|
|
96
|
+
|
|
97
|
+
if (min_val is not None and value < min_val) or (max_val is not None and value > max_val):
|
|
98
|
+
lower = min_val if min_val is not None else "-Infinite"
|
|
99
|
+
upper = max_val if max_val is not None else "Infinite"
|
|
100
|
+
return False, f"You inserted a number out of the valid range [{lower} - {upper}]"
|
|
101
|
+
|
|
102
|
+
return True, value
|
|
103
|
+
|
|
104
|
+
return Validator(
|
|
105
|
+
parser=parser,
|
|
106
|
+
is_valid=is_valid,
|
|
107
|
+
error_message=f"You didn't insert a valid {'float' if floating else 'integer'}. Try again.",
|
|
108
|
+
).prompt(prompt)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
# ==================== BOOLEAN LOGIC ====================
|
|
112
|
+
def yes_no(prompt):
|
|
113
|
+
"""Asks the user a yes/no question and returns True for yes and False for no."""
|
|
114
|
+
|
|
115
|
+
def parser(raw):
|
|
116
|
+
return raw.strip().lower()
|
|
117
|
+
|
|
118
|
+
def is_valid(value):
|
|
119
|
+
if value in ["y", "yes"]:
|
|
120
|
+
return True, True
|
|
121
|
+
if value in ["n", "no"]:
|
|
122
|
+
return True, False
|
|
123
|
+
return False, "Invalid input. Please enter 'y' or 'n'."
|
|
124
|
+
|
|
125
|
+
return Validator(
|
|
126
|
+
parser=parser,
|
|
127
|
+
is_valid=is_valid,
|
|
128
|
+
error_message="Invalid input. Please enter 'y' or 'n'."
|
|
129
|
+
).prompt(prompt + " (y/n): ")
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pyinput-cli
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A package for managing user input through the cli, and consistently across projects.
|
|
5
|
+
Author-email: Pedro Alberto Rosquete Ares <rosquetearespedro06@gmail.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2026 Pedro Alberto Rosquete Ares
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Documentation, https://github.com/prares-dev/pyinput-cli.git
|
|
29
|
+
Project-URL: Homepage, https://github.com/prares-dev/pyinput-cli.git
|
|
30
|
+
Project-URL: Repository, https://github.com/prares-dev/pyinput-cli.git
|
|
31
|
+
Requires-Python: >=3.9
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
License-File: LICENSE
|
|
34
|
+
Provides-Extra: dev
|
|
35
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
|
|
38
|
+
# pyinput-cli
|
|
39
|
+
**The definitive input handler for your python cli programs.**
|
|
40
|
+
|
|
41
|
+
pyinput-cli is a package that provides functions to handle user input in a consistent way across any project.
|
|
42
|
+
|
|
43
|
+
- **get_str**: reads a string and can validate its length or its format as a name.
|
|
44
|
+
- **get_num**: reads an integer or float and validates minimum and maximum values.
|
|
45
|
+
- **yes_no**: asks a yes/no question and returns the boolean result.
|
|
46
|
+
|
|
47
|
+
## Example usage
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
from pyinput import get_num, get_str, yes_no
|
|
51
|
+
|
|
52
|
+
name = get_str("Name: ", name=True, max_length=30)
|
|
53
|
+
age = get_num("Age: ", min_val=0, max_val=120)
|
|
54
|
+
continue_ = yes_no("Continue?")
|
|
55
|
+
|
|
56
|
+
print(name, age, continue_)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Notes
|
|
60
|
+
|
|
61
|
+
- Empty strings are rejected by default for `get_str`.
|
|
62
|
+
- `get_num` rejects invalid input and values outside the configured bounds.
|
|
63
|
+
- The functions are designed for small interactive CLI flows and can be reused in larger projects.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
pyinput/__init__.py
|
|
5
|
+
pyinput/pyinput.py
|
|
6
|
+
pyinput_cli.egg-info/PKG-INFO
|
|
7
|
+
pyinput_cli.egg-info/SOURCES.txt
|
|
8
|
+
pyinput_cli.egg-info/dependency_links.txt
|
|
9
|
+
pyinput_cli.egg-info/requires.txt
|
|
10
|
+
pyinput_cli.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pyinput
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pyinput-cli"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A package for managing user input through the cli, and consistently across projects."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
authors = [{name = "Pedro Alberto Rosquete Ares", email = "rosquetearespedro06@gmail.com"}]
|
|
12
|
+
license = { file = "LICENSE" }
|
|
13
|
+
dependencies = []
|
|
14
|
+
|
|
15
|
+
[project.urls]
|
|
16
|
+
Documentation = "https://github.com/prares-dev/pyinput-cli.git"
|
|
17
|
+
Homepage = "https://github.com/prares-dev/pyinput-cli.git"
|
|
18
|
+
Repository = "https://github.com/prares-dev/pyinput-cli.git"
|
|
19
|
+
|
|
20
|
+
[project.optional-dependencies]
|
|
21
|
+
dev = ["pytest>=8.0",]
|
|
22
|
+
|
|
23
|
+
[tool.setuptools.packages.find]
|
|
24
|
+
include = ["pyinput*"]
|
|
25
|
+
|
|
26
|
+
[tool.pytest.ini_options]
|
|
27
|
+
testpaths = ["tests"]
|
|
28
|
+
pythonpath = ['.']
|
|
29
|
+
addopts = "-ra"
|