{"id":21755317,"url":"https://github.com/hiroxto/console-wrapper","last_synced_at":"2026-05-20T04:36:55.806Z","repository":{"id":56984764,"uuid":"137360387","full_name":"hiroxto/console-wrapper","owner":"hiroxto","description":"Wrapper class of symfony/console.","archived":false,"fork":false,"pushed_at":"2019-04-02T09:31:11.000Z","size":112,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-25T23:51:39.318Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hiroxto.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-06-14T13:15:14.000Z","updated_at":"2020-05-08T02:10:05.000Z","dependencies_parsed_at":"2022-08-21T12:20:28.357Z","dependency_job_id":null,"html_url":"https://github.com/hiroxto/console-wrapper","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hiroxto%2Fconsole-wrapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hiroxto%2Fconsole-wrapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hiroxto%2Fconsole-wrapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hiroxto%2Fconsole-wrapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hiroxto","download_url":"https://codeload.github.com/hiroxto/console-wrapper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244728237,"owners_count":20500023,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-26T09:17:33.901Z","updated_at":"2026-05-20T04:36:50.767Z","avatar_url":"https://github.com/hiroxto.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# console-wrapper\n\n[![Build Status](https://travis-ci.org/hiroto-k/console-wrapper.svg?branch=master)](https://travis-ci.org/hiroto-k/console-wrapper)\n[![PHP from Packagist](https://img.shields.io/packagist/php-v/hiroto-k/console-wrapper.svg)](https://packagist.org/packages/hiroto-k/console-wrapper)\n[![Maintainability](https://api.codeclimate.com/v1/badges/ba59e655bdc9c4410966/maintainability)](https://codeclimate.com/github/hiroto-k/console-wrapper/maintainability)\n[![License](https://img.shields.io/github/license/hiroto-k/console-wrapper.svg)](https://github.com/hiroto-k/console-wrapper/blob/master/LICENSE)\n\nWrapper class of [symfony/console](https://github.com/symfony/console)\n\n## Install\n\n``composer require hiroto-k/console-wrapper:^1.0``\n\n## Documents\n\n- [Examples](#examples)\n    - [Command class](#example-command-class)\n    - [Application class](#example-application-class)\n    - [Execute file](#example-execute-file)\n- [Uses](#uses)\n    - [Arguments](#arguments)\n    - [Options](#options)\n    - [Global options](#global-options)\n    - [Output](#output)\n    - [Auto add commands by PSR-4](#auto-add-commands-by-psr-4)\n    - [Logger](#logger)\n        - [Default logger](#default-logger)\n        - [Override the default logger](#override-the-default-logger)\n        - [Using logger in Command::setup](#using-logger-in-commandsetup)\n    - [Helpers and Utils](#helpers-and-utils)\n        - [Confirm question](#confirm-question)\n        - [Call other command](#call-other-command)\n        - [Render tables](#render-tables)\n        - [Progress Bar](#progress-bar)\n        - [Gets a helper instance](#gets-a-helper-instance)\n\n## Examples\n\n### Example Command class\n\n```php\n\u003c?php\n\nnamespace ExampleApp\\Commands;\n\nuse HirotoK\\ConsoleWrapper\\Command;\nuse Symfony\\Component\\Console\\Input\\InputArgument;\nuse Symfony\\Component\\Console\\Input\\InputOption;\n\n/**\n * Command class of \"greeting\" command.\n */\nclass GreetingCommand extends Command\n{\n    /**\n     * Definition the command name.\n     *\n     * @var string\n     */\n    protected $name = 'greeting';\n\n    /**\n     * Definition the command description.\n     *\n     * @var string\n     */\n    protected $description = 'Example greeting command with console-wrapper';\n\n    /**\n     * Setup the command class.\n     */\n    protected function setup()\n    {\n        // Setup class configure.\n        // This method is called before the \"configure\" method.\n    }\n\n    /**\n     * Execute the command.\n     */\n    protected function handle()\n    {\n        $greet = $this-\u003eoption('hi') ? 'Hi, ' : 'Hello, ';\n\n        $name = $this-\u003eargument('first-name');\n\n        if ($this-\u003ehasArgument('family-name')) {\n            $name .= ' '.$this-\u003eargument('family-name');\n        }\n\n        $this-\u003ewriteln($greet.$name);\n    }\n\n    /**\n     * Definition the command arguments.\n     *\n     * @see \\Symfony\\Component\\Console\\Command\\Command::addArgument\n     *\n     * @return array\n     */\n    protected function commandArguments()\n    {\n        return [\n            // [$name, $mode = null, $description = '', $default = null]\n            ['first-name', InputArgument::REQUIRED, 'Your first name (required)'],\n            ['family-name', InputArgument::OPTIONAL, 'Your family name (optional)'],\n        ];\n    }\n\n    /**\n     * Definition the command options.\n     *\n     * @see \\Symfony\\Component\\Console\\Command\\Command::addOption\n     *\n     * @return array\n     */\n    protected function commandOptions()\n    {\n        return [\n            // [$name, $shortcut = null, $mode = null, $description = '', $default = null]\n            ['hi', null, InputOption::VALUE_NONE, 'Use \"Hi\".'],\n        ];\n    }\n}\n```\n\n### Example Application class\n\n```php\n\u003c?php\n\nnamespace ExampleApp;\n\nuse HirotoK\\ConsoleWrapper\\Application as WrapperApplication;\nuse Symfony\\Component\\Console\\Input\\InputOption;\n\n/**\n * Customize application class.\n */\nclass Application extends WrapperApplication\n{\n    /**\n     * Definition the global command options.\n     *\n     * @return array\n     */\n    protected function globalOptions()\n    {\n        return [\n            // [$name, $shortcut = null, $mode = null, $description = '', $default = null]\n            ['debug', null, InputOption::VALUE_NONE, 'Enable debug mode.'],\n            ['config', 'c', InputOption::VALUE_REQUIRED, 'Set path of config file.', 'path/to/default'],\n        ];\n    }\n}\n```\n\n### Example execute file\n\n```php\n\u003c?php\n\nuse ExampleApp\\Application;\nuse ExampleApp\\Commands\\GreetingCommand;\n\n$application = new Application();\n\n// Add command class.\n$application-\u003eadd(new GreetingCommand());\n\n// Start the application.\n$application-\u003erun();\n```\n\n## Uses\n\n### Arguments\n\nDefinition the arguments, use the ``Command::commandArguments()`` method.\n\nReturned array will pass to the ``Command::addArgument()`` method.\nSee the documents for arguments of ``Command::addArgument()`` method.\n\n- [Using Command Arguments](https://symfony.com/doc/current/console/input.html#using-command-arguments)\n\n```php\n/**\n * Definition the command arguments.\n *\n * @return array\n */\nprotected function commandArguments()\n{\n    return [\n        // [$name, $mode = null, $description = '', $default = null]\n        ['user-id', InputArgument::REQUIRED, 'User name (required)'],\n        ['task', InputArgument::OPTIONAL, 'Task name (optional)', 'default'],\n    ];\n}\n```\n\nFor the access to arguments, use the ``Command::argument()`` method.\n\nIf you need to all of the arguments, use the ``Command::arguments()`` method\n\nTo checks if the arguments exists, use the ``Command::hasArgument()`` method.\n\n```php\nprotected function handle()\n{\n    // Get argument value.\n    $userId = $this-\u003eargument('user-id');\n\n    // Get all arguments.\n    $allArguments = $this-\u003earguments();\n\n    // Checks whether a argument exists.\n    $hasTaskArgument = $this-\u003ehasArgument('task');\n}\n```\n\n### Options\n\nDefinition the options, use the ``Command::commandOptions()`` method.\n\nReturned array will pass to the ``Command::addOption()`` method.\nSee the documents for arguments of ``Command::addOption()`` method.\n\n- [Using Command Options](https://symfony.com/doc/current/console/input.html#using-command-options)\n\n```php\n/**\n * Definition the command options.\n *\n * @return array\n */\nprotected function commandOptions()\n{\n    return [\n        // [$name, $shortcut = null, $mode = null, $description = '', $default = null]\n        ['name', null, InputOption::VALUE_REQUIRED, 'User name.', 'default name'],\n        ['force', null, InputOption::VALUE_NONE, 'Force execute.'],\n    ];\n}\n```\n\nFor the access to options, use the ``Command::option()`` method.\n\nIf you need to all of the options, use the ``Command::options()`` method\n\nTo checks if the options exists, use the ``Command::hasOption()`` method.\n\n```php\nprotected function handle()\n{\n    // Get option value.\n    $name = $this-\u003eoption('name');\n\n    // Get all options.\n    $allOptions = $this-\u003eoptions();\n\n    // Checks whether a option exists.\n    $hasForceOption = $this-\u003ehasOption('force');\n}\n```\n\n### Global options\n\nconsole-wrapper can easily set global options.\n\n```php\n\u003c?php\n\nnamespace ExampleApp;\n\nuse HirotoK\\ConsoleWrapper\\Application as WrapperApplication;\nuse Symfony\\Component\\Console\\Input\\InputOption;\n\nclass Application extends WrapperApplication\n{\n    /**\n     * Definition the global command options.\n     *\n     * @return array\n     */\n    protected function globalOptions()\n    {\n        return [\n            // [$name, $shortcut = null, $mode = null, $description = '', $default = null]\n            ['debug', null, InputOption::VALUE_NONE, 'Enable debug mode.'],\n            ['config', 'c', InputOption::VALUE_REQUIRED, 'Set path of config file.', 'path/to/default'],\n        ];\n    }\n}\n```\n\n### Output\n\nconsole-wrapper is provided some output methods.\n\n```php\nprotected function handle()\n{\n    // Display normal message.\n    $this-\u003ewriteln('message');\n    $this-\u003ewriteln([\n        'multi',\n        'line',\n        'message',\n    ]);\n\n    // Display message with styles.\n    $this-\u003einfo('info style');\n    $this-\u003ecomment('comment style');\n    $this-\u003equestion('question style');\n    $this-\u003eerror('error style');\n}\n```\n\n### Auto add commands by PSR-4\n\nIf project using PSR-4, auto load all commands by ``Application::loadByPsr4()`` method.\n\n```php\n/**\n * Auto add commands by PSR-4.\n * \n * loadByPsr4(string $nameSpacePrefix, string $targetDir)\n */\n$application-\u003eloadByPsr4(\"\\ExampleApp\\Commands\", realpath(__DIR__.'/src/Commands'));\n```\n\n### Logger\n\nconsole-wrapper can easily use the logger.\n\nSets the logger instance in the execute file.\nLogger class must be implemented the ``\\Psr\\Log\\LoggerInterface`` interface.\n\n```php\n/**\n * Set logger instance to application and command class.\n */\n$application-\u003esetLogger($logger);\n```\n\nIn the command class, use the ``logger()`` method and use the logger.\n\n```php\nprotected function handle()\n{\n    $this-\u003elogger()-\u003edebug('Debug message');\n    $this-\u003elogger()-\u003eerror('Error message');\n}\n```\n\n#### Default logger\n\nIf logger instance not sets in application, default using the ``\\Psr\\Log\\NullLogger`` class.\n\n[\\Psr\\Log\\NullLogger](https://github.com/php-fig/log/blob/master/Psr/Log/NullLogger.php)\n\n```php\nprotected function handle()\n{\n    // Output Psr\\Log\\NullLogger\n    $this-\u003ewriteln(get_class($this-\u003elogger()));\n}\n```\n\n#### Override the default logger\n\nIf you want override the default logger, please override the ``Application::createDefaultLogger()`` method.\nReturn instance must be implemented the ``\\Psr\\Log\\LoggerInterface`` interface.\n\n```php\n// Use monolog in default logger\n\nuse HirotoK\\ConsoleWrapper\\Application as WrapperApplication;\nuse Monolog\\Logger;\n\nclass Application extends WrapperApplication\n{\n    /**\n     * Override default logger instance.\n     * Return instance must be implement the \\Psr\\Log\\LoggerInterface\n     * \n     * @return \\Monolog\\Logger\n     */\n    protected function createDefaultLogger()\n    {\n        return new Logger();\n    }\n}\n```\n\n#### Using logger in Command::setup\n\nIf using the logger in ``Command::setup()`` method, **must be sets the logger instance before commands add**.\n\n```php\n// Register logger instance\n$application-\u003esetLogger($logger);\n\n// Add commands\n$application-\u003eadd(new GreetingCommand());\n$application-\u003eloadByPsr4(\"\\ExampleApp\\Commands\", realpath(__DIR__.'/src/Commands'));\n```\n\n### Helpers and Utils\n\n#### Confirm question\n\nSimple confirmation. Default returns ``true``.\n\n```php\nprotected function handle()\n{\n    if ($this-\u003econfirm('continue ? (y/n) ')) {\n        // If enter y\n    }\n}\n```\n\nsets default value\n\n```php\nprotected function handle()\n{\n    if ($this-\u003econfirm('continue ? (y/n) ', false)) {\n        // If enter y\n    }\n}\n```\n\n#### Call other command\n\nCall other command in command class.\n\n```php\nprotected function handle()\n{\n    // Call the \"other-command-name\" command\n    $this-\u003ecallCommand('other-command-name');\n}\n```\n\nwith parameters\n\n```php\nprotected function handle()\n{\n    // Call the \"other-command-name\" command, with name parameter\n    $this-\u003ecallCommand('other-command-name', ['name' =\u003e 'example']);\n}\n```\n\n#### Render tables\n\n```php\nprotected function handle()\n{\n    // Only creates Table class instance.\n    $table = $this-\u003ecreateTable();\n\n    // Sets headers and rows\n    $headers = ['name', 'location'];\n    $rows = [\n        ['Hoge', 'jp'],\n        ['Foo', 'us'],\n    ];\n    $table = $this-\u003ecreateTable($headers, $rows)\n\n    // Render table\n    $table-\u003erender();\n}\n```\n\n\n```text\n+------+----------+\n| name | location |\n+------+----------+\n| Hoge | jp       |\n| Foo  | us       |\n+------+----------+\n```\n\ncustomize tables, place see the ``\\Symfony\\Component\\Console\\Helper\\Table`` class.\n\n- [Document of Table class](https://symfony.com/doc/current/components/console/helpers/table.html)\n\n```php\n// Set the column width.\n$this\n    -\u003ecreateTable($headers, $rows)\n    -\u003esetColumnWidth(0, 10)\n    -\u003erender();\n```\n\n#### Progress Bar\n\n```php\nprotected function handle()\n{\n    $progressBar = $this-\u003ecreateProgressBar(100);\n\n    $progressBar-\u003estart();\n\n    $i = 0;\n    while ($i++ \u003c 100) {\n        $progressBar-\u003eadvance();\n    }\n\n    $progressBar-\u003efinish();\n}\n```\n\ncustomize progress bar, place see the ``\\Symfony\\Component\\Console\\Helper\\ProgressBar`` class.\n\n- [Document of ProgressBar class](https://symfony.com/doc/current/components/console/helpers/progressbar.html)\n\n#### Gets a helper instance\n\n```php\nprotected function handle()\n{\n    // Get the question helper instance\n    $this-\u003egetQuestionHelper();\n\n    // Get the process helper instance.\n    $this-\u003egetProcessHelper();\n}\n```\n\n## License\n\n[MIT License](https://github.com/hiroto-k/console-wrapper/blob/master/LICENSE \"MIT License\")\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhiroxto%2Fconsole-wrapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhiroxto%2Fconsole-wrapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhiroxto%2Fconsole-wrapper/lists"}