Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
khud committed Dec 22, 2020
1 parent 11d71ac commit 2abd590
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,34 @@ def main(numbers: ty.List[int]):
"""
...
```

## Custom types

If you want to parse arguments to your own types, you can do that
in the following manner:

```python
import typedparse
import typing as ty

def to_int(s: str) -> int:
if s.startswith("0x"):
return int(s, 16)
elif s.startswith("0"):
return int(s, 8)

return int(s, 10)

@typedparse.options(test={
"type": to_int
})
def main(test: ty.Optional[int] = 0):
"""Test
Args:
test: a test
"""
...
```
In this example, we use a custom function to convert string arguments to integers,
which supports hexadecimal and octal representations.

0 comments on commit 2abd590

Please sign in to comment.