Skip to content

Default command feature does not work #71

Open

Description

@esciara

I came across the same problem as described in #66 (comment) : the steps described to create a single command application does not work.

Example provided in the comment.

Also does not work with cleo 0.7.5 and python 3.6.9 .

Activity

  1. b0g3r commented on Nov 2, 2019

    @b0g3r

    The same on cleo 0.7.6

  2. vikpe commented on Nov 16, 2019

    @vikpe
    Contributor

    Commit 132ad3e (2018-12-06) removed set_default_command().


    In versions <=0.6.8

    Use set_default_command().

    from cleo import Application
    
    from .commands import GreetCommand
    
    app = Application()
    app.add(GreetCommand())
    app.set_default_command("greet", is_single_command=True)
    
    app.run()

    In versions >0.6.8

    Problem

    • When no command is given the application runs the first command that is set as .default().
    • The help() command is added first (as part of the default config) and is thus always run as the default command.

    Solution
    Create a custom config where the help command is not set as default and pass it to your application, example:

    foo/
      config.py
      main.py
    

    config.py

    from clikit.api.args.format.argument import Argument
    from clikit.api.args.format.option import Option
    from clikit.api.event import PRE_HANDLE
    from clikit.api.event import PRE_RESOLVE
    from clikit.config.default_application_config import DefaultApplicationConfig
    from clikit.handler.help import HelpTextHandler
    from clikit.resolver.help_resolver import HelpResolver
    
    
    class ApplicationConfig(DefaultApplicationConfig):
        """
        The default application configuration.
        """
    
        def configure(self):
            self.set_io_factory(self.create_io)
            self.add_event_listener(PRE_RESOLVE, self.resolve_help_command)
            self.add_event_listener(PRE_HANDLE, self.print_version)
    
            self.add_option("help", "h", Option.NO_VALUE, "Display this help message")
            self.add_option("quiet", "q", Option.NO_VALUE, "Do not output any message")
            self.add_option(
                "verbose",
                "v",
                Option.OPTIONAL_VALUE,
                "Increase the verbosity of messages: "
                '"-v" for normal output, '
                '"-vv" for more verbose output '
                'and "-vvv" for debug',
            )
            self.add_option(
                "version", "V", Option.NO_VALUE, "Display this application version"
            )
            self.add_option("ansi", None, Option.NO_VALUE, "Force ANSI output")
            self.add_option("no-ansi", None, Option.NO_VALUE, "Disable ANSI output")
            self.add_option(
                "no-interaction",
                "n",
                Option.NO_VALUE,
                "Do not ask any interactive question",
            )
    
            with self.command("help") as c:
                # c.default()
                c.set_description("Display the manual of a command")
                c.add_argument(
                    "command", Argument.OPTIONAL | Argument.MULTI_VALUED, "The command name"
                )
                c.set_handler(HelpTextHandler(HelpResolver()))

    main.py

    from cleo import Application
    from cleo import Command
    
    from .commands import GreetCommand
    from .config import ApplicationConfig
    
    config = ApplicationConfig("greet", "0.1.0")
    
    app = Application(config=config)
    app.add(GreetCommand().default())
    app.run()

    This is what I could think of until there is a public method available that allows you to define the default command.

  3. added this to the Future milestone on Jul 7, 2023
  4. Ruin2121 commented on Jun 20, 2024

    @Ruin2121

    I know this is a bit old, but it's pretty simple to do it. I don't know if it is intended for you to override self._default_command, but it works.

    from cleo.application import Application
    from cleo.commands.command import Command
    
    class MainCommand(Command):
        """
        Main Command
        """
    
        name = "main"
        description = "Main Command"
    
        arguments = []
    
        def handle(self) -> None:
            print("Hello World!")
    
    class App(Application):
        """
        App
        """
    
        def __init__(self) -> None:
            super().__init__(name="application", version="0.1.0")
            self._default_command = "main"
    
    
    def main() -> int:
        """
        Entry logic.
        """
        app = App()
        app.add(MainCommand())
    
        exit_code: int = app.run()
    
        return exit_code
    
    
    if __name__ == "__main__":
        main()
  5. modified the milestones: Future, 3.0 on Nov 28, 2024
  6. added
    bug
    question
    on Jan 3, 2025
  7. ironbars commented on Jan 5, 2025

    @ironbars

    @Ruin2121 When I set self._default_command, I get the following error (keeping with the greet example). Any idea what the issue might be?

    greet you
    
    The command you does not exist.
    

    But...

    greet greet you
    Hello, you
    

    Oddly enough though, the help works.

    greet --help
    
    Description:
      Greet someone
    
    Usage:
      greet [options] [--] <name>
    
    Arguments:
      name                  Who you want to greet
    ...
    

    Edit: Forgot to mention that I'm setting _default_command like this in my main:

    from cleo.application import Application
    from greet.greet import GreetCommand
    
    
    def cli():
        cmd = GreetCommand()
        app = Application()
        app.add(cmd)
        app._default_command = cmd.name
    
        app.run()
    
    
    if __name__ == "__main__":
        cli()

    I've seen it done this way elsewhere and I don't see a functional difference.

  8. mathisschmidt commented on Jan 13, 2025

    @mathisschmidt

    I have problem for create the default command too. It look like if you have argument, the technic of Ruin2121 is not working. It is same for you?

  9. ironbars commented on Jan 13, 2025

    @ironbars

    @mathisschmidt Correct, same issue. I'm still trying to work something out. As your comment implies, you could use only options (i.e. no arguments) if that would fit your interface design.

    After combing through the code, I did find another attribute that might help in the Application class: self._single_command. There is some logic that uses it to determine what should happen when you run the program. With my initial testing, I got an Index out of range error. That was just my first try, though. Haven't really dug any deeper yet.

  10. mathisschmidt commented on Jan 13, 2025

    @mathisschmidt

    @ironbars I did same, see the error and stop. If I find something before a release give this feature, I will send you a message here.

  11. ironbars commented on Jan 14, 2025

    @ironbars

    @mathisschmidt Thanks, I'll do the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugquestion

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions