@@ -32,16 +32,19 @@ class TextDescriptor extends Descriptor
32
32
protected function describeInputArgument (InputArgument $ argument , array $ options = array ())
33
33
{
34
34
if (null !== $ argument ->getDefault () && (!is_array ($ argument ->getDefault ()) || count ($ argument ->getDefault ()))) {
35
- $ default = sprintf ('<comment> ( default: %s) </comment> ' , $ this ->formatDefaultValue ($ argument ->getDefault ()));
35
+ $ default = sprintf ('<comment> [ default: %s] </comment> ' , $ this ->formatDefaultValue ($ argument ->getDefault ()));
36
36
} else {
37
37
$ default = '' ;
38
38
}
39
39
40
- $ nameWidth = isset ($ options ['name_width ' ]) ? $ options ['name_width ' ] : strlen ($ argument ->getName ());
40
+ $ totalWidth = isset ($ options ['total_width ' ]) ? $ options ['total_width ' ] : strlen ($ argument ->getName ());
41
+ $ spacingWidth = $ totalWidth - strlen ($ argument ->getName ()) + 2 ;
41
42
42
- $ this ->writeText (sprintf (" <info>%- $ {nameWidth} s</info> %s%s " ,
43
+ $ this ->writeText (sprintf (" <info>%s</info>%s %s%s " ,
43
44
$ argument ->getName (),
44
- str_replace ("\n" , "\n" .str_repeat (' ' , $ nameWidth + 2 ), $ argument ->getDescription ()),
45
+ str_repeat (' ' , $ spacingWidth ),
46
+ // + 17 = 2 spaces + <info> + </info> + 2 spaces
47
+ preg_replace ('/\s*\R\s*/ ' , PHP_EOL .str_repeat (' ' , $ totalWidth + 17 ), $ argument ->getDescription ()),
45
48
$ default
46
49
), $ options );
47
50
}
@@ -52,18 +55,33 @@ protected function describeInputArgument(InputArgument $argument, array $options
52
55
protected function describeInputOption (InputOption $ option , array $ options = array ())
53
56
{
54
57
if ($ option ->acceptValue () && null !== $ option ->getDefault () && (!is_array ($ option ->getDefault ()) || count ($ option ->getDefault ()))) {
55
- $ default = sprintf ('<comment> ( default: %s) </comment> ' , $ this ->formatDefaultValue ($ option ->getDefault ()));
58
+ $ default = sprintf ('<comment> [ default: %s] </comment> ' , $ this ->formatDefaultValue ($ option ->getDefault ()));
56
59
} else {
57
60
$ default = '' ;
58
61
}
59
62
60
- $ nameWidth = isset ($ options ['name_width ' ]) ? $ options ['name_width ' ] : strlen ($ option ->getName ());
61
- $ nameWithShortcutWidth = $ nameWidth - strlen ($ option ->getName ()) - 2 ;
63
+ $ value = '' ;
64
+ if ($ option ->acceptValue ()) {
65
+ $ value = '= ' .strtoupper ($ option ->getName ());
62
66
63
- $ this ->writeText (sprintf (" <info>%s</info> %- $ {nameWithShortcutWidth}s%s%s%s " ,
64
- '-- ' .$ option ->getName (),
65
- $ option ->getShortcut () ? sprintf ('(-%s) ' , $ option ->getShortcut ()) : '' ,
66
- str_replace ("\n" , "\n" .str_repeat (' ' , $ nameWidth + 2 ), $ option ->getDescription ()),
67
+ if ($ option ->isValueOptional ()) {
68
+ $ value = '[ ' .$ value .'] ' ;
69
+ }
70
+ }
71
+
72
+ $ totalWidth = isset ($ options ['total_width ' ]) ? $ options ['total_width ' ] : $ this ->calculateTotalWidthForOptions (array ($ option ));
73
+ $ synopsis = sprintf ('%s%s ' ,
74
+ $ option ->getShortcut () ? sprintf ('-%s, ' , $ option ->getShortcut ()) : ' ' ,
75
+ sprintf ('--%s%s ' , $ option ->getName (), $ value )
76
+ );
77
+
78
+ $ spacingWidth = $ totalWidth - strlen ($ synopsis ) + 2 ;
79
+
80
+ $ this ->writeText (sprintf (" <info>%s</info>%s%s%s%s " ,
81
+ $ synopsis ,
82
+ str_repeat (' ' , $ spacingWidth ),
83
+ // + 17 = 2 spaces + <info> + </info> + 2 spaces
84
+ preg_replace ('/\s*\R\s*/ ' , "\n" .str_repeat (' ' , $ totalWidth + 17 ), $ option ->getDescription ()),
67
85
$ default ,
68
86
$ option ->isArray () ? '<comment> (multiple values allowed)</comment> ' : ''
69
87
), $ options );
@@ -74,24 +92,16 @@ protected function describeInputOption(InputOption $option, array $options = arr
74
92
*/
75
93
protected function describeInputDefinition (InputDefinition $ definition , array $ options = array ())
76
94
{
77
- $ nameWidth = 0 ;
78
- foreach ($ definition ->getOptions () as $ option ) {
79
- $ nameLength = strlen ($ option ->getName ()) + 2 ;
80
- if ($ option ->getShortcut ()) {
81
- $ nameLength += strlen ($ option ->getShortcut ()) + 3 ;
82
- }
83
- $ nameWidth = max ($ nameWidth , $ nameLength );
84
- }
95
+ $ totalWidth = $ this ->calculateTotalWidthForOptions ($ definition ->getOptions ());
85
96
foreach ($ definition ->getArguments () as $ argument ) {
86
- $ nameWidth = max ($ nameWidth , strlen ($ argument ->getName ()));
97
+ $ totalWidth = max ($ totalWidth , strlen ($ argument ->getName ()));
87
98
}
88
- ++$ nameWidth ;
89
99
90
100
if ($ definition ->getArguments ()) {
91
101
$ this ->writeText ('<comment>Arguments:</comment> ' , $ options );
92
102
$ this ->writeText ("\n" );
93
103
foreach ($ definition ->getArguments () as $ argument ) {
94
- $ this ->describeInputArgument ($ argument , array_merge ($ options , array ('name_width ' => $ nameWidth )));
104
+ $ this ->describeInputArgument ($ argument , array_merge ($ options , array ('total_width ' => $ totalWidth )));
95
105
$ this ->writeText ("\n" );
96
106
}
97
107
}
@@ -101,11 +111,20 @@ protected function describeInputDefinition(InputDefinition $definition, array $o
101
111
}
102
112
103
113
if ($ definition ->getOptions ()) {
114
+ $ laterOptions = array ();
115
+
104
116
$ this ->writeText ('<comment>Options:</comment> ' , $ options );
105
- $ this ->writeText ("\n" );
106
117
foreach ($ definition ->getOptions () as $ option ) {
107
- $ this ->describeInputOption ($ option , array_merge ($ options , array ('name_width ' => $ nameWidth )));
118
+ if (strlen ($ option ->getShortcut ()) > 1 ) {
119
+ $ laterOptions [] = $ option ;
120
+ continue ;
121
+ }
108
122
$ this ->writeText ("\n" );
123
+ $ this ->describeInputOption ($ option , array_merge ($ options , array ('total_width ' => $ totalWidth )));
124
+ }
125
+ foreach ($ laterOptions as $ option ) {
126
+ $ this ->writeText ("\n" );
127
+ $ this ->describeInputOption ($ option , array_merge ($ options , array ('total_width ' => $ totalWidth )));
109
128
}
110
129
}
111
130
}
@@ -115,27 +134,26 @@ protected function describeInputDefinition(InputDefinition $definition, array $o
115
134
*/
116
135
protected function describeCommand (Command $ command , array $ options = array ())
117
136
{
118
- $ command ->getSynopsis ();
137
+ $ command ->getSynopsis (true );
138
+ $ command ->getSynopsis (false );
119
139
$ command ->mergeApplicationDefinition (false );
120
140
121
141
$ this ->writeText ('<comment>Usage:</comment> ' , $ options );
122
- $ this ->writeText ("\n" );
123
- $ this ->writeText (' ' .$ command ->getSynopsis (), $ options );
124
- $ this ->writeText ("\n" );
125
-
126
- if (count ($ command ->getAliases ()) > 0 ) {
142
+ foreach (array_merge (array ($ command ->getSynopsis (true )), $ command ->getAliases (), $ command ->getUsages ()) as $ usage ) {
127
143
$ this ->writeText ("\n" );
128
- $ this ->writeText ('<comment>Aliases:</comment> <info> ' . implode ( ' , ' , $ command -> getAliases ()). ' </info> ' , $ options );
144
+ $ this ->writeText (' ' . $ usage , $ options );
129
145
}
146
+ $ this ->writeText ("\n" );
130
147
131
- if ($ definition = $ command ->getNativeDefinition ()) {
148
+ $ definition = $ command ->getNativeDefinition ();
149
+ if ($ definition ->getOptions () || $ definition ->getArguments ()) {
132
150
$ this ->writeText ("\n" );
133
151
$ this ->describeInputDefinition ($ definition , $ options );
152
+ $ this ->writeText ("\n" );
134
153
}
135
154
136
- $ this ->writeText ("\n" );
137
-
138
155
if ($ help = $ command ->getProcessedHelp ()) {
156
+ $ this ->writeText ("\n" );
139
157
$ this ->writeText ('<comment>Help:</comment> ' , $ options );
140
158
$ this ->writeText ("\n" );
141
159
$ this ->writeText (' ' .str_replace ("\n" , "\n " , $ help ), $ options );
@@ -164,27 +182,12 @@ protected function describeApplication(Application $application, array $options
164
182
}
165
183
166
184
$ this ->writeText ("<comment>Usage:</comment> \n" , $ options );
167
- $ this ->writeText (" command [options] [arguments] \n\n" , $ options );
168
- $ this ->writeText ('<comment>Options:</comment> ' , $ options );
169
-
170
- $ inputOptions = $ application ->getDefinition ()->getOptions ();
171
-
172
- $ width = 0 ;
173
- foreach ($ inputOptions as $ option ) {
174
- $ nameLength = strlen ($ option ->getName ()) + 2 ;
175
- if ($ option ->getShortcut ()) {
176
- $ nameLength += strlen ($ option ->getShortcut ()) + 3 ;
177
- }
178
- $ width = max ($ width , $ nameLength );
179
- }
180
- ++$ width ;
185
+ $ this ->writeText (" command [options] [arguments] \n\n" , $ options );
181
186
182
- foreach ($ inputOptions as $ option ) {
183
- $ this ->writeText ("\n" , $ options );
184
- $ this ->describeInputOption ($ option , array_merge ($ options , array ('name_width ' => $ width )));
185
- }
187
+ $ this ->describeInputDefinition (new InputDefinition ($ application ->getDefinition ()->getOptions ()), $ options );
186
188
187
- $ this ->writeText ("\n\n" , $ options );
189
+ $ this ->writeText ("\n" );
190
+ $ this ->writeText ("\n" );
188
191
189
192
$ width = $ this ->getColumnWidth ($ description ->getCommands ());
190
193
@@ -198,12 +201,13 @@ protected function describeApplication(Application $application, array $options
198
201
foreach ($ description ->getNamespaces () as $ namespace ) {
199
202
if (!$ describedNamespace && ApplicationDescription::GLOBAL_NAMESPACE !== $ namespace ['id ' ]) {
200
203
$ this ->writeText ("\n" );
201
- $ this ->writeText ('<comment> ' .$ namespace ['id ' ].'</comment> ' , $ options );
204
+ $ this ->writeText (' <comment> ' .$ namespace ['id ' ].'</comment> ' , $ options );
202
205
}
203
206
204
207
foreach ($ namespace ['commands ' ] as $ name ) {
205
208
$ this ->writeText ("\n" );
206
- $ this ->writeText (sprintf (" <info>%- $ {width}s</info> %s " , $ name , $ description ->getCommand ($ name )->getDescription ()), $ options );
209
+ $ spacingWidth = $ width - strlen ($ name );
210
+ $ this ->writeText (sprintf (" <info>%s</info>%s%s " , $ name , str_repeat (' ' , $ spacingWidth ), $ description ->getCommand ($ name )->getDescription ()), $ options );
207
211
}
208
212
}
209
213
@@ -252,4 +256,27 @@ private function getColumnWidth(array $commands)
252
256
253
257
return $ width + 2 ;
254
258
}
259
+
260
+ /**
261
+ * @param InputOption[] $options
262
+ *
263
+ * @return int
264
+ */
265
+ private function calculateTotalWidthForOptions ($ options )
266
+ {
267
+ $ totalWidth = 0 ;
268
+ foreach ($ options as $ option ) {
269
+ $ nameLength = 4 + strlen ($ option ->getName ()) + 2 ; // - + shortcut + , + whitespace + name + --
270
+
271
+ if ($ option ->acceptValue ()) {
272
+ $ valueLength = 1 + strlen ($ option ->getName ()); // = + value
273
+ $ valueLength += $ option ->isValueOptional () ? 2 : 0 ; // [ + ]
274
+
275
+ $ nameLength += $ valueLength ;
276
+ }
277
+ $ totalWidth = max ($ totalWidth , $ nameLength );
278
+ }
279
+
280
+ return $ totalWidth ;
281
+ }
255
282
}
0 commit comments