[FEATURE] Pattern matching on Argument #1577
Unanswered
fproulx-boostsecurity
asked this question in
Questions
Replies: 3 comments
-
|
You can accomplish this with a callback. import re
import typer
app = typer.Typer()
def regex_match(value):
regex = r"^fan"
if re.match(regex, value):
return value
raise typer.BadParameter(f"Does not match the regex: {regex}")
@app.command()
def print_word(word: str = typer.Argument(..., callback=regex_match)):
typer.echo(f"Your valid string is: {word}")
if __name__ == "__main__":
app() |
Beta Was this translation helpful? Give feedback.
0 replies
-
Additionally, you could nest a callback function in another function, so you only need one for arbitrary regex: import re
import typer
app = typer.Typer()
def regex_match(regex):
def callback(value):
if re.match(regex, value):
return value
raise typer.BadParameter(f"Does not match {regex}")
return callback
@app.command()
def print_word(word: str = typer.Argument(
...,
callback=regex_match(r"^fan")
)):
typer.echo(f"Your valid string is: {word}")
if __name__ == "__main__":
app() |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
This way (implementation with callback) it will not be shown in the help. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
The solution you would like
I would like to specify a pattern (regex) to limit the valid patterns for an argument
Beta Was this translation helpful? Give feedback.
All reactions