-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEchoTest.php
executable file
·96 lines (80 loc) · 2.54 KB
/
EchoTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
namespace Diversen;
class EchoTest
{
private $utils;
public function __construct()
{
$this->utils = new \Diversen\Cli\Utils();
}
/**
* Get command definition
* 'usage', 'options', 'main_options', 'arguments'
*/
public function getCommand()
{
return [
// Usage of the command
'usage' => "Command reads a file and output it in upper or lower case",
// Options for only this command
'options' => [
'--up' => 'Will put string in uppercase',
'--low' => 'Will put string in lowercase'
],
// 'cast' => [
// 'up' => 'int', // Cast to int, bool, float. Default is string
// ],
// Main options, which other commands may have access to
'main_options' => [
'--main' => 'Test with a main option'
],
// Are there any arguments and what are they used for.
// This is only for displaying help. Any number of arguments can be
'arguments' => [
'File' => 'Read from a file and output to stdout. You can also pipe input to the command',
],
// Set a default command if none if given
// php demos/example --up README.md
// Instead of:
// php demos/example echo --up README.md
// Then set 'is_default' to true
// 'is_default' => true,
];
}
private function getFileContents($file)
{
if (!file_exists($file)) {
return false;
}
if (!file_exists($file)) {
return false;
}
$input = file_get_contents($file);
return $input;
}
/**
* Run the command and return the result
* @param Diversen\ParseArgv $args
*/
public function runCommand(\Diversen\ParseArgv $args)
{
$input = $this->utils->readStdin();
if (empty($input)) {
$file = $args->getArgument(0);
$input = $this->getFileContents($file);
if (!$input) {
echo "No content was piped to STDIN or file was not specified" . PHP_EOL;
return 12;
}
}
if ($args->getOption('up')) {
$output = strtoupper($input) . PHP_EOL;
} else if ($args->getOption('low')) {
$output = strtolower($input) . PHP_EOL;
} else {
$output = $input . PHP_EOL;
}
echo $output;
return 0;
}
}