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

Change order of commands #544

Open
isezen opened this issue Apr 18, 2023 · 3 comments
Open

Change order of commands #544

isezen opened this issue Apr 18, 2023 · 3 comments
Labels
📚 documentation Update to non-code (readme, docstrings, typos, …) 🎁 feature request Not existing yet and need to be implemented 🙏 help wanted I can't do this alone and need contributors

Comments

@isezen
Copy link

isezen commented Apr 18, 2023

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,
)
@kdeldycke
Copy link
Owner

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

@kdeldycke
Copy link
Owner

In the mean time, I added a stub on option ordering in the documentation: 2c0e686

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 kdeldycke added 🎁 feature request Not existing yet and need to be implemented 📚 documentation Update to non-code (readme, docstrings, typos, …) 🙏 help wanted I can't do this alone and need contributors labels Apr 22, 2023
@isezen
Copy link
Author

isezen commented Apr 29, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📚 documentation Update to non-code (readme, docstrings, typos, …) 🎁 feature request Not existing yet and need to be implemented 🙏 help wanted I can't do this alone and need contributors
Projects
None yet
Development

No branches or pull requests

2 participants