Added manpage() and usage() functions

This commit is contained in:
Alannah Kearney 2019-05-20 22:15:35 +10:00
commit 7532bd04a0

View file

@ -1,8 +1,14 @@
<?php <?php
declare(strict_types=1);
namespace pointybeard\Helpers\Functions\Cli; namespace pointybeard\Helpers\Functions\Cli;
/** use pointybeard\Helpers\Cli\Input;
use pointybeard\Helpers\Functions\Flags;
use pointybeard\Helpers\Functions\Strings;
/*
* Checks if bash can be invoked. * Checks if bash can be invoked.
* *
* Credit to Troels Knak-Nielsen for inspiring this code. * Credit to Troels Knak-Nielsen for inspiring this code.
@ -10,22 +16,89 @@ namespace pointybeard\Helpers\Functions\Cli;
* *
* @return bool true if bash can be invoked * @return bool true if bash can be invoked
*/ */
if (!function_exists(__NAMESPACE__ . 'can_invoke_bash')) { if (!function_exists(__NAMESPACE__.'can_invoke_bash')) {
function can_invoke_bash() function can_invoke_bash(): bool
{ {
return (strcmp(trim(shell_exec("/usr/bin/env bash -c 'echo OK'")), 'OK') === 0); return 0 === strcmp(trim(shell_exec("/usr/bin/env bash -c 'echo OK'")), 'OK');
} }
} }
/** /*
* Checks if script is running as root user * Checks if script is running as root user
* *
* @return bool true if user is root * @return bool true if user is root
*/ */
if (!function_exists(__NAMESPACE__ . 'is_su')) { if (!function_exists(__NAMESPACE__.'is_su')) {
function is_su() function is_su(): bool
{ {
$userinfo = posix_getpwuid(posix_geteuid()); $userinfo = posix_getpwuid(posix_geteuid());
return (bool)($userinfo['uid'] == 0 || $userinfo['name'] == 'root');
return (bool) (0 == $userinfo['uid'] || 'root' == $userinfo['name']);
}
}
if (!function_exists(__NAMESPACE__.'usage')) {
function usage(string $name, Input\InputCollection $collection): string
{
$arguments = [];
foreach ($collection->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
);
} }
} }