Skip to content

Commit ccda769

Browse files
committed
Renaming args in tapify
1 parent 00beb16 commit ccda769

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

tap/tapify.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111

1212

1313
def tapify(class_or_function: Union[Callable[[InputType], OutputType], OutputType],
14-
args: Optional[List[str]] = None,
1514
known_only: bool = False,
15+
command_line_args: Optional[List[str]] = None,
1616
**func_kwargs) -> OutputType:
1717
"""Tapify initializes a class or runs a function by parsing arguments from the command line.
1818
1919
:param class_or_function: The class or function to run with the provided arguments.
20-
:param args: Arguments to parse. If None, arguments are parsed from the command line.
2120
:param known_only: If true, ignores extra arguments and only parses known arguments.
21+
:param command_line_args: A list of command line style arguments to parse (e.g., ['--arg', 'value']).
22+
If None, arguments are parsed from the command line (default behavior).
2223
:param func_kwargs: Additional keyword arguments for the function. These act as default values when
2324
parsing the command line arguments and overwrite the function defaults but
2425
are overwritten by the parsed command line arguments.
@@ -38,7 +39,7 @@ def tapify(class_or_function: Union[Callable[[InputType], OutputType], OutputTyp
3839
# Get the description of each argument in the class init or function
3940
param_to_description = {param.arg_name: param.description for param in docstring.params}
4041

41-
# Create a Tap object
42+
# Create a Tap object with a description from the docstring of the function or class
4243
tap = Tap(description='\n'.join(filter(None, (docstring.short_description, docstring.long_description))))
4344

4445
# Add arguments of class init or function to the Tap object
@@ -75,10 +76,10 @@ def tapify(class_or_function: Union[Callable[[InputType], OutputType], OutputTyp
7576
raise ValueError(f'Unknown keyword arguments: {func_kwargs}')
7677

7778
# Parse command line arguments
78-
args = tap.parse_args(
79-
args=args,
79+
command_line_args = tap.parse_args(
80+
args=command_line_args,
8081
known_only=known_only
8182
)
8283

8384
# Initialize the class or run the function with the parsed arguments
84-
return class_or_function(**args.as_dict())
85+
return class_or_function(**command_line_args.as_dict())

tests/test_tapify.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def __eq__(self, other: float) -> bool:
5050
return other == pie()
5151

5252
for class_or_function in [pie, Pie, PieDataclass]:
53-
self.assertEqual(tapify(class_or_function, args=[]), 3.14)
53+
self.assertEqual(tapify(class_or_function, command_line_args=[]), 3.14)
5454

5555
def test_tapify_simple_types(self):
5656
def concat(a: int, simple: str, test: float, of: float, types: bool) -> str:
@@ -75,7 +75,7 @@ def __eq__(self, other: str) -> bool:
7575
return other == concat(self.a, self.simple, self.test, self.of, self.types)
7676

7777
for class_or_function in [concat, Concat, ConcatDataclass]:
78-
output = tapify(class_or_function, args=[
78+
output = tapify(class_or_function, command_line_args=[
7979
'--a', '1',
8080
'--simple', 'simple',
8181
'--test', '3.14',
@@ -109,7 +109,7 @@ def __eq__(self, other: str) -> bool:
109109
return other == concat(self.a, self.simple, self.test, self.of, self.types, self.wow)
110110

111111
for class_or_function in [concat, Concat, ConcatDataclass]:
112-
output = tapify(class_or_function, args=[
112+
output = tapify(class_or_function, command_line_args=[
113113
'--a', '1',
114114
'--simple', 'simple',
115115
'--test', '3.14',
@@ -140,7 +140,7 @@ def __eq__(self, other: str) -> bool:
140140
return other == concat(self.complexity, self.requires, self.intelligence)
141141

142142
for class_or_function in [concat, Concat, ConcatDataclass]:
143-
output = tapify(class_or_function, args=[
143+
output = tapify(class_or_function, command_line_args=[
144144
'--complexity', 'complex', 'things', 'require',
145145
'--requires', '1', '0',
146146
'--intelligence', 'jesse',
@@ -171,7 +171,7 @@ def __eq__(self, other: str) -> bool:
171171
return other == concat(self.complexity, self.requires, self.intelligence)
172172

173173
for class_or_function in [concat, Concat, ConcatDataclass]:
174-
output = tapify(class_or_function, args=[
174+
output = tapify(class_or_function, command_line_args=[
175175
'--complexity', '1', '2', '3',
176176
'--requires', '1', '0',
177177
'--intelligence', 'jesse',
@@ -211,7 +211,7 @@ def __eq__(self, other: str) -> bool:
211211
return other == concat(self.complexity, self.requires, self.intelligence, self.maybe, self.possibly)
212212

213213
for class_or_function in [concat, Concat, ConcatDataclass]:
214-
output = tapify(class_or_function, args=[
214+
output = tapify(class_or_function, command_line_args=[
215215
'--complexity', 'complex', 'things', 'require',
216216
'--requires', '-3', '12',
217217
'--possibly', 'huh?'
@@ -241,7 +241,7 @@ def __eq__(self, other: str) -> bool:
241241

242242
for class_or_function in [concat, Concat, ConcatDataclass]:
243243
with self.assertRaises(SystemExit):
244-
tapify(class_or_function, args=[
244+
tapify(class_or_function, command_line_args=[
245245
'--so', '23',
246246
'--many', '9.3'
247247
])
@@ -267,7 +267,7 @@ def __eq__(self, other: str) -> bool:
267267

268268
for class_or_function in [concat, Concat, ConcatDataclass]:
269269
with self.assertRaises(SystemExit):
270-
tapify(class_or_function, args=[
270+
tapify(class_or_function, command_line_args=[
271271
'--so', '23',
272272
'--few', '9.3',
273273
'--args', 'wow'
@@ -293,7 +293,7 @@ def __eq__(self, other: str) -> bool:
293293
return other == concat(self.so, self.few)
294294

295295
for class_or_function in [concat, Concat, ConcatDataclass]:
296-
output = tapify(class_or_function, args=[
296+
output = tapify(class_or_function, command_line_args=[
297297
'--so', '23',
298298
'--few', '9.3',
299299
'--args', 'wow'
@@ -325,7 +325,7 @@ def __eq__(self, other: str) -> bool:
325325
return other == concat(self.i, self.like, self.k, self.w, self.args, self.always)
326326

327327
for class_or_function in [concat, Concat, ConcatDataclass]:
328-
output = tapify(class_or_function, args=[
328+
output = tapify(class_or_function, command_line_args=[
329329
'--i', '23',
330330
'--args', 'wow',
331331
'--like', '3.03',
@@ -358,7 +358,7 @@ def __eq__(self, other: str) -> bool:
358358

359359
for class_or_function in [concat, Concat, ConcatDataclass]:
360360
with self.assertRaises(ValueError):
361-
tapify(class_or_function, args=[
361+
tapify(class_or_function, command_line_args=[
362362
'--i', '23',
363363
'--args', 'wow',
364364
'--like', '3.03',
@@ -383,12 +383,12 @@ def __eq__(self, other: str) -> bool:
383383
return other == concat(self.problems)
384384

385385
for class_or_function in [concat, Concat, ConcatDataclass]:
386-
output = tapify(class_or_function, args=[], problems=Problems('oh', 'no!'))
386+
output = tapify(class_or_function, command_line_args=[], problems=Problems('oh', 'no!'))
387387

388388
self.assertEqual(output, 'Problems(oh, no!)')
389389

390390
with self.assertRaises(SystemExit):
391-
tapify(class_or_function, args=['--problems', '1', '2'])
391+
tapify(class_or_function, command_line_args=['--problems', '1', '2'])
392392

393393
def test_tapify_untyped(self):
394394
def concat(untyped_1, typed_1: int,
@@ -422,7 +422,7 @@ def __eq__(self, other: str) -> bool:
422422
self.untyped_3, self.typed_3)
423423

424424
for class_or_function in [concat, Concat, ConcatDataclass]:
425-
output = tapify(class_or_function, args=[
425+
output = tapify(class_or_function, command_line_args=[
426426
'--untyped_1', 'why not type?',
427427
'--typed_1', '1',
428428
'--typed_2', 'typing is great!',
@@ -458,8 +458,8 @@ def __eq__(self, other: str) -> bool:
458458
return other == concat(self.a, self.b, self.c)
459459

460460
for class_or_function in [concat, Concat, ConcatDataclass]:
461-
output_1 = tapify(class_or_function, args=['--a', '1', '--b', '2', '--c', '3'])
462-
output_2 = tapify(class_or_function, args=['--a', '4', '--b', '5', '--c', '6'])
461+
output_1 = tapify(class_or_function, command_line_args=['--a', '1', '--b', '2', '--c', '3'])
462+
output_2 = tapify(class_or_function, command_line_args=['--a', '4', '--b', '5', '--c', '6'])
463463

464464
self.assertEqual(output_1, '1 2 3')
465465
self.assertEqual(output_2, '4 5 6')
@@ -480,7 +480,7 @@ def __eq__(self, other: str) -> bool:
480480

481481
for class_or_function in [concat, Concat]:
482482
with self.assertRaises(SystemExit):
483-
tapify(class_or_function, args=['--a', '1', '--b', '2'])
483+
tapify(class_or_function, command_line_args=['--a', '1', '--b', '2'])
484484

485485
def test_tapify_help(self):
486486
def concat(a: int, b: int, c: int) -> str:
@@ -524,7 +524,7 @@ def __eq__(self, other: str) -> bool:
524524
f = io.StringIO()
525525
with contextlib.redirect_stdout(f):
526526
with self.assertRaises(SystemExit):
527-
tapify(class_or_function, args=['-h'])
527+
tapify(class_or_function, command_line_args=['-h'])
528528

529529
self.assertIn('Concatenate three numbers.', f.getvalue())
530530
self.assertIn('--a A (int, required) The first number.', f.getvalue())

0 commit comments

Comments
 (0)