Skip to content

Commit f5174d1

Browse files
Fixes #9: Support --help in addition to 'help'
1 parent ed1fe8f commit f5174d1

File tree

4 files changed

+80
-39
lines changed

4 files changed

+80
-39
lines changed

.travis.yml

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ branches:
77
- /^[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+.*$/
88

99
php:
10-
- 5.3
11-
- 5.4
12-
- 5.5
13-
- 5.6
10+
- 7.1
1411
- 7.0
12+
- 5.6
13+
- 5.5
14+
- 5.4
15+
- 5.3
1516

1617
sudo: false
1718

@@ -24,8 +25,7 @@ before_script:
2425
- composer install
2526

2627
script:
27-
- vendor/bin/phpcs --standard=PSR2 -n src
28-
- vendor/bin/phpunit
28+
- composer test
2929

3030
after_success:
3131
- travis_retry php vendor/bin/coveralls -v

composer.json

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
"satooshi/php-coveralls": "^1.0",
1919
"squizlabs/php_codesniffer": "2.*"
2020
},
21+
"scripts": {
22+
"test": [ "@phpunit", "@cs" ],
23+
"phpunit": "phpunit --colors=always",
24+
"cs": "phpcs --standard=PSR2 -n src",
25+
"cbf": "phpcbf --standard=PSR2 -n src"
26+
},
2127
"bin": ["bin/cgr"],
2228
"autoload": {
2329
"psr-4": {"Consolidation\\Cgr\\": "src"}

help/help

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
The 'cgr' tool is a "safer" alternative to 'composer global require'.
2+
Installing projects with cgr helps avoid dependency conflicts between
3+
different tools. Use 'cgr' wherever 'composer global require' is recommended.
4+
5+
Examples:
6+
7+
Install a project:
8+
------------------
9+
$ cgr drush/drush
10+
11+
Display the info of a project:
12+
-----------------------------
13+
$ cgr info drush/drush
14+
15+
Display the info of all projects installed via 'cgr':
16+
----------------------------------------------------
17+
$ cgr info
18+
19+
Update a project:
20+
-----------------
21+
$ cgr update drush/drush
22+
23+
Update all projects installed via 'cgr':
24+
----------------------------------------
25+
$ cgr update
26+
27+
Remove a project:
28+
-----------------
29+
$ cgr remove drush/drush
30+
31+
For more information, see: https://github.com/consolidation/cgr
32+

src/Application.php

+36-33
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public function run($argv, $home)
3030

3131
list($argv, $options) = $this->parseOutOurOptions($argv, $optionDefaultValues);
3232

33-
if (reset($argv) == 'help') {
34-
$this->help($argv);
35-
return 0;
33+
$helpArg = $this->getHelpArgValue($argv);
34+
if (!empty($helpArg)) {
35+
return $this->help($helpArg);
3636
}
3737

3838
$commandList = $this->separateProjectAndGetCommandList($argv, $home, $options);
@@ -42,44 +42,47 @@ public function run($argv, $home)
4242
return $this->runCommandList($commandList, $options);
4343
}
4444

45-
public function help($argv)
45+
/**
46+
* Returns the first argument after `help`, or the
47+
* first argument if `--help` is present. Otherwise,
48+
* returns an empty string.
49+
*/
50+
public function getHelpArgValue($argv)
4651
{
47-
// Future: support 'help <command>'?
48-
print <<<EOT
49-
The 'cgr' tool is a "safer" alternative to 'composer global require'.
50-
Installing projects with cgr helps avoid dependency conflicts between
51-
different tools. Use 'cgr' wherever 'composer global require' is recommended.
52-
53-
Examples:
54-
55-
Install a project:
56-
------------------
57-
$ cgr drush/drush
52+
$hasHelp = false;
53+
$helpArg = '';
5854

59-
Display the info of a project:
60-
-----------------------------
61-
$ cgr info drush/drush
62-
63-
Display the info of all projects installed via 'cgr':
64-
----------------------------------------------------
65-
$ cgr info
55+
foreach ($argv as $arg) {
56+
if (($arg == 'help') || ($arg == '--help') || ($arg == '-h')) {
57+
$hasHelp = true;
58+
} elseif (($arg[0] != '-') && empty($helpArg)) {
59+
$helpArg = $arg;
60+
}
61+
}
6662

67-
Update a project:
68-
-----------------
69-
$ cgr update drush/drush
63+
if (!$hasHelp) {
64+
return false;
65+
}
7066

71-
Update all projects installed via 'cgr':
72-
----------------------------------------
73-
$ cgr update
67+
if (empty($helpArg)) {
68+
return 'help';
69+
}
7470

75-
Remove a project:
76-
-----------------
77-
$ cgr remove drush/drush
71+
return $helpArg;
72+
}
7873

79-
For more information, see: https://github.com/consolidation/cgr
74+
public function help($helpArg)
75+
{
76+
$helpFile = dirname(__DIR__) . '/help/' . $helpArg;
8077

78+
if (!file_exists($helpFile)) {
79+
print "No help available for '$helpArg'\n";
80+
return 1;
81+
}
8182

82-
EOT;
83+
$helpContents = file_get_contents($helpFile);
84+
print $helpContents;
85+
return 0;
8386
}
8487

8588
/**

0 commit comments

Comments
 (0)