forked from wp-cli/php-cli-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.php
More file actions
170 lines (157 loc) · 5.4 KB
/
cli.php
File metadata and controls
170 lines (157 loc) · 5.4 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
* PHP Command Line Tools
*
* This source file is subject to the MIT license that is bundled
* with this package in the file LICENSE.
*
* @author James Logsdon <dwarf@girsbrain.org>
* @copyright 2010 James Logsdom (http://girsbrain.org)
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace cli;
/**
* Registers a basic auto loader for the `cli` namespace.
*/
function register_autoload() {
spl_autoload_register( function($class) {
// Only attempt to load classes in our namespace
if( substr( $class, 0, 4 ) !== 'cli\\' ) {
return;
}
$base = dirname( __DIR__ ) . DIRECTORY_SEPARATOR;
$path = $base . str_replace( '\\', DIRECTORY_SEPARATOR, $class ) . '.php';
if( is_file( $path ) ) {
require_once $path;
}
} );
}
/**
* Handles rendering strings. If extra scalar arguments are given after the `$msg`
* the string will be rendered with `sprintf`. If the second argument is an `array`
* then each key in the array will be the placeholder name. Placeholders are of the
* format {:key}.
*
* @param string $msg The message to render.
* @param mixed ... Either scalar arguments or a single array argument.
* @return string The rendered string.
*/
function render( $msg ) {
$args = func_get_args();
return call_user_func_array( array( '\\cli\\Streams', 'render' ), $args );
}
/**
* Shortcut for printing to `STDOUT`. The message and parameters are passed
* through `sprintf` before output.
*
* @param string $msg The message to output in `printf` format.
* @param mixed ... Either scalar arguments or a single array argument.
* @return void
* @see \cli\render()
*/
function out( $msg ) {
$args = func_get_args();
call_user_func_array( array( '\\cli\\Streams', 'out' ), $args );
}
/**
* Pads `$msg` to the width of the shell before passing to `cli\out`.
*
* @param string $msg The message to pad and pass on.
* @param mixed ... Either scalar arguments or a single array argument.
* @return void
* @see cli\out()
*/
function out_padded( $msg ) {
$args = func_get_args();
call_user_func_array( array( '\\cli\\Streams', 'out_padded' ), $args );
}
/**
* Prints a message to `STDOUT` with a newline appended. See `\cli\out` for
* more documentation.
*
* @see cli\out()
*/
function line( $msg = '' ) {
// func_get_args is empty if no args are passed even with the default above.
$args = func_get_args();
if( $args ) {
call_user_func_array( array( '\\cli\Streams', 'line' ), $args );
} else {
\cli\Streams::line();
}
}
/**
* Shortcut for printing to `STDERR`. The message and parameters are passed
* through `sprintf` before output.
*
* @param string $msg The message to output in `printf` format. With no string,
* a newline is printed.
* @param mixed ... Either scalar arguments or a single array argument.
* @return void
*/
function err( $msg = '' ) {
// func_get_args is empty if no args are passed even with the default above.
$args = func_get_args();
if( $args ) {
call_user_func_array( array( '\\cli\Streams', 'err' ), $args );
} else {
\cli\Streams::err();
}
}
/**
* Takes input from `STDIN` in the given format. If an end of transmission
* character is sent (^D), an exception is thrown.
*
* @param string $format A valid input format. See `fscanf` for documentation.
* If none is given, all input up to the first newline
* is accepted.
* @return string The input with whitespace trimmed.
* @throws \Exception Thrown if ctrl-D (EOT) is sent as input.
*/
function input( $format = null ) {
return \cli\Streams::input( $format );
}
/**
* Displays an input prompt. If no default value is provided the prompt will
* continue displaying until input is received.
*
* @param string $question The question to ask the user.
* @param string $default A default value if the user provides no input.
* @param string $marker A string to append to the question and default value
* on display.
* @return string The users input.
* @see cli\input()
*/
function prompt( $question, $default = false, $marker = ': ' ) {
return \cli\Streams::prompt( $question, $default, $marker );
}
/**
* Presents a user with a multiple choice question, useful for 'yes/no' type
* questions (which this function defaults too).
*
* @param string $question The question to ask the user.
* @param string $valid A string of characters allowed as a response. Case
* is ignored.
* @param string $default The default choice. NULL if a default is not allowed.
* @return string The users choice.
* @see cli\prompt()
*/
function choose( $question, $choice = 'yn', $default = 'n' ) {
return \cli\Streams::choose( $question, $choice, $default );
}
/**
* Displays an array of strings as a menu where a user can enter a number to
* choose an option. The array must be a single dimension with either strings
* or objects with a `__toString()` method.
*
* @param array $items The list of items the user can choose from.
* @param string $default The index of the default item.
* @param string $title The message displayed to the user when prompted.
* @return string The index of the chosen item.
* @see cli\line()
* @see cli\input()
* @see cli\err()
*/
function menu( $items, $default = false, $title = 'Choose an item' ) {
return \cli\Streams::menu( $items, $default, $title );
}