Skip to content

Commit

Permalink
feat(command): improving command options parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
razshare committed Dec 17, 2024
1 parent 9a315ab commit 42fd920
Show file tree
Hide file tree
Showing 17 changed files with 152 additions and 192 deletions.
3 changes: 1 addition & 2 deletions docs/Command.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ $runner = new class implements CommandRunnerInterface {
* @return Result<None>
*/
public function build(CommandBuilder $builder):void {
$builder->withOption('o','--option', error('No value provided.'));
$builder->requires('o');
$builder->required('o','--option', error('No value provided.'));
}

/**
Expand Down
9 changes: 3 additions & 6 deletions src/commands/Core/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ public function __construct(private EnvironmentInterface $environment) {
}

public function build(CommandBuilder $builder):void {
$builder->withOption('b', 'build', error('No value provided.'));
$builder->withOption('o', 'optimize', ok('0'));
$builder->withOption('e', 'environment', ok('0'));

$builder->requires('b');
$builder->requires('e');
$builder->required('b', 'build');
$builder->required('e', 'environment');
$builder->optional('o', 'optimize');
}

public function run(CommandContext $context):Result {
Expand Down
4 changes: 1 addition & 3 deletions src/commands/Core/HiCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
#[Provider]
final class HiCommand implements CommandRunnerInterface {
public function build(CommandBuilder $builder):void {
$builder->withOption('h', 'hi', error('No value provided.'));
$builder->requires('h');
$builder->required('h', 'hi');
}

public function run(CommandContext $context):Result {
echo "Hi.\n";
return ok();
}
}
8 changes: 2 additions & 6 deletions src/commands/Core/InstallPreCommitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@
#[Provider]
final class InstallPreCommitCommand implements CommandRunnerInterface {
public function build(CommandBuilder $builder):void {
$builder->withOption('i', 'install-pre-commit', error('No value provided.'));
$builder->requires('i');
$builder->required('i', 'install-pre-commit');
}

public function run(CommandContext $context):Result {
$command = $context->get('install-pre-commit')->unwrap($error);
if ($error) {
return error($error);
}
$command = $context->get('install-pre-commit')?:'';

installPreCommit($command)->unwrap($error);
if ($error) {
Expand Down
3 changes: 1 addition & 2 deletions src/commands/Core/TipsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
#[Provider]
final class TipsCommand implements CommandRunnerInterface {
public function build(CommandBuilder $builder):void {
$builder->withOption('t', 'tips', error('No value provided.'));
$builder->requires('t');
$builder->required('t', 'tips');
}

public function run(CommandContext $context):Result {
Expand Down
3 changes: 1 addition & 2 deletions src/commands/Core/UninstallPreCommitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
#[Provider]
final class UninstallPreCommitCommand implements CommandRunnerInterface {
public function build(CommandBuilder $builder):void {
$builder->withOption('u', 'uninstall-pre-commit', error('No value provided.'));
$builder->requires('u');
$builder->required('u', 'uninstall-pre-commit');
}

public function run(CommandContext $context):Result {
Expand Down
37 changes: 26 additions & 11 deletions src/lib/Core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ public function __construct(private string $startFileName) {
}

public function build(CommandBuilder $builder):void {
$builder->withOption('m', 'main', error('No value provided.'));
$builder->withOption('p', 'php');
$builder->withOption('e', 'environment');
$builder->withOption('n', 'name');
$builder->withOption('d', "die-on-change");
$builder->withOption('w', "watch");
$builder->withOption('l', 'libraries');
$builder->withOption('r', 'resources');

$builder->requires('m');
$builder->required('m', 'main');
$builder->optional('p', 'php');
$builder->optional('e', 'environment');
$builder->optional('n', 'name');
$builder->optional('d', "die-on-change");
$builder->optional('w', "watch");
$builder->optional('l', 'libraries');
$builder->optional('r', 'resources');
}

public function run(CommandContext $context):Result {
Expand All @@ -29,7 +27,24 @@ public function run(CommandContext $context):Result {
$main = $context->get('main')?:'';
$libraries = $context->get('libraries')?:'';
$resources = $context->get('resources')?:'';
$environment = $context->get('environment')?:'env.ini';
$environment = $context->get('environment')?:'';

if ($main) {
$main = realpath($main);
}

if ($libraries) {
$libraries = realpath($libraries);
}

if ($resources) {
$resources = realpath($resources);
}

if ($environment) {
$environment = realpath($environment);
}


if (!$main) {
return error('No main file specified. Use `--main=src/main.php` to specify a main file.');
Expand Down
8 changes: 2 additions & 6 deletions src/lib/Core/ApplicationBundled.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@ public function __construct(
}

public function build(CommandBuilder $builder):void {
$builder->withOption('e', 'environment', ok('0'));
$builder->requires('e');
$builder->optional('e', 'environment');
}

public function run(CommandContext $context):Result {
$environment = $context->get('environment')->unwrap($error);
if ($error || !$environment) {
$environment = $this->environment;
}
$environment = $context->get('environment')?:$this->environment;

Bootstrap::start(
main: $this->main,
Expand Down
56 changes: 18 additions & 38 deletions src/lib/Core/CommandBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,33 @@
define('ZERO_RESULT', ok('0'));

class CommandBuilder {
/** @var array<string,CommandOption> */
private array $options = [];
/** @var array<int,CommandParameter> */
private array $parameters = [];

/**
* Get the command options.
* @return array<string,CommandOption>
* @return array<int,CommandParameter>
*/
public function options():array {
return $this->options;
public function &parameters():array {
return $this->parameters;
}


/** @var array<string> */
private array $required = [];

/**
* Get the required options.
* @return array<string>
*/
public function required():array {
return $this->required;
}

/**
* Enforce an option to be required.
* @param string $name
* @return void
*/
public function requires(string $name):void {
$this->required[$name] = true;
public function required(string $shortName, string $longName):void {
$this->parameters[] = new CommandParameter(
longName: $longName,
shortName: $shortName,
updated: false,
required: true,
value: '',
);
}

/**
* Add a command option.
* @param string $shortName
* @param string $longName
* @param Result<string> $fallback
* @return CommandBuilder
*/
public function withOption(string $shortName, string $longName, Result $fallback = ZERO_RESULT):self {
$option = new CommandOption(
public function optional(string $shortName, string $longName):void {
$this->parameters[] = new CommandParameter(
longName: $longName,
shortName: $shortName,
valueResult: $fallback,
updated: false,
required: false,
value: '',
);
$this->options[$longName] = $option;
$this->options[$shortName] = $option;
return $this;
}
}
30 changes: 16 additions & 14 deletions src/lib/Core/CommandContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,32 @@
class CommandContext {
/**
*
* @param array<string,CommandOption> $options
* @param array<int,CommandParameter> $parameters
* @return void
*/
public function __construct(private array $options) {
public function __construct(private array $parameters) {
}

/**
* Get a command option.
* @param string $name Name of the option, short or long.
* Get a command parameter.
* @param string $name Name of the option, short or long.
* @return string
*/
public function get(string $name):string {
if (!isset($this->options[$name]) || !$this->options[$name]) {
return '0';
}
$value = $this->options[$name]->valueResult->unwrap($error);
if ($error) {
return '0';
}
foreach ($this->parameters as $parameter) {
if ($name !== $parameter->longName && $name !== $parameter->shortName) {
continue;
}
$value = $parameter->value;
$boolean = filter_var($value, FILTER_VALIDATE_BOOLEAN);

if ('' !== $value && $boolean) {
return '1';
}

if('' === $value){
return '1';
return $value;
}

return $value?:'0';
return '';
}
}
17 changes: 0 additions & 17 deletions src/lib/Core/CommandOption.php

This file was deleted.

20 changes: 20 additions & 0 deletions src/lib/Core/CommandParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace CatPaw\Core;

readonly class CommandParameter {
/**
* @param string $longName
* @param string $shortName
* @param bool $required
* @param string $value
* @return void
*/
public function __construct(
public string $longName,
public string $shortName,
public bool $required,
public bool $updated,
public string $value,
) {
}
}
5 changes: 1 addition & 4 deletions src/lib/Core/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,10 @@ private static function findFunctionDependencies(
try {
$reflectionParameters = $reflection->getParameters();
$items = [];
foreach ($reflectionParameters as $key => $reflectionParameter) {
foreach ($reflectionParameters as $reflectionParameter) {
$unwrappedType = ReflectionTypeManager::unwrap($reflectionParameter);
$wrappedType = ReflectionTypeManager::wrap($reflectionParameter);
$type = $unwrappedType?$unwrappedType->getName():'';
if (FileServerOverwriteInterface::class === $type) {
echo "here\n";
}
$name = $reflectionParameter->getName();
$attributes = $reflectionParameter->getAttributes();

Expand Down
Loading

0 comments on commit 42fd920

Please sign in to comment.