Skip to content

Commit 8d05c90

Browse files
committed
docs: improve and convert doc strings to markdown
1 parent e505e34 commit 8d05c90

File tree

3 files changed

+864
-50
lines changed

3 files changed

+864
-50
lines changed

args.py

Lines changed: 152 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,86 @@
1515

1616

1717
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
18-
__version__ = '2024033101'
18+
__version__ = '2025041901'
1919

2020

2121
def csv(arg):
22-
"""Returns a list from a `csv` input argument.
22+
"""
23+
Converts a CSV string into a list of values.
24+
25+
This function takes a comma-separated string (CSV format) and returns a list where each element
26+
corresponds to a value in the CSV string. Leading and trailing whitespace from each value is
27+
removed.
28+
29+
### Parameters
30+
- **arg** (`str`): A string containing values separated by commas (CSV format).
31+
32+
### Returns
33+
- **list**: A list of strings, each representing an element from the CSV input string.
34+
35+
### Example
36+
>>> csv("apple, orange, banana, grape")
37+
['apple', 'orange', 'banana', 'grape']
38+
39+
>>> csv(" one, two, three , four ")
40+
['one', 'two', 'three', 'four']
2341
"""
2442
return [x.strip() for x in arg.split(',')]
2543

2644

2745
def float_or_none(arg):
28-
"""Returns None or float from a `float_or_none` input argument.
46+
"""
47+
Converts an input to a float, or returns None if the input is 'none' or None.
48+
49+
This function attempts to convert the input argument into a float. If the input is `None` or
50+
the string 'none' (case insensitive), the function returns `None`. Otherwise, it returns the
51+
argument as a float.
52+
53+
### Parameters
54+
- **arg** (`str`, `None`, or `float`): The input value that will be converted to a float or
55+
returned as `None`.
56+
57+
### Returns
58+
- **float** or **None**: Returns the input as a float if it is convertible, or `None` if the
59+
input is 'none' or `None`.
60+
61+
### Example
62+
>>> float_or_none("123.45")
63+
123.45
64+
65+
>>> float_or_none("none")
66+
None
67+
68+
>>> float_or_none(None)
69+
None
2970
"""
3071
if arg is None or str(arg).lower() == 'none':
3172
return None
3273
return float(arg)
3374

3475

3576
def help(param):
36-
"""Return global valid help text for a parameter.
77+
"""
78+
Retrieves the help text for a given parameter.
79+
80+
This function returns the global help text associated with a specific parameter. It contains
81+
explanations for the valid options and usage of the parameter. If no help text is available
82+
for the parameter, it returns an empty string.
83+
84+
### Parameters
85+
- **param** (`str`): The parameter for which help text is to be retrieved. This must be a
86+
valid key in the predefined help dictionary.
87+
88+
### Returns
89+
- **str**: The help text for the given parameter, or an empty string if the parameter is not
90+
found.
91+
92+
### Example
93+
>>> help('--match')
94+
Lorem ipsum
95+
96+
>>> help('--nonexistent')
97+
''
3798
"""
3899
h = {
39100
'--match':
@@ -53,29 +114,76 @@ def help(param):
53114

54115

55116
def int_or_none(arg):
56-
"""Returns None or int from a `int_or_none` input argument.
117+
"""
118+
Converts a given argument to an integer or returns None.
119+
120+
This function checks if the argument is `None` or the string `'none'`, in which case it returns
121+
`None`. Otherwise, it attempts to convert the argument to an integer and returns the result.
122+
123+
### Parameters
124+
- **arg** (`str` or `None`): The input value to be converted to an integer, or `None`.
125+
126+
### Returns
127+
- **int** or **None**: The integer value of the argument if it can be converted, or `None` if
128+
the argument is `None` or `'none'`.
129+
130+
### Example
131+
>>> int_or_none('42')
132+
42
133+
134+
>>> int_or_none('none')
135+
None
136+
137+
>>> int_or_none(None)
138+
None
57139
"""
58140
if arg is None or str(arg).lower() == 'none':
59141
return None
60142
return int(arg)
61143

62144

63145
def number_unit_method(arg, unit='%', method='USED'):
64-
"""Expects '<number>[unit][method]. Useful for threshold arguments.
65-
Number is an integer or float.
66-
Unit is one of `%%|K|M|G|T|P`.
67-
If "unit" is omitted, `%` is assumed.
68-
`K` means `kibibyte` etc.
69-
Method is one of `USED|FREE`.
70-
If "method" is ommitted, `USED` is assumed.
71-
Examples: '
72-
* `95`: returns (95, '%', 'USED')
73-
* `9.5M`: returns (9.5, 'M', 'USED')
74-
* `95%USED`: returns (95, '%', 'USED')
75-
* `5FREE`: : returns (5, '%', 'FREE')
76-
* `5%FREE`: : returns (5, '%', 'FREE')
77-
* `9.5GFREE`: returns (9.5, 'G', 'FREE')
78-
* `1400GUSED`: returns (1400, 'G', 'USED')
146+
"""
147+
Parses a string representing a number with an optional unit and method, and returns the
148+
corresponding components.
149+
150+
This function expects an input string in the format `<number>[unit][method]`, typically
151+
used for threshold arguments. The function extracts and returns the numeric value, unit
152+
(defaults to `%`), and method (defaults to `USED`). The function supports various units
153+
such as `K`, `M`, `G`, `T`, `P`, and `%`, and methods like `USED` and `FREE`.
154+
155+
### Parameters
156+
- **arg** (`str`): The input string representing the number, unit, and method.
157+
- **unit** (`str`, optional): The unit of measurement, one of `%%|K|M|G|T|P`. Defaults to `%`.
158+
- **method** (`str`, optional): The method used, one of `USED|FREE`. Defaults to `USED`.
159+
160+
### Returns
161+
- **tuple**: A tuple containing:
162+
- **float**: The numeric value.
163+
- **str**: The unit (defaults to `%` if not specified).
164+
- **str**: The method (defaults to `USED` if not specified).
165+
166+
### Example
167+
>>> number_unit_method('95')
168+
(95.0, '%', 'USED')
169+
170+
>>> number_unit_method('9.5M')
171+
(9.5, 'M', 'USED')
172+
173+
>>> number_unit_method('95%USED')
174+
(95.0, '%', 'USED')
175+
176+
>>> number_unit_method('5FREE')
177+
(5.0, '%', 'FREE')
178+
179+
>>> number_unit_method('5%FREE')
180+
(5.0, '%', 'FREE')
181+
182+
>>> number_unit_method('9.5GFREE')
183+
(9.5, 'G', 'FREE')
184+
185+
>>> number_unit_method('1400GUSED')
186+
(1400.0, 'G', 'USED')
79187
"""
80188
# use named groups in regex
81189
regex = re.compile(
@@ -93,13 +201,35 @@ def number_unit_method(arg, unit='%', method='USED'):
93201

94202

95203
def range_or_none(arg):
96-
"""Returns None or range from a `range_or_none` input argument.
204+
"""
205+
See str_or_none()
97206
"""
98207
return str_or_none(arg)
99208

100209

101210
def str_or_none(arg):
102-
"""Returns None or str from a `str_or_none` input argument.
211+
"""
212+
Converts an input argument into a string or returns `None`.
213+
214+
This function checks if the input is `None` or the string `"none"` (case-insensitive) and
215+
returns `None` in those cases. Otherwise, it returns the input as a string.
216+
217+
### Parameters
218+
- **arg** (`any`): The input argument that can be any type.
219+
220+
### Returns
221+
- **str** or **None**: If the input is not `None` or `"none"`, it returns the input as a
222+
string; otherwise, it returns `None`.
223+
224+
### Example
225+
>>> str_or_none(123)
226+
'123'
227+
228+
>>> str_or_none('none')
229+
None
230+
231+
>>> str_or_none(None)
232+
None
103233
"""
104234
if arg is None or str(arg).lower() == 'none':
105235
return None

0 commit comments

Comments
 (0)