mirror of
https://github.com/n3w/helpers-functions-cli.git
synced 2025-12-19 12:43:22 +00:00
Refactoring of manpage() to hide 'Options' and/or 'Arguments' if there are none to show.
This commit is contained in:
parent
627406241e
commit
3f07310dfe
1 changed files with 60 additions and 48 deletions
108
src/Cli/Cli.php
108
src/Cli/Cli.php
|
|
@ -62,54 +62,10 @@ if (!function_exists(__NAMESPACE__.'usage')) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!function_exists(__NAMESPACE__.'manpage')) {
|
/**
|
||||||
function manpage(string $name, string $version, string $description, Input\InputCollection $collection, $foregroundColour=Colour\Colour::FG_DEFAULT, $headingColour=Colour\Colour::FG_WHITE, array $additional=[]): string
|
* Uses tput to find out the size of the window (columns and lines)
|
||||||
{
|
* @return array an array containing exactly 2 items: 'cols' and 'lines'
|
||||||
$arguments = $options = [];
|
*/
|
||||||
|
|
||||||
foreach ($collection->getArguments() as $a) {
|
|
||||||
$arguments[] = (string) $a;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($collection->getOptions() as $o) {
|
|
||||||
$options[] = (string) $o;
|
|
||||||
}
|
|
||||||
|
|
||||||
$arguments = implode($arguments, PHP_EOL);
|
|
||||||
$options = implode($options, PHP_EOL);
|
|
||||||
|
|
||||||
// Convienence function for wrapping a heading with colour
|
|
||||||
$heading = function(string $input) use ($headingColour) {
|
|
||||||
return Colour\Colour::colourise($input, $headingColour);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Convienence function for wrapping input in a specified colour
|
|
||||||
$colourise = function(string $input) use ($foregroundColour) {
|
|
||||||
return Colour\Colour::colourise($input, $foregroundColour);
|
|
||||||
};
|
|
||||||
$sections = [
|
|
||||||
$colourise(sprintf(
|
|
||||||
"%s %s, %s\r\n%s\r\n",
|
|
||||||
$name,
|
|
||||||
$version,
|
|
||||||
$description,
|
|
||||||
usage($name, $collection)
|
|
||||||
)),
|
|
||||||
$heading('Arguments:'),
|
|
||||||
$colourise($arguments) . PHP_EOL,
|
|
||||||
$heading('Options:'),
|
|
||||||
$colourise($options) . PHP_EOL
|
|
||||||
];
|
|
||||||
|
|
||||||
foreach($additional as $name => $contents) {
|
|
||||||
$sections[] = $heading("{$name}:");
|
|
||||||
$sections[] = $colourise($contents) . PHP_EOL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return implode($sections, PHP_EOL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!function_exists(__NAMESPACE__.'get_window_size')) {
|
if (!function_exists(__NAMESPACE__.'get_window_size')) {
|
||||||
function get_window_size(): array
|
function get_window_size(): array
|
||||||
{
|
{
|
||||||
|
|
@ -119,3 +75,59 @@ if (!function_exists(__NAMESPACE__.'get_window_size')) {
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!function_exists(__NAMESPACE__.'manpage')) {
|
||||||
|
function manpage(string $name, string $version, string $description, Input\InputCollection $collection, $foregroundColour=Colour\Colour::FG_DEFAULT, $headingColour=Colour\Colour::FG_WHITE, array $additionalSections=[]): string
|
||||||
|
{
|
||||||
|
// Convienence function for wrapping a heading with colour
|
||||||
|
$heading = function(string $input) use ($headingColour) {
|
||||||
|
return Colour\Colour::colourise($input, $headingColour);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Convienence function for wrapping input in a specified colour
|
||||||
|
$colourise = function(string $input) use ($foregroundColour) {
|
||||||
|
return Colour\Colour::colourise($input, $foregroundColour);
|
||||||
|
};
|
||||||
|
|
||||||
|
$sections = [
|
||||||
|
$colourise(sprintf(
|
||||||
|
"%s %s, %s\r\n%s\r\n",
|
||||||
|
$name,
|
||||||
|
$version,
|
||||||
|
$description,
|
||||||
|
usage($name, $collection)
|
||||||
|
)),
|
||||||
|
];
|
||||||
|
|
||||||
|
$arguments = [];
|
||||||
|
$options = [];
|
||||||
|
|
||||||
|
foreach ($collection->getArguments() as $a) {
|
||||||
|
$arguments[] = (string) $a;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($collection->getOptions() as $o) {
|
||||||
|
$options[] = (string) $o;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the arguments, if there are any.
|
||||||
|
if(false === empty($arguments)){
|
||||||
|
$sections[] = $heading("Arguments:");
|
||||||
|
$sections[] = $colourise(implode($arguments, PHP_EOL)) . PHP_EOL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the options, if there are any.
|
||||||
|
if(false === empty($options)){
|
||||||
|
$sections[] = $heading("Options:");
|
||||||
|
$sections[] = $colourise(implode($options, PHP_EOL)) . PHP_EOL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Iterate over all additional items and add them as new sections
|
||||||
|
foreach($additionalSections as $name => $contents) {
|
||||||
|
$sections[] = $heading("{$name}:");
|
||||||
|
$sections[] = $colourise($contents) . PHP_EOL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return implode($sections, PHP_EOL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue