-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathOptionPrinter.php
81 lines (72 loc) · 2.2 KB
/
OptionPrinter.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
<?php
/**
* This file is part of the GetOptionKit package.
*
* (c) Yo-An Lin <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace CLIFramework;
use GetOptionKit\OptionCollection;
use GetOptionKit\Option;
use GetOptionKit\OptionPrinter\OptionPrinter as OptionPrinterInterface;
use CLIFramework\Formatter;
class OptionPrinter implements OptionPrinterInterface
{
public $screenWidth = 78;
public $formatter;
public function __construct()
{
$this->formatter = new Formatter;
}
/**
* Render readable spec
*/
public function renderOption(Option $opt)
{
$columns = array();
if ($opt->short) {
$columns[] = $this->formatter->format(sprintf('-%s', $opt->short), 'strong_white')
. $this->renderOptionValueHint($opt, false);
}
if ($opt->long) {
$columns[] = $this->formatter->format(sprintf('--%s', $opt->long), 'strong_white')
. $this->renderOptionValueHint($opt, true);
}
return join(', ', $columns);
}
public function renderOptionValueHint(Option $opt, $assign = true)
{
$n = 'value';
if ($opt->valueName) {
$n = $opt->valueName;
} elseif ($opt->isa) {
$n = $opt->isa;
}
if ($opt->isRequired()) {
return sprintf('%s<%s>', $assign ? '=' : ' ', $this->formatter->format($n, 'underline'));
} elseif ($opt->isOptional()) {
return sprintf('%s[<%s>]', $assign ? '=' : ' ', $this->formatter->format($n, 'underline'));
}
return '';
}
/**
* render option descriptions
*
* @return string output
*/
public function render(OptionCollection $options)
{
# echo "* Available options:\n";
$lines = array();
foreach ($options as $option) {
$c1 = $this->renderOption($option);
$lines[] = "\t" . $c1;
$lines[] = wordwrap("\t\t" . $option->desc, $this->screenWidth, "\n\t\t"); # wrap text
$lines[] = "";
}
return join("\n", $lines);
}
}