Skip to content

Commit c5359c3

Browse files
authored
Merge pull request #76 from wj-Mcat/main
Make the __doc__ the description for a Tap object by default.
2 parents d57d925 + 984fddc commit c5359c3

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

tap/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
__all__ = ['__version__']
22

33
# major, minor, patch
4-
version_info = 1, 7, 2
4+
version_info = 1, 7, 3
55

66
# Nice string for the version
77
__version__ = '.'.join(map(str, version_info))

tap/tap.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ def __init__(self,
110110
# Indicate that initialization is complete
111111
self._initialized = True
112112

113+
self.description = self.__doc__ or ''
114+
113115
def _add_argument(self, *name_or_flags, **kwargs) -> None:
114116
"""Adds an argument to self (i.e. the super class ArgumentParser).
115117
@@ -311,6 +313,9 @@ def process_args(self) -> None:
311313

312314
def add_subparser(self, flag: str, subparser_type: type, **kwargs) -> None:
313315
"""Add a subparser to the collection of subparsers"""
316+
help_desc = kwargs.get('help', subparser_type.__doc__)
317+
kwargs['help'] = help_desc
318+
314319
self._subparser_buffer.append((flag, subparser_type, kwargs))
315320

316321
def _add_subparsers(self) -> None:

tests/test_integration.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
from pathlib import Path
55
import pickle
6+
from re import L
67
import sys
78
from tempfile import TemporaryDirectory
89
from typing import Any, Iterable, List, Optional, Set, Tuple, Union
@@ -1561,5 +1562,33 @@ def test_pickle(self):
15611562
self.assertEqual(loaded_args.as_dict(), args.as_dict())
15621563

15631564

1565+
class TestParserDescription(TestCase):
1566+
def test_root_parser_description(self):
1567+
class RootParser(Tap):
1568+
"""<Root Parser>"""
1569+
field: str = '1'
1570+
1571+
root_parser = RootParser()
1572+
help_info = root_parser.format_help()
1573+
self.assertIn('<Root Parser>', help_info)
1574+
1575+
def test_sub_parser_description(self):
1576+
1577+
class SubParser(Tap):
1578+
"""<Sub Parser>"""
1579+
sub_field: str = '2'
1580+
1581+
class RootParser(Tap):
1582+
"""<Root Parser>"""
1583+
field: str = '1'
1584+
1585+
def configure(self):
1586+
self.add_subparsers(help='All sub parser')
1587+
self.add_subparser('sub', SubParser)
1588+
1589+
help_desc = RootParser().format_help()
1590+
self.assertIn('<Sub Parser>', help_desc)
1591+
self.assertIn('<Root Parser>', help_desc)
1592+
15641593
if __name__ == '__main__':
15651594
unittest.main()

0 commit comments

Comments
 (0)