|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Cocotte\Console\Documentation; |
|
4
|
|
|
|
|
5
|
|
|
use Cocotte\Console\StyledInputOption; |
|
6
|
|
|
use Symfony\Component\Console\Formatter\OutputFormatter; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
9
|
|
|
|
|
10
|
|
|
final class OptionDescriber |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var OutputInterface |
|
15
|
|
|
*/ |
|
16
|
|
|
private $output; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(OutputInterface $output) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->output = $output; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function describe(InputOption $option) |
|
24
|
|
|
{ |
|
25
|
|
|
$name = '--'.$option->getName(); |
|
26
|
|
|
if ($option->getShortcut()) { |
|
27
|
|
|
$name .= '|-'.implode('|-', explode('|', $option->getShortcut())).''; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
$emphasis = $this->optionEmphasis($option); |
|
31
|
|
|
|
|
32
|
|
|
$description = $this->removeDecoration($option->getDescription()); |
|
33
|
|
|
$this->write( |
|
34
|
|
|
'#### `'.$name.'`'."\n\n" |
|
35
|
|
|
.$this->formatOptionDescription($description, $emphasis) |
|
36
|
|
|
.'* Accept value: '.$this->optionValue($option)."\n" |
|
37
|
|
|
.'* Is value required: '.$this->optionValueRequired($option)."\n" |
|
38
|
|
|
.'* Is multiple: '.$this->optionIsMultiple($option)."\n" |
|
39
|
|
|
.'* Default: `'.str_replace("\n", '', var_export($option->getDefault(), true)).'`' |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param InputOption $option |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
private function optionValue(InputOption $option): string |
|
48
|
|
|
{ |
|
49
|
|
|
return ($option->acceptValue() ? 'yes' : 'no'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param InputOption $option |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
private function optionValueRequired(InputOption $option): string |
|
57
|
|
|
{ |
|
58
|
|
|
return ($option->isValueRequired() ? 'yes' : 'no'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param InputOption $option |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
|
|
private function optionIsMultiple(InputOption $option): string |
|
66
|
|
|
{ |
|
67
|
|
|
return ($option->isArray() ? 'yes' : 'no'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param InputOption $option |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
private function optionEmphasis(InputOption $option): string |
|
75
|
|
|
{ |
|
76
|
|
|
return $option instanceof StyledInputOption ? '##### ' : ''; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
private function removeDecoration(string $string): string |
|
80
|
|
|
{ |
|
81
|
|
|
$f = new OutputFormatter(); |
|
82
|
|
|
|
|
83
|
|
|
return $f->format($string); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Writes content to output. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $content |
|
90
|
|
|
* @param bool $decorated |
|
91
|
|
|
*/ |
|
92
|
|
|
private function write($content, $decorated = false) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->output->write($content, |
|
95
|
|
|
false, |
|
96
|
|
|
$decorated ? OutputInterface::OUTPUT_NORMAL : OutputInterface::OUTPUT_RAW); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param $description |
|
101
|
|
|
* @param $emphasis |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
|
|
private function formatOptionDescription($description, $emphasis): string |
|
105
|
|
|
{ |
|
106
|
|
|
return ($description ? $emphasis.preg_replace('/\s*[\r\n]\s*/', "\n", $description)."\n\n" : ''); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
} |