Skip to content

Commit 52cd81f

Browse files
Add help text
1 parent 4e6adf1 commit 52cd81f

File tree

4 files changed

+83
-29
lines changed

4 files changed

+83
-29
lines changed

README.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,18 @@ Note that PHP for Windows does not include CA certificates, so you'll need to in
3737

3838
## Usage
3939

40-
`vendor/bin/libyear < path to project > [-q|--quiet] [-v|--verbose]`
40+
`vendor/bin/libyear <path> [-q|--quiet] [-v|--verbose]`
4141

42-
(or `php path/to/libyear.phar < path to project > [-q|--quiet] [-v|--verbose]` for the PHAR version)
42+
(or `php path/to/libyear.phar <path> [-q|--quiet] [-v|--verbose]` for the PHAR version)
4343

4444
Arguments:
45-
- `path to project`: required, directory containing `composer.json` and `composer.lock` files
46-
- `-q`, `--quiet`: optional, quiet mode will only output libraries which are not up-to-date (that is, where "Libyears Behind" > 0)
47-
- `-v`, `--verbose`: optional, verbose mode will output processing details like when a library isn't found in a repository
45+
- `path`: required, directory containing `composer.json` and `composer.lock` files
46+
47+
Options:
48+
- `-h`, `--help`: show help text and exit without checking dependencies
49+
- `-q`, `--quiet`: quiet mode will only output libraries which are not up-to-date (that is, where "Libyears Behind" > 0)
50+
- `-u`, `--update`: update mode will write the latest version info to your `composer.json` file (note that you'll still need to run `composer update` to actually update your local dependencies)
51+
- `-v`, `--verbose`: verbose mode will output processing details like when a library isn't found in a repository
4852

4953
## Contributing
5054

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ecoapm/libyear",
3-
"version": "2.2.1",
3+
"version": "2.3.0",
44
"description": "A simple measure of software dependency freshness",
55
"homepage": "https://libyear.com",
66
"readme": "README.md",

src/App.php

+55-22
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,25 @@ public function __construct(Calculator $calculator, ComposerFile $composer, $out
2929
*/
3030
public function run(array $args): void
3131
{
32+
if (in_array('-h', $args) || in_array('--help', $args))
33+
{
34+
$this->showHelp();
35+
return;
36+
}
37+
3238
$quiet_mode = in_array('-q', $args) || in_array('--quiet', $args);
3339
$update_mode = in_array('-u', $args) || in_array('--update', $args);
3440
$verbose_mode = in_array('-v', $args) || in_array('--verbose', $args);
35-
$other_args = array_filter(array_slice($args, 1), fn ($a) => !in_array($a, ['-q', '--quiet', '-u', '--update', '-v', '--verbose']));
36-
$dir = $other_args ? array_values($other_args)[0] : '.';
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] : '.';
3744

3845
$real_dir = realpath($dir);
3946
fwrite($this->output, "Gathering information for $real_dir...\n");
4047

4148
$dependencies = $this->getDependencies($dir, $quiet_mode, $verbose_mode);
42-
43-
$table = new Table(
44-
['Package', 'Current Version', 'Released', 'Newest Version', 'Released', 'Libyears Behind'],
45-
array_map(
46-
fn (Dependency $dependency): array => [
47-
$dependency->name,
48-
$dependency->current_version->version_number,
49-
isset($dependency->current_version->released) ? $dependency->current_version->released->format('Y-m-d') : "",
50-
isset($dependency->newest_version->version_number) ? $dependency->newest_version->version_number : "",
51-
isset($dependency->newest_version->released) ? $dependency->newest_version->released->format('Y-m-d') : "",
52-
$dependency->getLibyearsBehind() !== null ? number_format($dependency->getLibyearsBehind(), 2) : ""
53-
],
54-
$dependencies
55-
)
56-
);
57-
5849
if (!empty($dependencies)) {
59-
$rows = $table->getDisplayLines();
60-
foreach ($rows as $row) {
61-
fwrite($this->output, $row . "\n");
62-
}
50+
$this->showTable($dependencies);
6351
}
6452

6553
$total = Calculator::getTotalLibyearsBehind($dependencies);
@@ -86,4 +74,49 @@ private function getDependencies(string $dir, bool $quiet_mode, bool $verbose_mo
8674
? array_filter($dependencies, fn (Dependency $dependency): bool => $dependency->getLibyearsBehind() > 0)
8775
: $dependencies;
8876
}
77+
78+
private function showTable(array $dependencies): void
79+
{
80+
$table = new Table(
81+
['Package', 'Current Version', 'Released', 'Newest Version', 'Released', 'Libyears Behind'],
82+
array_map(
83+
fn (Dependency $dependency): array => [
84+
$dependency->name,
85+
$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) : ""
90+
],
91+
$dependencies
92+
)
93+
);
94+
95+
$rows = $table->getDisplayLines();
96+
foreach ($rows as $row) {
97+
fwrite($this->output, $row . "\n");
98+
}
99+
}
100+
101+
private function showHelp(): void
102+
{
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);
121+
}
89122
}

tests/AppTest.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,24 @@ private static function calculator(): Calculator
3232
]);
3333
}
3434

35-
public function testShowsAllDependenciesByDefaut()
35+
public function testCanDisplayHelpText()
36+
{
37+
//arrange
38+
$composer = Mockery::mock(ComposerFile::class);
39+
$output = fopen('php://memory', 'a+');
40+
$app = new App(self::calculator(), $composer, $output);
41+
42+
//act
43+
$app->run(['libyear', '--help']);
44+
45+
//assert
46+
fseek($output, 0);
47+
$console = stream_get_contents($output);
48+
$this->assertStringContainsString('Arguments:', $console);
49+
$this->assertStringContainsString('Options:', $console);
50+
}
51+
52+
public function testShowsAllDependenciesByDefault()
3653
{
3754
//arrange
3855
$composer = Mockery::mock(ComposerFile::class);

0 commit comments

Comments
 (0)