Skip to content

Commit 4086f6e

Browse files
Use Garden CLI for arg parsing
1 parent c93a702 commit 4086f6e

File tree

5 files changed

+196
-82
lines changed

5 files changed

+196
-82
lines changed

composer.json

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,36 @@
11
{
2-
"name": "ecoapm/libyear",
3-
"version": "3.0.0",
4-
"description": "A simple measure of software dependency freshness",
5-
"homepage": "https://libyear.com",
6-
"readme": "README.md",
7-
"license": "MIT",
8-
"authors": [
9-
{
10-
"name": "ecoAPM",
11-
"email": "[email protected]",
12-
"homepage": "https://ecoAPM.com"
13-
}
14-
],
15-
"bin": [
16-
"libyear"
17-
],
18-
"require": {
19-
"php": ">=8.1",
20-
"ext-json": ">=8.1",
21-
"composer/semver": "3.3.2",
22-
"guzzlehttp/guzzle": "7.7.0",
23-
"wp-cli/php-cli-tools": "0.11.18"
24-
},
25-
"require-dev": {
26-
"phpunit/phpunit": "10.2.3",
27-
"mockery/mockery": "1.6.2"
28-
},
29-
"autoload": {
30-
"psr-4": {
31-
"ecoAPM\\LibYear\\": "src/",
32-
"ecoAPM\\LibYear\\Tests\\": "tests/"
33-
}
34-
}
2+
"name": "ecoapm/libyear",
3+
"version": "3.0.0",
4+
"description": "A simple measure of software dependency freshness",
5+
"homepage": "https://libyear.com",
6+
"readme": "README.md",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "ecoAPM",
11+
"email": "[email protected]",
12+
"homepage": "https://ecoAPM.com"
13+
}
14+
],
15+
"bin": [
16+
"libyear"
17+
],
18+
"require": {
19+
"php": ">=8.1",
20+
"ext-json": ">=8.1",
21+
"composer/semver": "3.3.2",
22+
"guzzlehttp/guzzle": "7.7.0",
23+
"vanilla/garden-cli": "v4.0",
24+
"wp-cli/php-cli-tools": "v0.11.18"
25+
},
26+
"require-dev": {
27+
"phpunit/phpunit": "10.2.4",
28+
"mockery/mockery": "1.6.2"
29+
},
30+
"autoload": {
31+
"psr-4": {
32+
"ecoAPM\\LibYear\\": "src/",
33+
"ecoAPM\\LibYear\\Tests\\": "tests/"
34+
}
35+
}
3536
}

composer.lock

Lines changed: 112 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/App.php

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace ecoAPM\LibYear;
44

55
use cli\Table;
6+
use Garden\Cli\Cli;
7+
use Exception;
8+
use InvalidArgumentException;
69

710
class App
811
{
@@ -11,14 +14,21 @@ class App
1114

1215
/** @var resource */
1316
private $output;
17+
private Cli $cli;
1418

1519
/**
20+
* @param Cli $cli
1621
* @param Calculator $calculator
1722
* @param ComposerFile $composer
1823
* @param resource $output
1924
*/
20-
public function __construct(Calculator $calculator, ComposerFile $composer, $output)
25+
public function __construct(Cli $cli, Calculator $calculator, ComposerFile $composer, $output)
2126
{
27+
$this->cli = $cli->description('libyear: a simple measure of dependency freshness -- calculates the total number of years behind their respective newest versions for all dependencies listed in a composer.json file.')
28+
->opt('quiet:q', 'only display outdated dependencies', false, 'boolean')
29+
->opt('update:u', 'update composer.json with newest versions', false, 'boolean')
30+
->opt('verbose:v', 'display network debug information', false, 'boolean')
31+
->arg('path', 'the directory containing composer.json and composer.lock files');
2232
$this->calculator = $calculator;
2333
$this->composer = $composer;
2434
$this->output = $output;
@@ -29,18 +39,21 @@ public function __construct(Calculator $calculator, ComposerFile $composer, $out
2939
*/
3040
public function run(array $args): void
3141
{
32-
if (in_array('-h', $args) || in_array('--help', $args))
33-
{
34-
$this->showHelp();
42+
try {
43+
$arguments = $this->cli->parse($args, false);
44+
} catch (Exception $e) {
45+
$error = $e->getMessage();
46+
fwrite($this->output, "{$error}\n");
47+
if (!str_starts_with($error, "usage: ")) {
48+
$this->showHelp();
49+
}
3550
return;
3651
}
3752

38-
$quiet_mode = in_array('-q', $args) || in_array('--quiet', $args);
39-
$update_mode = in_array('-u', $args) || in_array('--update', $args);
40-
$verbose_mode = in_array('-v', $args) || in_array('--verbose', $args);
41-
$known_options = ['-q', '--quiet', '-u', '--update', '-v', '--verbose'];
42-
$other_args = array_filter(array_slice($args, 1), fn ($a) => !in_array($a, $known_options));
43-
$dir = !empty($other_args) ? array_values($other_args)[0] : '.';
53+
$quiet_mode = $arguments->getOpt('quiet') !== null;
54+
$update_mode = $arguments->getOpt('update') !== null;
55+
$verbose_mode = $arguments->getOpt('verbose') !== null;
56+
$dir = $arguments->getArg('path') ?? '.';
4457

4558
$real_dir = realpath($dir);
4659
fwrite($this->output, "Gathering information for $real_dir...\n");
@@ -58,6 +71,7 @@ public function run(array $args): void
5871
if ($update_mode) {
5972
$this->composer->update($dir, $dependencies);
6073
fwrite($this->output, "composer.json updated\n");
74+
fwrite($this->output, "A manual run of \"composer update\" is required to actually update dependencies\n");
6175
}
6276
}
6377

@@ -83,10 +97,10 @@ private function showTable(array $dependencies): void
8397
fn (Dependency $dependency): array => [
8498
$dependency->name,
8599
$dependency->current_version->version_number,
86-
isset($dependency->current_version->released) ? $dependency->current_version->released->format('Y-m-d') : "",
87-
isset($dependency->newest_version->version_number) ? $dependency->newest_version->version_number : "",
88-
isset($dependency->newest_version->released) ? $dependency->newest_version->released->format('Y-m-d') : "",
89-
$dependency->getLibyearsBehind() !== null ? number_format($dependency->getLibyearsBehind(), 2) : ""
100+
isset($dependency->current_version->released) ? $dependency->current_version->released->format('Y-m-d') : '',
101+
isset($dependency->newest_version->version_number) ? $dependency->newest_version->version_number : '',
102+
isset($dependency->newest_version->released) ? $dependency->newest_version->released->format('Y-m-d') : '',
103+
$dependency->getLibyearsBehind() !== null ? number_format($dependency->getLibyearsBehind(), 2) : ''
90104
],
91105
$dependencies
92106
)
@@ -98,25 +112,14 @@ private function showTable(array $dependencies): void
98112
}
99113
}
100114

101-
private function showHelp(): void
115+
/**
116+
* @return void
117+
*/
118+
public function showHelp(): void
102119
{
103-
$output = 'libyear: a simple measure of dependency freshness' . PHP_EOL
104-
. PHP_EOL
105-
. 'Calculates the total number of years behind their respective newest versions for all'
106-
. ' dependencies listed in composer.json.' . PHP_EOL
107-
. PHP_EOL
108-
. 'Usage: libyear <path> [-q|--quiet] [-u|--update] [-v|--verbose]' . PHP_EOL
109-
. PHP_EOL
110-
. 'Arguments:' . PHP_EOL
111-
. '- path (required) the directory containing composer.json and composer.lock files' . PHP_EOL
112-
. PHP_EOL
113-
. 'Options:' . PHP_EOL
114-
. '--help (-h) show this message and exit' . PHP_EOL
115-
. '--quiet (-q) only display outdated dependencies' . PHP_EOL
116-
. '--update (-u) update composer.json with newest versions' . PHP_EOL
117-
. '--verbose (-v) display network debug information' . PHP_EOL
118-
. PHP_EOL;
119-
120-
fwrite($this->output, $output);
120+
ob_start();
121+
$this->cli->writeHelp();
122+
$output = ob_get_clean();
123+
fwrite($this->output, $output);
121124
}
122125
}

src/Factory.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
namespace ecoAPM\LibYear;
44

55
use cli\progress\Bar;
6+
use Garden\Cli\Cli;
67
use GuzzleHttp\Client;
78

89
class Factory
910
{
1011
public static function app(): App
1112
{
13+
$cli = new Cli();
14+
1215
$fs = new FileSystem();
1316
$file = new ComposerFile($fs, STDERR);
1417

@@ -17,6 +20,6 @@ public static function app(): App
1720

1821
$progress = new Bar("Checking dependencies...", 0);
1922
$calculator = new Calculator($file, $api, $progress);
20-
return new App($calculator, $file, STDOUT);
23+
return new App($cli, $calculator, $file, STDOUT);
2124
}
2225
}

0 commit comments

Comments
 (0)