[QUESTION] How to automatically validate input parameter as IPv4Address #1310
-
First check
DescriptionWith parser.add_argument(
"--host",
type=IPv4Address,
default=IPv4Address("127.0.0.1"),
)This will automatically try to construct a I didn't see this type documented in |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments
-
|
Perhaps this should be a feature request. @app.command()
def main(
host: IPv4Address = typer.Option(
IPv4Address("127.0.0.1"), help="Host interface to listen on."
),
) -> None:
passOutput when run: |
Beta Was this translation helpful? Give feedback.
-
|
In the meantime, you can get use the @app.command()
def main(host: stre= typer.Option("127.0.0.1", help="Host interface to listen on.", callback=IPv4Address)) -> None:
pass |
Beta Was this translation helpful? Give feedback.
-
|
@daddycocoaman Thanks, this is close to what I was looking for. The |
Beta Was this translation helpful? Give feedback.
-
|
+1, I need this feature in 99% of cli projects. |
Beta Was this translation helpful? Give feedback.
-
|
@johnthagen , unfortunately, click (basic library) seem to not support IPv4Address type. So guess we need to request this type on click's issue tracker. p.s. |
Beta Was this translation helpful? Give feedback.
-
|
Looks like creating new Click types isn't that difficult either, although
it seems that the Click team isn't always open integrating new types. This
guy tried to get Date merged into Click and it was rejected.
pallets/click#1655
It might be worth creating an additional package for extra click types that
Typer can import,
or building on the existing project for additional parameters.
https://github.com/click-contrib/click_params
…On Wed, Mar 3, 2021, 7:29 AM baterflyrity ***@***.***> wrote:
@johnthagen <https://github.com/johnthagen> , unfortunately, click (basic
library) seem not to support IPv4Address type. So guess we need to request
this type on click's issue tracker.
p.s.
As you can see
<https://github.com/tiangolo/typer/blob/c3a4c72a4f1a18b7f16addb508862ec63411fa63/typer/main.py#L503>
types are just mapped to underlying library ones.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#239 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AFBVEI6JLLRUE4IJEZPET3TTBYTQ3ANCNFSM4YEPGLKQ>
.
|
Beta Was this translation helpful? Give feedback.
-
|
You can make from ipaddress import IPv4Address
import typer
from typing_extensions import Annotated
def main(
host: Annotated[
IPv4Address,
typer.Argument(
parser=IPv4Address,
default_factory=lambda: IPv4Address("127.0.0.1"),
help="Host address (IP v4)",
show_default="127.0.0.1",
),
],
):
print(f"host is {host}")
if __name__ == "__main__":
typer.run(main)Docs: https://typer.tiangolo.com/tutorial/parameter-types/custom-types/ |
Beta Was this translation helpful? Give feedback.
You can make
IPv4Address(and other custom types) working by specifyingparserparameter:Docs: https://typer.tiangolo.com/tutorial/parameter-types/custom-types/