diff --git a/src/Application.php b/src/Application.php index 1701db8..9047c7c 100644 --- a/src/Application.php +++ b/src/Application.php @@ -76,17 +76,6 @@ public function doRun(InputInterface $input, OutputInterface $output) return parent::doRun($input, $output); } - protected function getDefaultInputDefinition() - { - $definition = parent::getDefaultInputDefinition(); - - $definition->addOptions([ - new InputOption('--instance', '-I', InputOption::VALUE_REQUIRED, 'Specify the concrete5 directory', '.'), - ]); - - return $definition; - } - public function getHelp() { $artWidth = 42; diff --git a/src/Command/Backup/BackupCommand.php b/src/Command/Backup/BackupCommand.php index 7d51012..a42ba60 100644 --- a/src/Command/Backup/BackupCommand.php +++ b/src/Command/Backup/BackupCommand.php @@ -90,8 +90,8 @@ public function __invoke(?string $filename, InputInterface $input) */ public static function register(Container $container, Application $console): void { - $console - ->command( + static::command( + $console, 'backup:backup [filename] [--skip-core] [--temp] [--dir=]', self::class, ['backup'] diff --git a/src/Command/Backup/InspectCommand.php b/src/Command/Backup/InspectCommand.php index 70812ec..9f45473 100644 --- a/src/Command/Backup/InspectCommand.php +++ b/src/Command/Backup/InspectCommand.php @@ -85,7 +85,7 @@ public function __invoke(string $backupFile, InputInterface $input, ManifestFact public static function register(Container $container, Application $console): void { - $console->command('backup:inspect backupfile [-r|--manifest-only] [--ls=]', self::class) + self::command($console, 'backup:inspect backupfile [-r|--manifest-only] [--ls=]', self::class) ->descriptions('Inspects a Concrete installation backup', [ 'backupfile' => 'The path to the backup file to inspect', '--manifest-only' => 'Output the raw manifest json, combine with jq command.', diff --git a/src/Command/Backup/RestoreCommand.php b/src/Command/Backup/RestoreCommand.php index f176e38..3eada70 100644 --- a/src/Command/Backup/RestoreCommand.php +++ b/src/Command/Backup/RestoreCommand.php @@ -86,8 +86,8 @@ public function __invoke( public static function register(Container $container, Application $console): void { - $console - ->command( + self::command( + $console, 'backup:restore backupFile [-D|--dryrun] [--skip-db] [--skip-core] [--skip-packages] [--skip-config] [--skip-files] [--skip-application] [--skip-index] [--skip-database] [--reload-fpm-command=]', diff --git a/src/Command/Command.php b/src/Command/Command.php index 7dfbc2c..fb80660 100644 --- a/src/Command/Command.php +++ b/src/Command/Command.php @@ -4,6 +4,7 @@ namespace Concrete\Console\Command; +use Concrete\Console\Application; use Concrete\Console\Concrete\Connection\ApplicationEnabledConnectionInterface; use Concrete\Console\Concrete\Connection\ConnectionAwareInterface; use Concrete\Console\Concrete\Connection\ConnectionAwareTrait; @@ -12,6 +13,7 @@ use Concrete\Console\Installation\InstallationAwareTrait; use League\Container\ContainerAwareInterface; use League\Container\ContainerAwareTrait; +use Silly\Command\Command as SillyCommand; abstract class Command implements ContainerAwareInterface, @@ -27,6 +29,21 @@ abstract class Command implements use ConnectionAwareTrait; use InstallationAwareTrait; + /** + * Additional options to add separate from declared commands + * + * @var array + */ + protected static $extendedOptions = [ + [ + 'long' => 'instance', + 'short' => 'I', + 'description' => 'Specify the concrete5 directory', + 'default' => '.', + 'optional' => true, + ] + ]; + /** * @throws \Concrete\Console\Exception\Installation\VersionMismatch */ @@ -39,4 +56,51 @@ public function getApplication(): \Concrete\Core\Application\Application return $connection->getApplication(); } + + /** + * Static function for resolving the command instance with $extendedOptions included. + * + * @param Application $console + * @param string $expression + * @param string|callable $callable + * @param array $aliases + * + * @return SillyCommand + */ + public static function command( + Application $console, + string $expression, + $callable, + array $aliases = [] + ): SillyCommand { + $extension = []; + $descriptions = []; + $defaults = []; + foreach (static::$extendedOptions as $option) { + [ + 'long' => $long, + 'short' => $short, + 'description' => $description, + 'default' => $default, + 'optional' => $optional + ] = $option; + + // Resolve the extension string + $optionalAddition = $optional ? '=' : ''; + $shortFlag = $short ? "-{$short}|" : ''; + $longFlag = "--{$long}{$optionalAddition}"; + $extension[] = "[{$shortFlag}{$longFlag}]"; + + // resolve defaults and descriptions + $defaults[$long] = $default; + $descriptions['--' . $long] = $description; + } + + $extension = $extension ? ' ' . implode(' ', $extension) : ''; + + // Create a new command with our extension string added + return $console->command("{$expression}{$extension}", $callable, $aliases) + ->descriptions('', $descriptions) + ->defaults($defaults); + } } diff --git a/src/Command/Database/DumpCommand.php b/src/Command/Database/DumpCommand.php index 490bf15..353950b 100644 --- a/src/Command/Database/DumpCommand.php +++ b/src/Command/Database/DumpCommand.php @@ -66,7 +66,7 @@ public function __invoke(string $file, Input $input) public static function register(Container $container, Application $console): void { - $console->command('database:dump [file] [-z|--gz]', self::class) + self::command($console, 'database:dump [file] [-z|--gz]', self::class) ->descriptions('Dumps the Concrete database to a file', [ 'file' => 'Filename for the dump file', '--gz' => 'Flag to gzip', diff --git a/src/Command/Site/InfoCommand.php b/src/Command/Site/InfoCommand.php index 5d06002..50277e3 100644 --- a/src/Command/Site/InfoCommand.php +++ b/src/Command/Site/InfoCommand.php @@ -31,7 +31,7 @@ public function __invoke() */ public static function register(Container $container, Application $console): void { - $console->command('site:info', self::class, ['info']) + self::command($console, 'site:info', self::class, ['info']) ->descriptions('Get info about the current Concrete installation'); } } diff --git a/src/Command/Site/SyncCommand.php b/src/Command/Site/SyncCommand.php index 5984519..b72fb70 100644 --- a/src/Command/Site/SyncCommand.php +++ b/src/Command/Site/SyncCommand.php @@ -55,7 +55,7 @@ public function __invoke(string $from, InputInterface $input) public static function register(Container $container, Application $console): void { - $console->command('site:sync from [--config=]', self::class) + self::command($console, 'site:sync from [--config=]', self::class) ->descriptions( 'Sync a remote site into this site using backups.', [