cleo icon indicating copy to clipboard operation
cleo copied to clipboard

Doc: Examples of creating single command applications

Open pmav99 opened this issue 7 years ago • 2 comments

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>

pmav99 avatar Jan 20 '19 00:01 pmav99

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.

sindrehan avatar Feb 22 '19 14:02 sindrehan

Anothor simple hacked solution: https://github.com/m9810223/advent-of-code-python/blob/3eb52dd7b168b52064149defc4b2d59e30e05fae/aoc_run.py#L85-L93

m9810223 avatar Dec 03 '21 17:12 m9810223