Skip to content

Doc: Examples of creating single command applications #66

Open

Description

@pmav99

The docs only have examples for creating multi command applications but there are no examples for creating single command applications. Is there support for this usecase?

I mean this:

app cmd1 <args>
    cmd2 <args>
    cmd3 <args>

vs this:

cmd <args>

Activity

  1. sindrehan commented on Feb 22, 2019

    @sindrehan

    The docs does have an example on how to make a single command application, but as far as I can tell it doesn't work.
    I tried running the following script:

    #!/usr/bin/env python
    
    from cleo import Application
    from cleo import Command
    
    
    class GreetCommand(Command):
        """
        Greets someone
    
        greet
            {name? : Who do you want to greet?}
            {--y|yell : If set, the task will yell in uppercase letters}
        """
    
        def handle(self):
            name = self.argument('name')
    
            if name:
                text = 'Hello {}'.format(name)
            else:
                text = 'Hello'
    
            if self.option('yell'):
                text = text.upper()
    
            self.line(text)
    
    command = GreetCommand()
    
    app = Application()
    app.add(command.default())
    
    # this now executes the 'GreetCommand' without passing its name
    app.run()

    But when I try to run the script without arguments, python cmd.py, it produces the following output:

    Console Tool
    
    USAGE
      console [-h] [-q] [-v [<...>]] [-V] [--ansi] [--no-ansi] [-n] <command> [<arg1>] ... [<argN>]
    
    ARGUMENTS
      <command>              The command to execute
      <arg>                  The arguments of the command
    
    GLOBAL OPTIONS
      -h (--help)            Display this help message
      -q (--quiet)           Do not output any message
      -v (--verbose)         Increase the verbosity of messages: "-v" for normal output, "-vv" for more
                             verbose output and "-vvv" for debug
      -V (--version)         Display this application version
      --ansi                 Force ANSI output
      --no-ansi              Disable ANSI output
      -n (--no-interaction)  Do not ask any interactive question
    
    AVAILABLE COMMANDS
      completions            Generate completion scripts for your shell.
      greet                  Greets someone
      help                   Display the manual of a command
    

    Trying to run python cmd.py John produces

    [CannotResolveCommandException]
    The command "John" is not defined.
    

    So either I'm missing something on how to enable the default command, or something is wrong.

    I'm running version 0.7.2, installed from PyPi, on Ubuntu 16.04, with python 3.7.2.

  2. m9810223 commented on Dec 3, 2021

    @m9810223
  3. added this to the Future milestone on Jul 7, 2023
  4. modified the milestones: Future, 3.0 on Nov 28, 2024
  5. added
    enhancement
    docs
    on Jan 3, 2025
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

    docsenhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions