|
10 | 10 | namespace DevNet\Cli; |
11 | 11 |
|
12 | 12 | use DevNet\Cli\Commands\AddCommand; |
13 | | -use DevNet\Cli\Commands\MigrateCommand; |
14 | 13 | use DevNet\Cli\Commands\NewCommand; |
15 | 14 | use DevNet\Cli\Commands\RunCommand; |
| 15 | +use DevNet\Cli\Commands\CommandRegistry; |
16 | 16 | use DevNet\System\Command\CommandEventArgs; |
17 | 17 | use DevNet\System\Command\CommandLine; |
18 | | -use DevNet\System\Command\CommandOption; |
19 | 18 | use DevNet\System\IO\ConsoleColor; |
20 | 19 | use DevNet\System\IO\Console; |
21 | 20 |
|
22 | 21 | class Program |
23 | 22 | { |
24 | 23 | public static function main(array $args = []) |
25 | 24 | { |
26 | | - $rootCommand = new CommandLine(); |
27 | | - $rootCommand->addCommand(new AddCommand); |
28 | | - $rootCommand->addCommand(new NewCommand); |
29 | | - $rootCommand->addCommand(new RunCommand); |
30 | | - $rootCommand->addCommand(new MigrateCommand); |
31 | | - $rootCommand->addOption(new CommandOption('--help', '-h')); |
32 | | - $rootCommand->addOption(new CommandOption('--version', '-v')); |
33 | | - $rootCommand->Handler->add([new self, 'execute']); |
34 | | - $rootCommand->invoke($args); |
35 | | - exit; |
36 | | - } |
37 | | - |
38 | | - public function execute(object $sender, CommandEventArgs $args): void |
39 | | - { |
40 | | - $help = $args->get('--help'); |
41 | | - if ($help) { |
42 | | - self::showHelp($sender); |
43 | | - return; |
44 | | - } |
45 | | - |
46 | | - $version = $args->get('--version'); |
47 | | - if ($version) { |
48 | | - self::showVersion(); |
49 | | - return; |
50 | | - } |
| 25 | + $rootCommand = new CommandLine('devnet', 'DevNet command-line interface'); |
| 26 | + $rootCommand->addOption('--version', 'Show version information', '-v', null); |
| 27 | + $rootCommand->setHelp(function ($builder) { |
| 28 | + $builder->useDefaults(); |
| 29 | + $builder->writeLine("Run 'devnet [command] --help' for more information on a command."); |
| 30 | + }); |
| 31 | + |
| 32 | + $rootCommand->setHandler(function (object $sender, CommandEventArgs $args): void { |
| 33 | + $version = $args->getParameter('--version'); |
| 34 | + if ($version) { |
| 35 | + Console::writeline("DevNet CLI: 1.0.0"); |
| 36 | + return; |
| 37 | + } |
51 | 38 |
|
52 | | - if ($args->Residual) { |
53 | 39 | Console::foregroundColor(ConsoleColor::Red); |
54 | | - Console::writeline("The specified command or option was not found, try '--help' option for usage information."); |
| 40 | + Console::writeline("The command 'devnet' cannot be executed alone, try '--help' option for usage information."); |
55 | 41 | Console::resetColor(); |
56 | | - } |
57 | | - } |
58 | | - |
59 | | - public static function showHelp(object $sender): void |
60 | | - { |
61 | | - Console::writeline("DevNet command-line interface v1.0.0"); |
62 | | - Console::writeline("Usage: devnet [options]"); |
63 | | - Console::writeline(); |
64 | | - Console::writeline("Options:"); |
65 | | - Console::writeline(" --help Show command line help."); |
66 | | - Console::writeline(" --version Show DevNet Cli version."); |
67 | | - Console::writeline(); |
68 | | - Console::writeline("Usage: devnet [command] [arguments] [options]"); |
69 | | - Console::writeline(); |
70 | | - Console::writeline("commands:"); |
71 | | - $super = 0; |
72 | | - $commands = $sender->Commands; |
| 42 | + }); |
73 | 43 |
|
74 | | - foreach ($commands as $command) { |
75 | | - $lenth = strlen($command->Name); |
76 | | - if ($lenth > $super) { |
77 | | - $super = $lenth; |
78 | | - } |
79 | | - } |
| 44 | + $provider = CommandRegistry::getSingleton(); |
80 | 45 |
|
81 | | - foreach ($commands as $command) { |
82 | | - $lenth = strlen($command->Name); |
83 | | - $steps = $super - $lenth + 3; |
84 | | - $space = str_repeat(" ", $steps); |
85 | | - Console::writeline(" {$command->Name}$space{$command->Description}"); |
| 46 | + foreach ($provider as $command) { |
| 47 | + $rootCommand->addCommand($command); |
86 | 48 | } |
87 | 49 |
|
88 | | - Console::writeline(); |
89 | | - Console::writeline("Run 'devnet [command] --help' for more information on a command."); |
90 | | - } |
| 50 | + $rootCommand->addCommand(new AddCommand()); |
| 51 | + $rootCommand->addCommand(new NewCommand()); |
| 52 | + $rootCommand->addCommand(new RunCommand()); |
91 | 53 |
|
92 | | - public static function showVersion(): void |
93 | | - { |
94 | | - Console::writeline("DevNet command-line interface v1.0.0"); |
95 | | - Console::writeline("Copyright (c) DevNet"); |
| 54 | + $rootCommand->invoke($args); |
96 | 55 | } |
97 | 56 | } |
0 commit comments