Skip to content

Commit 52ac4d1

Browse files
committed
update Program cli
update use of CommandLine api remove showVersion() method and use Command HelperBuilder instead remove registration of MigrateCommand
1 parent 1ab4204 commit 52ac4d1

File tree

1 file changed

+23
-64
lines changed

1 file changed

+23
-64
lines changed

src/Program.php

Lines changed: 23 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -10,88 +10,47 @@
1010
namespace DevNet\Cli;
1111

1212
use DevNet\Cli\Commands\AddCommand;
13-
use DevNet\Cli\Commands\MigrateCommand;
1413
use DevNet\Cli\Commands\NewCommand;
1514
use DevNet\Cli\Commands\RunCommand;
15+
use DevNet\Cli\Commands\CommandRegistry;
1616
use DevNet\System\Command\CommandEventArgs;
1717
use DevNet\System\Command\CommandLine;
18-
use DevNet\System\Command\CommandOption;
1918
use DevNet\System\IO\ConsoleColor;
2019
use DevNet\System\IO\Console;
2120

2221
class Program
2322
{
2423
public static function main(array $args = [])
2524
{
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+
}
5138

52-
if ($args->Residual) {
5339
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.");
5541
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+
});
7343

74-
foreach ($commands as $command) {
75-
$lenth = strlen($command->Name);
76-
if ($lenth > $super) {
77-
$super = $lenth;
78-
}
79-
}
44+
$provider = CommandRegistry::getSingleton();
8045

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);
8648
}
8749

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());
9153

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);
9655
}
9756
}

0 commit comments

Comments
 (0)