@@ -38,7 +38,7 @@ class Options
3838 */
3939 public function __construct (Colors $ colors = null )
4040 {
41- if (!is_null ($ colors )) {
41+ if (!is_null ($ colors )) {
4242 $ this ->colors = $ colors ;
4343 } else {
4444 $ this ->colors = new Colors ();
@@ -311,7 +311,8 @@ public function getCmd()
311311 *
312312 * @return array
313313 */
314- public function getArgs () {
314+ public function getArgs ()
315+ {
315316 return $ this ->args ;
316317 }
317318
@@ -322,42 +323,60 @@ public function getArgs() {
322323 */
323324 public function help ()
324325 {
326+ $ tf = new TableFormatter ($ this ->colors );
325327 $ text = '' ;
326328
327329 $ hascommands = (count ($ this ->setup ) > 1 );
328330 foreach ($ this ->setup as $ command => $ config ) {
329331 $ hasopts = (bool )$ this ->setup [$ command ]['opts ' ];
330332 $ hasargs = (bool )$ this ->setup [$ command ]['args ' ];
331333
334+ // usage or command syntax line
332335 if (!$ command ) {
333- $ text .= 'USAGE: ' . $ this ->bin ;
336+ $ text .= $ this ->colors ->wrap ('USAGE: ' , 'brown ' );
337+ $ text .= "\n" ;
338+ $ text .= ' ' . $ this ->bin ;
339+ $ mv = 2 ;
334340 } else {
335- $ text .= "\n$ command " ;
341+ $ text .= "\n" ;
342+ $ text .= $ this ->colors ->wrap (' ' . $ command , 'purple ' );
343+ $ mv = 4 ;
336344 }
337345
338346 if ($ hasopts ) {
339- $ text .= ' <OPTIONS> ' ;
347+ $ text .= ' ' . $ this ->colors ->wrap ('<OPTIONS> ' , 'green ' );
348+ }
349+
350+ if (!$ command && $ hascommands ) {
351+ $ text .= ' ' . $ this ->colors ->wrap ('<COMMAND> ... ' , 'purple ' );
340352 }
341353
342354 foreach ($ this ->setup [$ command ]['args ' ] as $ arg ) {
343- if ( $ arg ['required ' ]) {
344- $ text .= ' < ' . $ arg [ ' name ' ] . ' > ' ;
345- } else {
346- $ text . = ' [< ' . $ arg [ ' name ' ] . '> ] ' ;
355+ $ out = $ this -> colors -> wrap ( ' < ' . $ arg ['name ' ] . ' > ' , ' cyan ' );
356+
357+ if (! $ arg [ ' required ' ]) {
358+ $ out = '[ ' . $ out . '] ' ;
347359 }
360+ $ text .= ' ' . $ out ;
348361 }
349362 $ text .= "\n" ;
350363
364+ // usage or command intro
351365 if ($ this ->setup [$ command ]['help ' ]) {
352366 $ text .= "\n" ;
353- $ text .= $ this -> tableFormat (
354- array (2 , 72 ),
367+ $ text .= $ tf -> format (
368+ array ($ mv , ' * ' ),
355369 array ('' , $ this ->setup [$ command ]['help ' ] . "\n" )
356370 );
357371 }
358372
373+ // option description
359374 if ($ hasopts ) {
360- $ text .= "\n OPTIONS \n\n" ;
375+ if (!$ command ) {
376+ $ text .= "\n" ;
377+ $ text .= $ this ->colors ->wrap ('OPTIONS: ' , 'brown ' );
378+ }
379+ $ text .= "\n" ;
361380 foreach ($ this ->setup [$ command ]['opts ' ] as $ long => $ opt ) {
362381
363382 $ name = '' ;
@@ -373,28 +392,43 @@ public function help()
373392 $ name .= ' < ' . $ opt ['needsarg ' ] . '> ' ;
374393 }
375394
376- $ text .= $ this ->tableFormat (
377- array (2 , 20 , 52 ),
378- array ('' , $ name , $ opt ['help ' ])
395+ $ text .= $ tf ->format (
396+ array ($ mv , '30% ' , '* ' ),
397+ array ('' , $ name , $ opt ['help ' ]),
398+ array ('' , 'green ' , '' )
379399 );
380400 $ text .= "\n" ;
381401 }
382402 }
383403
404+ // argument description
384405 if ($ hasargs ) {
406+ if (!$ command ) {
407+ $ text .= "\n" ;
408+ $ text .= $ this ->colors ->wrap ('ARGUMENTS: ' , 'brown ' );
409+ }
385410 $ text .= "\n" ;
386411 foreach ($ this ->setup [$ command ]['args ' ] as $ arg ) {
387412 $ name = '< ' . $ arg ['name ' ] . '> ' ;
388413
389- $ text .= $ this ->tableFormat (
390- array (2 , 20 , 52 ),
391- array ('' , $ name , $ arg ['help ' ])
414+ $ text .= $ tf ->format (
415+ array ($ mv , '30% ' , '* ' ),
416+ array ('' , $ name , $ arg ['help ' ]),
417+ array ('' , 'cyan ' , '' )
392418 );
393419 }
394420 }
395421
396- if ($ command == '' && $ hascommands ) {
397- $ text .= "\nThis tool accepts a command as first parameter as outlined below: \n" ;
422+ // head line and intro for following command documentation
423+ if (!$ command && $ hascommands ) {
424+ $ text .= "\n" ;
425+ $ text .= $ this ->colors ->wrap ('COMMANDS: ' , 'brown ' );
426+ $ text .= "\n" ;
427+ $ text .= $ tf ->format (
428+ array ($ mv , '* ' ),
429+ array ('' , 'This tool accepts a command as first parameter as outlined below: ' )
430+ );
431+ $ text .= "\n" ;
398432 }
399433 }
400434
@@ -425,41 +459,5 @@ private function readPHPArgv()
425459 }
426460 return $ argv ;
427461 }
428-
429- /**
430- * Displays text in multiple word wrapped columns
431- *
432- * @param int[] $widths list of column widths (in characters)
433- * @param string[] $texts list of texts for each column
434- * @return string
435- */
436- private function tableFormat ($ widths , $ texts )
437- {
438- $ wrapped = array ();
439- $ maxlen = 0 ;
440-
441- foreach ($ widths as $ col => $ width ) {
442- $ wrapped [$ col ] = explode ("\n" , wordwrap ($ texts [$ col ], $ width - 1 , "\n" , true )); // -1 char border
443- $ len = count ($ wrapped [$ col ]);
444- if ($ len > $ maxlen ) {
445- $ maxlen = $ len ;
446- }
447-
448- }
449-
450- $ out = '' ;
451- for ($ i = 0 ; $ i < $ maxlen ; $ i ++) {
452- foreach ($ widths as $ col => $ width ) {
453- if (isset ($ wrapped [$ col ][$ i ])) {
454- $ val = $ wrapped [$ col ][$ i ];
455- } else {
456- $ val = '' ;
457- }
458- $ out .= sprintf ('%- ' . $ width . 's ' , $ val );
459- }
460- $ out .= "\n" ;
461- }
462- return $ out ;
463- }
464462}
465463
0 commit comments