From 7532bd04a01079635f99c26d4434bd9850a5ae89 Mon Sep 17 00:00:00 2001 From: Alannah Kearney Date: Mon, 20 May 2019 22:15:35 +1000 Subject: [PATCH] Added manpage() and usage() functions --- src/Cli/Cli.php | 89 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 81 insertions(+), 8 deletions(-) diff --git a/src/Cli/Cli.php b/src/Cli/Cli.php index 955b42f..2e54036 100644 --- a/src/Cli/Cli.php +++ b/src/Cli/Cli.php @@ -1,8 +1,14 @@ getArguments() as $a) { + $arguments[] = strtoupper( + // Wrap with square brackets if it's not required + Flags\is_flag_set(Input\AbstractInputType::FLAG_OPTIONAL, $a->flags()) || + !Flags\is_flag_set(Input\AbstractInputType::FLAG_REQUIRED, $a->flags()) + ? "[{$a->name()}]" + : $a->name() + ); + } + $arguments = trim(implode($arguments, ' ')); + + return sprintf( + 'Usage: %s [OPTIONS]... %s%s', + $name, + $arguments, + strlen($arguments) > 0 ? '...' : '' + ); + } +} + +if (!function_exists(__NAMESPACE__.'manpage')) { + function manpage(string $name, string $version, string $description, string $example, Input\InputCollection $collection): string + { + $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.' '); + + return sprintf( + '%s %s, %s +%s + +Mandatory values for long options are mandatory for short options too. + +Arguments: + %s + +Options: + %s + +Examples: + %s +', + $name, + $version, + Strings\utf8_wordwrap($description), + usage($name, $collection), + $arguments, + $options, + $example + ); } }