<commit_before><?php
/**
 * @license   http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
 * @copyright Copyright (c) 2014 Zend Technologies USA Inc. (http://www.zend.com)
 */

namespace ZF\Console;

use InvalidArgumentException;
use Traversable;
use Zend\Console\Adapter\AdapterInterface as Console;
use Zend\Console\ColorInterface as Color;

/**
 * Create and execute console applications.
 */
class Application
{
    /**
     * @var null|string|callable
     */
    protected $banner;

    /**
     * @var Console
     */
    protected $console;

    /**
     * @var Dispatcher
     */
    protected $dispatcher;

    /**
     * @var null|string|callable
     */
    protected $footer;

    /**
     * @var string
     */
    protected $name;

    /**
     * @var RouteCollection
     */
    protected $routeCollection;

    /**
     * @var string
     */
    protected $version;

    /**
     * Initialize the application
     *
     * Creates a RouteCollection and populates it with the $routes provided.
     *
     * Sets the banner to call showVersion().
     *
     * If no help command is defined, defines one.
     *
     * If no version command is defined, defines one.
     *
     * @param string $name Application name
     * @param string $version Application version
     * @param array|Traversable $routes Routes/route specifications to use for the application
     * @param Console $console Console adapter to use within the application
     * @param Dispatcher $dispatcher Configured dispatcher mapping routes to callables
     */
    public function __construct(
        $name,
        $version,
        $routes,
        Console $console,
        Dispatcher $dispatcher
    ) {
        if (! is_array($routes) && ! $routes instanceof Traversable) {
            throw new InvalidArgumentException('Routes must be provided as an array or Traversable object');
        }

        $this->name       = $name;
        $this->version    = $version;
        $this->console    = $console;
        $this->dispatcher = $dispatcher;

        $this->routeCollection = $routeCollection = new RouteCollection();
        $this->setRoutes($routes);

        $this->banner = array($this, 'showVersion');

        if (! $routeCollection->hasRoute('help')) {
            $this->setupHelpCommand($routeCollection, $dispatcher);
        }

        if (! $routeCollection->hasRoute('version')) {
            $this->setupVersionCommand($routeCollection, $dispatcher);
        }
    }

    /**
     * Run the application
     *
     * If no arguments are provided, pulls them from $argv, stripping the
     * script argument first.
     *
     * If the argument list is empty, displays a usage message.
     *
     * If arguments are provided, but no routes match, displays a usage message
     * and returns a status of 1.
     *
     * Otherwise, attempts to dispatch the matched command, returning the
     * execution status.
     *
     * @param array $args
     * @return int
     */
    public function run(array $args = null)
    {
        $this->setProcessTitle();

        if ($args === null) {
            global $argv;
            $args = array_slice($argv, 1);
        }

        if (empty($args)) {
            $this->showMessage($this->banner);
            $this->showUsageMessage();
            $this->showMessage($this->footer);
            return 0;
        }

        $route = $this->routeCollection->match($args);
        if (! $route instanceof Route) {
            $this->showUnmatchedRouteMessage($args);
            return 1;
        }

        return $this->dispatcher->dispatch($route, $this->console);
    }

    /**
     * Display the application version
     *
     * @param Console $console
     * @return int
     */
    public function showVersion(Console $console)
    {
        $console->writeLine(
            $console->colorize($this->name . ',', Color::GREEN)
            . ' version '
            . $console->colorize($this->version, Color::BLUE)
        );
        $console->writeLine('');
        return 0;
    }
<commit_msg>Usability: Added better error reporting when a command is called with the correct name, but with wrong arguments.<commit_after><?php
/**
 * @license   http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
 * @copyright Copyright (c) 2014 Zend Technologies USA Inc. (http://www.zend.com)
 */

namespace ZF\Console;

use InvalidArgumentException;
use Traversable;
use Zend\Console\Adapter\AdapterInterface as Console;
use Zend\Console\ColorInterface as Color;

/**
 * Create and execute console applications.
 */
class Application
{
    /**
     * @var null|string|callable
     */
    protected $banner;

    /**
     * @var Console
     */
    protected $console;

    /**
     * @var Dispatcher
     */
    protected $dispatcher;

    /**
     * @var null|string|callable
     */
    protected $footer;

    /**
     * @var string
     */
    protected $name;

    /**
     * @var RouteCollection
     */
    protected $routeCollection;

    /**
     * @var string
     */
    protected $version;

    /**
     * Initialize the application
     *
     * Creates a RouteCollection and populates it with the $routes provided.
     *
     * Sets the banner to call showVersion().
     *
     * If no help command is defined, defines one.
     *
     * If no version command is defined, defines one.
     *
     * @param string $name Application name
     * @param string $version Application version
     * @param array|Traversable $routes Routes/route specifications to use for the application
     * @param Console $console Console adapter to use within the application
     * @param Dispatcher $dispatcher Configured dispatcher mapping routes to callables
     */
    public function __construct(
        $name,
        $version,
        $routes,
        Console $console,
        Dispatcher $dispatcher
    ) {
        if (! is_array($routes) && ! $routes instanceof Traversable) {
            throw new InvalidArgumentException('Routes must be provided as an array or Traversable object');
        }

        $this->name       = $name;
        $this->version    = $version;
        $this->console    = $console;
        $this->dispatcher = $dispatcher;

        $this->routeCollection = $routeCollection = new RouteCollection();
        $this->setRoutes($routes);

        $this->banner = array($this, 'showVersion');

        if (! $routeCollection->hasRoute('help')) {
            $this->setupHelpCommand($routeCollection, $dispatcher);
        }

        if (! $routeCollection->hasRoute('version')) {
            $this->setupVersionCommand($routeCollection, $dispatcher);
        }
    }

    /**
     * Run the application
     *
     * If no arguments are provided, pulls them from $argv, stripping the
     * script argument first.
     *
     * If the argument list is empty, displays a usage message.
     *
     * If arguments are provided, but no routes match, displays a usage message
     * and returns a status of 1.
     *
     * Otherwise, attempts to dispatch the matched command, returning the
     * execution status.
     *
     * @param array $args
     * @return int
     */
    public function run(array $args = null)
    {
        $this->setProcessTitle();

        if ($args === null) {
            global $argv;
            $args = array_slice($argv, 1);
        }

        if (empty($args)) {
            $this->showMessage($this->banner);
            $this->showUsageMessage();
            $this->showMessage($this->footer);
            return 0;
        }

        $route = $this->routeCollection->match($args);
        $name  = $argv[1];
        if (! $route instanceof Route && !$this->routeCollection->hasRoute($name)) {
            $this->showUnmatchedRouteMessage($args);
            return 1;
        }

        $route = $this->routeCollection->match(array('help', $name));

        return $this->dispatcher->dispatch($route, $this->console);
    }

    /**
     * Display the application version
     *
     * @param Console $console
     * @return int
     */
    public function showVersion(Console $console)
    {
        $console->writeLine(
            $console->colorize($this->name . ',', Color::GREEN)
            . ' version '
            . $console->colorize($this->version, Color::BLUE)
        );
        $console->writeLine('');
        return 0;
    }

{'fraction_non_alphanumeric': 0.11893957487461189, 'fraction_numerical': 0.002746596608550275, 'mean_word_length': 2.122669649515287, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '766656', 'n_tokens_mistral': 2300, 'n_tokens_neox': 2233, 'n_words': 629}