Replies: 12 comments
-
|
I was struggling with same problem here. I'll post how I solved my problem, this could be useful for others. Once I tried to define option type as To workaround this situation I had to use I had to use this other pattern: At the end, my code looks like: # Since there is a `_parse_option`
def _parse_option(value):
result = defaultdict(list)
for value in values:
k, v = value.split('=')
result[k.strip()].append(v.strip())
return result.items()
@app.command
def my_command(opts: List[str] = typer.Option(..., callback=_parse_option):
print(opts)
# [('key1', ['value1']), ('key2', ['value2'])]Note 1: Note 2: I'm using |
Beta Was this translation helpful? Give feedback.
-
|
Another, possibly naive way of handling this would be to use the Click Context and a bit of custom parsing, though as mentioned in my own question, I don't know how to add that to the help output. This would look something like: def _parse_extras(extras: List[str]):
_extras = extras[:]
extra_options = {
'complex_option1': [],
'complex_option2': [],
}
while len(_extras) > 0:
if _extras[0] == '--complex-option1' and len(_extras) > 2:
complex_values = _extras[1:2]
_extras = _extras[3:]
extra_options['complex_option1'].append(_parse_opt1(*complex_values))
elif _extras[0] == '--complex-option2' and len(_extras) > 3:
complex_values = _extras[1:3]
_extras = _extras[4:]
extra_options['complex_option2'].append(_parse_opt2(*complex_values))
else:
raise click.NoSuchOption(_extras[0])
return extra_options
@app.command(context_settings={"allow_extra_args": True, "ignore_unknown_options": True})
def my_command(ctx: typer.Context, other_normal_opts):
extra_options = _parse_extras(ctx.args)
complex_option1 = extra_options['complex_option1']
complex_option2 = extra_options['complex_option2']
print(dir()) |
Beta Was this translation helpful? Give feedback.
-
|
It was pretty simple to get this working locally for something simple like |
Beta Was this translation helpful? Give feedback.
-
|
@noctuid Hi! I think i have a simple usecase in my project - |
Beta Was this translation helpful? Give feedback.
-
|
I changed the Typer code itself (see WIP Support specifying options as a list of tuples). I'm not sure it will work with |
Beta Was this translation helpful? Give feedback.
-
|
@noctuid thanks for sharing this, hope it will be merged at some point! I re-thinked my approach - tuple with optional second argument is ambiguous indeed. For now it's working for me with using |
Beta Was this translation helpful? Give feedback.
-
|
This minor fix to make @jonatasleon's worthy workaround to support also values containing the k, v = value.split('=', 1) |
Beta Was this translation helpful? Give feedback.
-
|
Using @app.command()
def foo(pairs: list[click.Tuple] = typer.Option(click_type=click.Tuple([str, str]))):
for x, y in pairs:
print(f'x: {x}, y: {y}') |
Beta Was this translation helpful? Give feedback.
-
What version of typer are you using @alex-janss? I'm getting |
Beta Was this translation helpful? Give feedback.
-
0.9.0 |
Beta Was this translation helpful? Give feedback.
-
This workaround works, but still it's a pitty that it does not allow type hinting to guess the type of the variable, thought. With this example both |
Beta Was this translation helpful? Give feedback.
-
I found the following workaround, with no sign of type errors by PyCharm linter / type checker. I did not check with def parse_colon_separated_pair(value: str):
return tuple(value.split(sep=':', maxsplit=2))
@app.command()
def foo(
pairs: Annotated[
Optional[List[click.Tuple]],
typer.Option(
metavar="KEY:VALUE",
parser=parse_colon_separated_pair,
] = None,
):
print(f"{type(pairs)=}, {pairs=}")
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
I'm converting a small Click app to Typer and hit the following issue; I can't seem to get Tuples as Multiple Multi Value Options working.
I would like to achieve the following:
This is achieved in Click with the following option:
Operating System
Linux
Operating System Details
No response
Typer Version
0.4.1
Python Version
3.7.3
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions