click-extra icon indicating copy to clipboard operation
click-extra copied to clipboard

Change order of commands

Open isezen opened this issue 3 years ago • 4 comments

Hello,

Is there any workaround that commands not to be sorted by default? I want them to be in order as I wish. I tried this stackoverflow solution. It works for click library but not for click-extra. I feel that they are sorted at somewhere else in click-extra code, but I couldn't find. My trial is as below:

import cloup as _cloup
import click_extra as _ce
from click_extra import *  # pylint: disable=W0401,W0614 # noqa:F401,F403


def _extra_params():
    return [
        _ce.colorize.HelpOption(),
    ]


class OrderedExtraGroup(_ce.commands.ExtraGroup):  # pylint: disable=R0901
    """Ordered ExtraGroup Class."""

    def __init__(self, *args, **kwargs):
        """Initialize."""
        self.help_priorities = {}
        super().__init__(*args, **kwargs)

    def get_help(self, ctx):
        """Get Help."""
        self.list_commands = self.list_commands_for_help
        return super().get_help(ctx)

    def list_commands_for_help(self, ctx):
        """Reorder the list of commands when listing the help."""
        commands = super().list_commands(ctx)
        return list(c[1] for c in sorted(
            (self.help_priorities.get(command, 1), command)
            for command in commands))

    def command(self, *args, **kwargs):
        """
        Ordered Command.

        Behave the same as `click.Group.command()` except capture a
        priority for listing command names in help.
        """
        help_priority = kwargs.pop('help_priority', 1)
        help_priorities = self.help_priorities

        def decorator(f):
            cmd = super(OrderedExtraGroup, self).command(*args, **kwargs)(f)
            help_priorities[cmd.name] = help_priority
            return cmd

        return decorator


group = _ce.decorators.decorator_factory(
    dec=_cloup.group,
    cls=OrderedExtraGroup,
    params=_extra_params,
)

isezen avatar Apr 18 '23 21:04 isezen

There are two places in Click Extra were options are sorted:

  • The default extra parameters order is hard-coded in the form of a list: https://github.com/kdeldycke/click-extra/blob/main/click_extra/decorators.py#L80-L102
  • There is an extra_option_at_end parameter on commands that is True by default: https://github.com/kdeldycke/click-extra/blob/bb19d20b681d1b9856640fe6591c1720dccae01d/click_extra/commands.py#L126-L128

kdeldycke avatar Apr 22 '23 10:04 kdeldycke

In the mean time, I added a stub on option ordering in the documentation: https://github.com/kdeldycke/click-extra/commit/2c0e686e4809eb07a8140217391cd94908ec4b6e

If you feel like it, you can update this documentation with some examples on how to use these option.

Now if you think the current ordering options are too limited, maybe we can discuss about adding an order parameter on ExtraOption class.

kdeldycke avatar Apr 22 '23 11:04 kdeldycke

Hello @kdeldycke,

I just want to be sure that we are on the same point. I've been talking about ordering of user commands but not default extra parameters. For instance, the current output is in alphabetical order as follow:

Usage: cwf run [OPTIONS] COMMAND [ARGS]...

  Run CMAQ Workflow components.

Options:
  -h, --help  Show this message and exit.

Commands:
  bcon  Create chemical boundary conditions.
  cctm  Run CMAQ Chemistry Transport Model (CCTM).
  emis  Create Emissions.
  icon  Create chemical initial conditions.
  mcip  Meteorology - Chemistry Interface Processor.
  post  Post-process (combine) CMAQ outputs.

However, the order is important for my case. As you said, perhaps you can define an order parameter if a user want them to be in alphabetical order, otherwise you can leave them in the way the functions processed by decorators.

isezen avatar Apr 29 '23 11:04 isezen