click-contrib/click-option-group

RequiredAny misses an option even if it's given

JOJ0 opened this issue · 5 comments

JOJ0 commented

Hi,
this might not be an issue but layer 8 problem ;-) I can't spot my error....

My option group:

@media.command(name="list")
@click_option_group.optgroup.group(
    "List media by",
    cls=click_option_group.RequiredAnyOptionGroup,
    help="")
@click_option_group.optgroup.option(
    "--room-id", "-r", type=str,
    help="""list all media in room with this room ID ('!abcdefg').""")
@click_option_group.optgroup.option(
    "--user-id", "-u", type=str,
    help="""list all media uploaded by user with this matrix ID
    (@user:server).""")
@click.option(
    "--from", "-f", "from_", type=int, default=0, show_default=True,
    help="""offset media listing by given number. This option is also used for
    pagination but only supported together with --user-id.""")

Error when invoking command:

jojo@jum ~/git/synadm (media) $ synadm media list -r xx
Usage: synadm media list [OPTIONS]
Try "synadm media list -h" for help.

Error: Missing one of the required options from "List media by" option group:
  "--room-id" / "-r"
  "--user-id" / "-u"
jojo@jum ~/git/synadm (media) $

The actual command function:

@click.pass_obj
@click.pass_context
def media_list_cmd(ctx, helper, room_id, user_id, from_, limit, sort, reverse):
    """ list local media by room or user
    """
    if room_id:
        media_list = helper.api.room_media_list(room_id)
        if media_list is None:
            click.echo("Media list could not be fetched.")
            raise SystemExit(1)
        helper.output(media_list)
    elif user_id:
        #media_list = helper.api.user_media_list(user_id)
        from synadm.cli import user
        ctx.invoke(user.get_function("user_media_cmd"), user_id=user_id,
                   from_=from_, limit=limit, sort=sort, reverse=reverse)
JOJ0 commented

Giving the -u xx option does work though!

Hi @JOJ0,

I'm trying to reproduce the behavior and I cannot reproduce it.

My code example:

import click
from click_option_group import optgroup, RequiredAnyOptionGroup

@click.group()
def synadm():
    pass

@synadm.group()
def media():
    pass

@media.command(name="list")
@optgroup.group("List media by", cls=RequiredAnyOptionGroup, help="")
@optgroup.option(
    "--room-id", "-r", type=str,
    help="""list all media in room with this room ID ('!abcdefg').""")
@optgroup.option(
    "--user-id", "-u", type=str,
    help="""list all media uploaded by user with this matrix ID
    (@user:server).""")
@click.option(
    "--from", "-f", "from_", type=int, default=0, show_default=True,
    help="""offset media listing by given number. This option is also used for
    pagination but only supported together with --user-id.""")
@click.pass_obj
@click.pass_context
def media_list_cmd(ctx, helper, room_id, user_id, from_):
    print(room_id, user_id, from_)

if __name__ == '__main__':
    synadm()
$ scratch_80.py media list -r xx
xx None 0

$ scratch_80.py media list -u yy
None yy 0

$ scratch_80.py media list
Usage: scratch_80.py media list [OPTIONS]
Try 'scratch_80.py media list --help' for help.

Error: At least one of the following options from 'List media by' option group is required:
  '--room-id' / '-r'
  '--user-id' / '-u'
JOJ0 commented

Thank you very much for taking the time to reproduce! That helps already. It must be something on my end then!

JOJ0 commented

Found it, I had 3 more options in there that I didn't post because I thought it would only clutter up the report:

e.g. one of them being:

@click.option(
    "--reverse", "-r", is_flag=True, default=False,
    help="""Direction of media order. If set it will reverse the sort order of
    --order-by method. This option is only supported together with --user-id.
    """

Your answer brought me on the right track that it must have to do something with those 3 options. Stupid me was just using -r twice, hence the confusion! Haha

Thanks for helping me out!

@JOJ0 You are welcome. :)