Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8c2c001

Browse files
introduce a more compact help screen
This is inspired by #9 but implements it in a more elegant way, requiring less conditionals.
1 parent 797a29b commit 8c2c001

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

‎examples/complex.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ protected function setup(Options $options)
3131
$options->registerOption('load', 'Another flag only for the bar command, requiring an argument', 'l', 'input',
3232
'bar');
3333

34+
$options->registerCommand('compact', 'Display the help text in a more compact manner');
3435
}
3536

3637
/**
@@ -51,6 +52,10 @@ protected function main(Options $options)
5152
case 'bar':
5253
$this->success('The bar command was called');
5354
break;
55+
case 'compact':
56+
$options->useCompactHelp();
57+
echo $options->help();
58+
exit;
5459
default:
5560
$this->error('No known command was called, we show the default help instead:');
5661
echo $options->help();
@@ -64,4 +69,4 @@ protected function main(Options $options)
6469
}
6570

6671
$cli = new Complex();
67-
$cli->run();
72+
$cli->run();

‎src/Options.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class Options
3131
/** @var Colors for colored help output */
3232
protected $colors;
3333

34+
/** @var string newline used for spacing help texts */
35+
protected $newline = "\n";
36+
3437
/**
3538
* Constructor
3639
*
@@ -88,6 +91,16 @@ public function setCommandHelp($help)
8891
$this->setup['']['commandhelp'] = $help;
8992
}
9093

94+
/**
95+
* Use a more compact help screen with less new lines
96+
*
97+
* @param bool $set
98+
*/
99+
public function useCompactHelp($set = true)
100+
{
101+
$this->newline = $set ? '' : "\n";
102+
}
103+
91104
/**
92105
* Register the names of arguments for help generation and number checking
93106
*
@@ -366,7 +379,7 @@ public function help()
366379
$text .= '' . $this->bin;
367380
$mv = 2;
368381
} else {
369-
$text .= "\n";
382+
$text .= $this->newline;
370383
$text .= $this->colors->wrap('' . $command, Colors::C_PURPLE);
371384
$mv = 4;
372385
}
@@ -387,14 +400,14 @@ public function help()
387400
}
388401
$text .= '' . $out;
389402
}
390-
$text .= "\n";
403+
$text .= $this->newline;
391404

392405
// usage or command intro
393406
if ($this->setup[$command]['help']) {
394407
$text .= "\n";
395408
$text .= $tf->format(
396409
array($mv, '*'),
397-
array('', $this->setup[$command]['help'] . "\n")
410+
array('', $this->setup[$command]['help'] . $this->newline)
398411
);
399412
}
400413

@@ -425,7 +438,7 @@ public function help()
425438
array('', $name, $opt['help']),
426439
array('', 'green', '')
427440
);
428-
$text .= "\n";
441+
$text .= $this->newline;
429442
}
430443
}
431444

@@ -435,7 +448,7 @@ public function help()
435448
$text .= "\n";
436449
$text .= $this->colors->wrap('ARGUMENTS:', Colors::C_BROWN);
437450
}
438-
$text .= "\n";
451+
$text .= $this->newline;
439452
foreach ($this->setup[$command]['args'] as $arg) {
440453
$name = '<' . $arg['name'] . '>';
441454

@@ -456,7 +469,7 @@ public function help()
456469
array($mv, '*'),
457470
array('', $commandhelp)
458471
);
459-
$text .= "\n";
472+
$text .= $this->newline;
460473
}
461474
}
462475

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /