Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis committed Apr 11, 2022
1 parent 118a89c commit a54433e
Show file tree
Hide file tree
Showing 9 changed files with 311 additions and 17 deletions.
31 changes: 31 additions & 0 deletions README.md
Expand Up @@ -10,6 +10,37 @@
composer require jbzoo/cli
```

### Usage example

The minimak reference example:

```php
<?php

declare(strict_types=1);

use JBZoo\Cli\CliCommand;

class MyCommand extends CliCommand
{
protected function configure(): void
{
$this->setName('namespace:command-name');
parent::configure();
}

/**
* @inheritDoc
*/
protected function executeAction(): int
{
$this->_('Hello world!');
return 0;
}
}
```



### See also

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -29,7 +29,7 @@
},

"require-dev" : {
"jbzoo/toolbox-dev" : "^3.3.0"
"jbzoo/toolbox-dev" : "^3.3.1"
},

"autoload" : {
Expand All @@ -39,6 +39,7 @@
},

"autoload-dev" : {
"psr-4" : {"ExampleApp\\" : "tests/ExampleApp"},
"classmap" : ["tests"]
},

Expand Down
19 changes: 16 additions & 3 deletions src/CliCommand.php
Expand Up @@ -183,8 +183,8 @@ protected function getOptFloat(string $optionName): float
*/
protected function getOptString(string $optionName): string
{
$value = $this->getOpt($optionName) ?? '';
return (string)$value;
$value = (string)$this->getOpt($optionName);
return \trim($value) ?: '';
}

/**
Expand All @@ -197,6 +197,19 @@ protected function getOptArray(string $optionName): array
return (array)$list;
}

/**
* @param string $optionName
* @param string $defaultDatetime
* @return \DateTimeImmutable
*/
protected function getOptDatetime(
string $optionName,
string $defaultDatetime = '1970-01-01 00:00:00'
): \DateTimeImmutable {
$dateAsString = $this->getOptString($optionName) ?: $defaultDatetime;
return new \DateTimeImmutable($dateAsString);
}

/**
* @return string|null
*/
Expand Down Expand Up @@ -251,7 +264,7 @@ private function showProfiler(): void
return;
}

[$totalTime, $curMemory, $maxMemory] = $this->helper->getProfileDate();
[$totalTime, $curMemory, $maxMemory] = $this->helper->getProfileInfo();

$this->_(\implode('; ', [
"Memory Usage/Peak: <green>{$curMemory}</green>/<green>{$maxMemory}</green>",
Expand Down
8 changes: 5 additions & 3 deletions src/Helper.php
Expand Up @@ -44,6 +44,8 @@ class Helper
public const VERB_ERROR = 'error';
public const VERB_EXCEPTION = 'exception';

public const TIMESTAMP_FORMAT = 'Y-m-d H:i:s.v P';

/**
* @var $this
*/
Expand Down Expand Up @@ -181,7 +183,7 @@ public static function addOutputStyles(OutputInterface $output): OutputInterface
/**
* @return array
*/
public function getProfileDate(): array
public function getProfileInfo(): array
{
return [
\number_format(\microtime(true) - $this->startTimer, 3),
Expand Down Expand Up @@ -213,12 +215,12 @@ public function _($messages, string $verboseLevel = self::VERB_DEFAULT): void
$profilePrefix = '';

if ($this->input->getOption('timestamp')) {
$timestamp = (new \DateTimeImmutable())->format(\DateTimeInterface::RFC3339);
$timestamp = (new \DateTimeImmutable())->format(self::TIMESTAMP_FORMAT);
$profilePrefix .= "<green>[</green>{$timestamp}<green>]</green> ";
}

if ($this->input->getOption('profile')) {
[$totalTime, $curMemory] = $this->getProfileDate();
[$totalTime, $curMemory] = $this->getProfileInfo();
$profilePrefix .= "<green>[</green>{$curMemory}<green>/</green>{$totalTime}s<green>]</green> ";
}

Expand Down
185 changes: 185 additions & 0 deletions tests/ExampleApp/Commands/ExampleAgruments.php
@@ -0,0 +1,185 @@
<?php

/**
* JBZoo Toolbox - Cli
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Cli
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @link https://github.com/JBZoo/Cli
*/

declare(strict_types=1);

namespace ExampleApp\Commands;

use JBZoo\Cli\CliCommand;
use Symfony\Component\Console\Input\InputOption;

/**
* Class ExampleAgruments
*/
class ExampleAgruments extends CliCommand
{
protected function configure(): void
{
$this
->setName('example:agruments')
->setDescription('Show description of command.')
->setHelp(
"Full description and usage of command.\n" .
"You can use severla lines."
)

// None
->addOption('opt', 'o', InputOption::VALUE_NONE, 'Just a boolean flag')

// Required
->addOption('opt-req', null, InputOption::VALUE_REQUIRED, 'The option with required value')
->addOption('opt-req-default', null, InputOption::VALUE_REQUIRED,
'The option is requred but it has default value', 42)

// Optional
->addOption('opt-optional', null, InputOption::VALUE_OPTIONAL,
'Option is not required and can be undefined')
->addOption('opt-optional-default', null, InputOption::VALUE_OPTIONAL,
'Option is not required with default value', 42)

// Array
->addOption(
'opt-array-optional',
null,
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
"Multiple values are allowed. Can be empty"
)
->addOption(
'opt-array-req',
null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Multiple values are allowed. Value is required'
)
->addOption(
'opt-array-req-default',
'a',
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Multiple values are allowed. Value is required with defaut value',
[42, 'foo', 'bar']
)

// Arguments
->addArgument('arg-req', InputOption::VALUE_REQUIRED, 'Argument is required')
->addArgument('arg-default', InputOption::VALUE_REQUIRED, 'Argument is optional with default value', 42)
->addArgument('arg-optional', InputOption::VALUE_OPTIONAL, 'Argument is optional, no default value');

parent::configure();
}

/**
* @inheritDoc
*/
protected function executeAction(): int
{
////////////////////////////////////////// Just a boolean flag
// ./my-app example:agruments
$this->getOpt('opt'); // false

// ./my-app example:agruments --opt
$this->getOpt('opt'); // true

// ./my-app example:agruments -o
$this->getOpt('opt'); // true


////////////////////////////////////////// The option requires a value
// ./my-app example:agruments --opt-req
$this->getOpt('opt-req'); // Exception: The "--opt-req" option requires a value.

// ./my-app example:agruments --opt-req=123.6
$this->getOpt('opt-req'); // "123.6"

// ./my-app example:agruments --opt-req=123.6
$this->getOptBool('opt-req'); // true

// ./my-app example:agruments --opt-req=123.6
$this->getOptInt('opt-req'); // 123

// ./my-app example:agruments --opt-req=123.6
$this->getOptFloat('opt-req'); // 123.6

// ./my-app example:agruments --opt-req=" 123.6 "
$this->getOptString('opt-req'); // "123.6"

// ./my-app example:agruments --opt-req=123.6
$this->getOptArray('opt-req'); // ["123.6"]

// ./my-app example:agruments --opt-req="15 July 2021 13:48:00"
$this->getOptDatetime('opt-req'); // \DateTimeImmutable {date: 2021-07-15 13:48:00. UTC (+00:00) }

////////////////////////////////////////// The option requires a value with default value
// ./my-app example:agruments
$this->getOpt('opt-req-default'); // 42

// ./my-app example:agruments --opt-req-default
$this->getOpt('opt-req-default'); // Exception: The "--opt-req-default" option requires a value.

// ./my-app example:agruments --opt-req-default=123.6
$this->getOpt('opt-req-default'); // "123.6"


////////////////////////////////////////// Multiple values are allowed. Value is required with defaut value
// ./my-app example:agruments
$this->getOpt('opt-array-req-default'); // "bar"

// ./my-app example:agruments
$this->getOptArray('opt-array-req-default'); // [42, 'foo', 'bar']

// ./my-app example:agruments --opt-array-req-default=123 --opt-array-req-default=asdasd
$this->getOptArray('opt-array-req-default'); // ['123', 'Qwerty']

// ./my-app example:agruments -a123 -aQwerty
$this->getOptArray('opt-array-req-default'); // ['123', 'Qwerty']

// ./my-app example:agruments -a123 -aQwerty
$this->getOpt('opt-array-req-default'); // 'Qwerty'

// ./my-app example:agruments -aQwerty -aAsd
$this->getOpt('opt-array-req-default'); // 'Asd'


////////////////////////////////////////// Arguments
// ./my-app example:agruments
$this->input->getArgument('arg-req'); // null

// ./my-app example:agruments Qwerty
$this->input->getArgument('arg-req'); // "Qwerty"

// ./my-app example:agruments Qwerty
$this->input->getArgument('arg-default'); // 42

// ./my-app example:agruments Qwerty Some text
$this->input->getArgument('arg-default'); // "Some"

// ./my-app example:agruments Qwerty "Some text"
$this->input->getArgument('arg-default'); // "Some text"

// ./my-app example:agruments Qwerty "Some text"
$this->input->getArgument('arg-optional'); // []

// ./my-app example:agruments Qwerty "Some text" 123
$this->input->getArgument('arg-optional'); // ["123"]

// ./my-app example:agruments Qwerty "Some text" 123 456 "789 098"
$this->input->getArgument('arg-optional'); // ["123", "456", "789 098"]


////////////////////////////////////////// Standard input
// echo " Qwerty 123 " | php ./my-app example:agruments
self::getStdIn(); // " Qwerty 123 \n"

return 0;
}
}
42 changes: 42 additions & 0 deletions tests/ExampleApp/Commands/Simple.php
@@ -0,0 +1,42 @@
<?php

/**
* JBZoo Toolbox - Cli
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Cli
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @link https://github.com/JBZoo/Cli
*/

declare(strict_types=1);

namespace ExampleApp\Commands;

use JBZoo\Cli\CliCommand;

/**
* Class Simple
*/
class Simple extends CliCommand
{
protected function configure(): void
{
$this->setName('simple');
parent::configure();
}

/**
* @inheritDoc
*/
protected function executeAction(): int
{
$this->_('Hello world!');

return 0;
}
}
29 changes: 29 additions & 0 deletions tests/ExampleApp/my-app
@@ -0,0 +1,29 @@
#!/usr/bin/env php
<?php

/**
* JBZoo Toolbox - Cli
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Cli
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @link https://github.com/JBZoo/Cli
*/

declare(strict_types=1);

namespace ExampleApp;

use JBZoo\Cli\CliApplication;

require_once dirname(__DIR__, 2) . '/vendor/autoload.php';

$application = new CliApplication('Example CLI Application', 'v1.0.0');
$application->registerCommandsByPath(__DIR__ . '/Commands', __NAMESPACE__);
// $application->setDefaultCommand('simple');

$application->run();
2 changes: 1 addition & 1 deletion tests/Helper.php
Expand Up @@ -59,7 +59,7 @@ public static function executeReal(
$postAction
]));

// dump($realCommand);
//dump($realCommand);
$process = Process::fromShellCommandline($realCommand, $cwd, null, null, 3600);
$process->run();

Expand Down

0 comments on commit a54433e

Please sign in to comment.