You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+142-1Lines changed: 142 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -666,7 +666,7 @@ from tap import tapify
666
666
classSquarer:
667
667
"""Squarer with a number to square.
668
668
669
-
:param num: The number to square.
669
+
:param num: The number to square.
670
670
"""
671
671
num: float
672
672
@@ -681,6 +681,94 @@ if __name__ == '__main__':
681
681
682
682
Running `python square_dataclass.py --num -1` prints `The square of your number is 1.0.`.
683
683
684
+
<details>
685
+
<summary>Argument descriptions</summary>
686
+
687
+
For dataclasses, the argument's description (which is displayed in the `-h` help message) can either be specified in the
688
+
class docstring or the field's description in `metadata`. If both are specified, the description from the docstring is
689
+
used. In the example below, the description is provided in `metadata`.
690
+
691
+
```python
692
+
# square_dataclass.py
693
+
from dataclasses import dataclass, field
694
+
695
+
from tap import tapify
696
+
697
+
@dataclass
698
+
classSquarer:
699
+
"""Squarer with a number to square.
700
+
"""
701
+
num: float= field(metadata={"description": "The number to square."})
702
+
703
+
defget_square(self) -> float:
704
+
"""Get the square of the number."""
705
+
returnself.num **2
706
+
707
+
if__name__=='__main__':
708
+
squarer = tapify(Squarer)
709
+
print(f'The square of your number is {squarer.get_square()}.')
710
+
```
711
+
712
+
</details>
713
+
714
+
#### Pydantic
715
+
716
+
Pydantic [Models](https://docs.pydantic.dev/latest/concepts/models/) and
717
+
[dataclasses](https://docs.pydantic.dev/latest/concepts/dataclasses/) can be `tapify`d.
718
+
719
+
```python
720
+
# square_pydantic.py
721
+
from pydantic import BaseModel, Field
722
+
723
+
from tap import tapify
724
+
725
+
classSquarer(BaseModel):
726
+
"""Squarer with a number to square.
727
+
"""
728
+
num: float= Field(description="The number to square.")
729
+
730
+
defget_square(self) -> float:
731
+
"""Get the square of the number."""
732
+
returnself.num **2
733
+
734
+
if__name__=='__main__':
735
+
squarer = tapify(Squarer)
736
+
print(f'The square of your number is {squarer.get_square()}.')
737
+
```
738
+
739
+
<details>
740
+
<summary>Argument descriptions</summary>
741
+
742
+
For Pydantic v2 models and dataclasses, the argument's description (which is displayed in the `-h` help message) can
743
+
either be specified in the class docstring or the field's `description`. If both are specified, the description from the
744
+
docstring is used. In the example below, the description is provided in the docstring.
745
+
746
+
For Pydantic v1 models and dataclasses, the argument's description must be provided in the class docstring:
747
+
748
+
```python
749
+
# square_pydantic.py
750
+
from pydantic import BaseModel
751
+
752
+
from tap import tapify
753
+
754
+
classSquarer(BaseModel):
755
+
"""Squarer with a number to square.
756
+
757
+
:param num: The number to square.
758
+
"""
759
+
num: float
760
+
761
+
defget_square(self) -> float:
762
+
"""Get the square of the number."""
763
+
returnself.num **2
764
+
765
+
if__name__=='__main__':
766
+
squarer = tapify(Squarer)
767
+
print(f'The square of your number is {squarer.get_square()}.')
768
+
```
769
+
770
+
</details>
771
+
684
772
### tapify help
685
773
686
774
The help string on the command line is set based on the docstring for the function or class. For example, running `python square_function.py -h` will print:
@@ -751,4 +839,57 @@ if __name__ == '__main__':
751
839
Running `python person.py --name Jesse --age 1` prints `My name is Jesse.` followed by `My age is 1.`. Without `known_only=True`, the `tapify` calls would raise an error due to the extra argument.
752
840
753
841
### Explicit boolean arguments
842
+
754
843
Tapify supports explicit specification of boolean arguments (see [bool](#bool) for more details). By default, `explicit_bool=False` and it can be set with `tapify(..., explicit_bool=True)`.
844
+
845
+
## Convert to a `Tap` class
846
+
847
+
`to_tap_class` turns a function or class into a `Tap` class. The returned class can be [subclassed](#subclassing) to add
848
+
special argument behavior. For example, you can override [`configure`](#configuring-arguments) and
849
+
[`process_args`](#argument-processing).
850
+
851
+
If the object can be `tapify`d, then it can be `to_tap_class`d, and vice-versa. `to_tap_class` provides full control
852
+
over argument parsing.
853
+
854
+
### Examples
855
+
856
+
#### Simple
857
+
858
+
```python
859
+
# main.py
860
+
"""
861
+
My script description
862
+
"""
863
+
864
+
from pydantic import BaseModel
865
+
866
+
from tap import to_tap_class
867
+
868
+
classProject(BaseModel):
869
+
package: str
870
+
is_cool: bool=True
871
+
stars: int=5
872
+
873
+
if__name__=="__main__":
874
+
ProjectTap = to_tap_class(Project)
875
+
tap = ProjectTap(description=__doc__) # from the top of this script
0 commit comments