2024-01-01 00:45:31 +04:00
|
|
|
<?php
|
2024-04-04 22:57:10 +04:00
|
|
|
namespace nur\sery\wip\cli;
|
2024-01-01 00:45:31 +04:00
|
|
|
|
|
|
|
use Exception;
|
2024-07-16 01:54:30 +04:00
|
|
|
use nur\sery\ExitError;
|
2024-01-01 00:45:31 +04:00
|
|
|
use nur\sery\output\msg;
|
|
|
|
use nur\sery\output\std\StdMessenger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Application: une application en ligne de commande
|
|
|
|
*/
|
|
|
|
abstract class Application {
|
|
|
|
protected static function _app_init(): void {
|
|
|
|
msg::set_messenger_class(StdMessenger::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function _app_configure(Application $app): void {
|
|
|
|
$app->parseArgs();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function _app_main(Application $app): void {
|
|
|
|
$retcode = $app->main();
|
|
|
|
if (is_int($retcode)) exit($retcode);
|
|
|
|
elseif (is_bool($retcode)) exit($retcode? 0: 1);
|
|
|
|
elseif ($retcode !== null) exit(strval($retcode));
|
|
|
|
}
|
|
|
|
|
|
|
|
static function run(?Application $app=null): void {
|
|
|
|
try {
|
|
|
|
static::_app_init();
|
|
|
|
if ($app === null) $app = new static();
|
|
|
|
static::_app_configure($app);
|
|
|
|
static::_app_main($app);
|
2024-07-16 01:54:30 +04:00
|
|
|
} catch (ExitError $e) {
|
2024-01-01 00:45:31 +04:00
|
|
|
msg::error($e->getUserMessage());
|
|
|
|
exit($e->getCode());
|
|
|
|
} catch (Exception $e) {
|
|
|
|
msg::error($e);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sortir de l'application avec un code d'erreur, qui est 0 par défaut (i.e
|
|
|
|
* pas d'erreur)
|
|
|
|
*
|
2024-07-16 01:54:30 +04:00
|
|
|
* équivalent à lancer l'exception {@link ExitError}
|
2024-01-01 00:45:31 +04:00
|
|
|
*/
|
|
|
|
protected static final function exit(int $exitcode=0, $message=null) {
|
2024-07-16 01:54:30 +04:00
|
|
|
throw new ExitError($exitcode, $message);
|
2024-01-01 00:45:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sortir de l'application avec un code d'erreur, qui vaut 1 par défaut (i.e
|
|
|
|
* une erreur s'est produite)
|
|
|
|
*
|
2024-07-16 01:54:30 +04:00
|
|
|
* équivalent à lancer l'exception {@link ExitError}
|
2024-01-01 00:45:31 +04:00
|
|
|
*/
|
|
|
|
protected static final function die($message=null, int $exitcode=1) {
|
2024-07-16 01:54:30 +04:00
|
|
|
throw new ExitError($exitcode, $message);
|
2024-01-01 00:45:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
const ARGS = [];
|
|
|
|
|
|
|
|
/** @throws ArgsException */
|
|
|
|
function parseArgs(array $args=null): void {
|
|
|
|
$parser = new ArgsParser(static::ARGS);
|
|
|
|
$parser->parse($this, $args);
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract function main();
|
|
|
|
}
|