- cloup version: 0.7
- Python version: 3.9
- Operating System: mac arm64
Description
Dear @janluke ,
I am currently working on a switchover for our library wetterdienst [1]. Therewith I'm currently moving our client accessor to click, however as we have certain mixing arguments for some functions I was happy to find that you are working on making this easier allowing to check for conditions. First of all thanks for your much needed work!
I'd like to add the following condition where the user can either supply rank or distance which in combination with provided longitude and latitude is used for geo filtering of some weather stations.
@cloup.option_group(
"Latitude-Longitude rank filtering",
cloup.option("--latitude", type=click.FLOAT),
cloup.option("--longitude", type=click.FLOAT),
cloup.option("--rank", type=click.INT),
cloup.option("--distance", type=click.FLOAT),
constraint=If(
IsSet("latitude") & IsSet("longitude"),
then=RequireExactly(3),
else_=cloup.constraints.accept_none),
)
However as far as it goes it would only give me a nice help text but when running the command with arguments it ends up with
Traceback (most recent call last):
File "/Users/benjamin/DEV/earth_observations/sources/wetterdienst/wetterdienst/ui/cli.py", line 736, in <module>
wetterdienst()
File "/Users/benjamin/DEV/earth_observations/sources/wetterdienst/.venv/lib/python3.9/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/Users/benjamin/DEV/earth_observations/sources/wetterdienst/.venv/lib/python3.9/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/Users/benjamin/DEV/earth_observations/sources/wetterdienst/.venv/lib/python3.9/site-packages/click/core.py", line 1257, in invoke
sub_ctx = cmd.make_context(cmd_name, args, parent=ctx)
File "/Users/benjamin/DEV/earth_observations/sources/wetterdienst/.venv/lib/python3.9/site-packages/click/core.py", line 700, in make_context
self.parse_args(ctx, args)
File "/Users/benjamin/DEV/earth_observations/sources/wetterdienst/.venv/lib/python3.9/site-packages/cloup/constraints/_support.py", line 104, in parse_args
constr.check_values(ctx)
File "/Users/benjamin/DEV/earth_observations/sources/wetterdienst/.venv/lib/python3.9/site-packages/cloup/constraints/_support.py", line 49, in check_values
self.constraint.check_values(self.params, ctx)
File "/Users/benjamin/DEV/earth_observations/sources/wetterdienst/.venv/lib/python3.9/site-packages/cloup/constraints/_conditional.py", line 49, in check_values
condition_is_true = condition(ctx)
File "/Users/benjamin/DEV/earth_observations/sources/wetterdienst/.venv/lib/python3.9/site-packages/cloup/constraints/conditions.py", line 104, in __call__
return all(c(ctx.params) for c in self.predicates)
File "/Users/benjamin/DEV/earth_observations/sources/wetterdienst/.venv/lib/python3.9/site-packages/cloup/constraints/conditions.py", line 104, in <genexpr>
return all(c(ctx.params) for c in self.predicates)
File "/Users/benjamin/DEV/earth_observations/sources/wetterdienst/.venv/lib/python3.9/site-packages/cloup/constraints/conditions.py", line 136, in __call__
if not isinstance(ctx.command, ConstraintMixin):
AttributeError: 'dict' object has no attribute 'command'
Is there a way to achieve what I'm planning with the current status of the library?
Again, thanks a lot for your work here! Much appreciated!
[1] https://github.com/earthobservations/wetterdienst
Edit
Got it working by making the first two arguments required like
@cloup.option_group(
"Latitude-Longitude rank filtering",
cloup.option("--latitude", type=click.FLOAT, required=True),
cloup.option("--longitude", type=click.FLOAT, required=True),
cloup.option("--rank", type=click.INT),
cloup.option("--distance", type=click.FLOAT),
constraint=RequireExactly(3)
)
Is this the recommend way of setting the constraints?