Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Would it be possible to support arguments in addtion to options? #26

Open
sdstewar opened this issue Feb 4, 2021 · 5 comments
Open
Labels
enhancement New feature or request help wanted Extra attention is needed question Further information is requested

Comments

@sdstewar
Copy link

sdstewar commented Feb 4, 2021

I have a use case where I'd like to accept a list of items contained in a file given by -f or an arbitrary number of positional arguments. Unfortunately Click only allows this on arguments rather than options. So would it be possible to support a mix of mutually exclusive options and arguments?

@espdev
Copy link
Member

espdev commented Feb 5, 2021

@sdstewar

click-option-group only works with options. It is currently not possible to mix mutually exclusive options and arguments inside an option group. It's hard to say right now if this feature is possible at all. I need to experiment with this.

@espdev espdev added the question Further information is requested label Feb 5, 2021
@sdstewar
Copy link
Author

sdstewar commented Feb 5, 2021

That's what I figured but was hoping it'd be "Oh yeah, that's easy!" :)

Our internal dev has standardized on Click and I'm working updating some older tooling. I'll look for an alternative setup but think this would still be a nice feature to have.

BTW, loving the your package so far.

@espdev
Copy link
Member

espdev commented Feb 6, 2021

... it'd be "Oh yeah, that's easy!" :)

Things get complicated if we try to do something non-standard in Click. :)

I'll look for an alternative setup

An arbitrary number of positional arguments can be replaced to option with multiple=True flag. In this case we need to repeat an option name again and again:

-i item1 -i item2 -i item3 ... -i itemN

Yes, I know, it looks verbose, but this is the only way to set an arbitrary number of option values in Click except maybe this (with additional parsing):

-i "item1 item2 item3 ... itemN"
-i item1,item2,item3,itemN

@espdev espdev added the enhancement New feature or request label Feb 6, 2021
@LA-Toth
Copy link

LA-Toth commented Jan 4, 2022

I created an almost working version for it based on latest pip package.
optgrp-arg.diff.txt

Checking with MutuallyExclusiveGroup it's clear that there are missing parts.
I added an option and an argument. If I specify the argument in the command-line, it's fine. But if I specify only the option, the group thinks that the argument is also specified.

@espdev espdev added the help wanted Extra attention is needed label Jul 16, 2022
@janluke
Copy link

janluke commented Aug 21, 2022

I think the 2nd solution suggested by @espdev is pretty reasonable. Otherwise, [shameless plug] you may consider using Cloup (a set of click extensions):

from pathlib import Path
import cloup
from cloup.constraints import mutually_exclusive

@cloup.command()
@cloup.argument("items", nargs=-1, required=False)
@cloup.option("-f", "--file-path", type=cloup.file_path(exists=True))
@cloup.constraint(mutually_exclusive, ["items", "file_path"])
def cmd(items: Tuple[str], file_path: Path):
    ...

If at least one of the parameters has to be provided, you should use the require_any constraint instead. See here for more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants