This repository was archived by the owner on Feb 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.php
More file actions
112 lines (96 loc) · 2.46 KB
/
cli.php
File metadata and controls
112 lines (96 loc) · 2.46 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
namespace Cli;
use function Argv\{cleanArguments, getFlags, getCommand, isCommandCall};
/**
* Helper function to create a simple CLI application
* @param array $arguments
* @param string $helpMessage
* @param array $flagAliases
* @param bool|boolean $showHelp
* @param array $outputStreams
* @return null
*/
function cli(
array $arguments,
string $helpMessage,
array $flagAliases = [],
bool $showHelp = true,
array $outputStreams = [STDOUT]
) {
return new class($arguments, $helpMessage, $flagAliases, $showHelp, $outputStreams) {
/**
* Contains the $argv arguments, cleaned (=removed script name)
* @var array
*/
public $arguments = [];
/**
* Contains the help message shown on call or if no arguments are provided
* @var string
*/
public $helpMessage = '';
/**
* Contents of the composer.json file, if available
* @var \stdClass
*/
public $composer;
/**
* Contains all the flags based on the provided arguments
* @var class@anonymous
*/
public $flags;
/**
* Contains the information if given argument call is a command call
* @var boolean
*/
public $isCommand = false;
/**
* Contains command name if provided by arguments
* @var string
*/
public $commandName = '';
/**
* Contains an array of output streams on which the print command will write
* @var array
*/
protected $outputStreams;
public function __construct(
array $arguments,
string $helpMessage,
array $flagAliases,
bool $showHelp = false,
array $outputStreams = [STDOUT]
) {
$cleanedArguments = cleanArguments($arguments);
$this->isCommand = isCommandCall($cleanedArguments);
if ($this->isCommand) {
$this->commandName = getCommand($cleanedArguments);
}
$this->arguments = $cleanedArguments;
$this->helpMessage = $helpMessage;
$this->flags = getFlags($cleanedArguments, $flagAliases);
$this->composer = @json_decode(@file_get_contents(getcwd() . '/composer.json')) ?? new \stdClass();
$this->outputStreams = $outputStreams;
if (
(
count($cleanedArguments) < 1
&& $showHelp === true
)
|| $this->flags->help !== false
) {
$this->print($helpMessage);
}
}
/**
* General purpose logging method to std out
* @param string $contents
* @return class@anonymous
*/
public function print(string $contents)
{
foreach ($this->outputStreams as $stream) {
fwrite($stream, $contents);
}
return $this;
}
};
}