New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for getting raw parameters without validation #322
Conversation
Do you have a usecase in mind? |
Yes! @tlenclos needs that. (I asked a ticket but... no shame 😂) Anyway, this is very useful for such cases:
same for MANY commands actually ! |
e0728ea
to
1bf68d8
Compare
|
Should I ship that? #[AsTask(ignoreValidationErrors: true)]
function ls(#[AsRawTokens] array $extraParam): void
{
run(['ls', ...$extraParam], tty: true);
} Implementation
diff --git a/src/Attribute/AsRawTokens.php b/src/Attribute/AsRawTokens.php
new file mode 100644
index 0000000..3071806
--- /dev/null
+++ b/src/Attribute/AsRawTokens.php
@@ -0,0 +1,8 @@
+<?php
+
+namespace Castor\Attribute;
+
+#[\Attribute(\Attribute::TARGET_PARAMETER)]
+class AsRawTokens
+{
+}
diff --git a/src/Console/Command/TaskCommand.php b/src/Console/Command/TaskCommand.php
index adefbb2..a6ccf52 100644
--- a/src/Console/Command/TaskCommand.php
+++ b/src/Console/Command/TaskCommand.php
@@ -5,8 +5,10 @@ namespace Castor\Console\Command;
use Castor\Attribute\AsArgument;
use Castor\Attribute\AsCommandArgument;
use Castor\Attribute\AsOption;
+use Castor\Attribute\AsRawTokens;
use Castor\Attribute\AsTask;
use Castor\Console\Application;
+use Castor\Console\Input\Input;
use Castor\Event\AfterExecuteTaskEvent;
use Castor\Event\BeforeExecuteTaskEvent;
use Castor\EventDispatcher;
@@ -81,6 +83,10 @@ class TaskCommand extends Command implements SignalableCommandInterface
}
foreach ($this->function->getParameters() as $parameter) {
+ if ($parameter->getAttributes(AsRawTokens::class, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? null) {
+ continue;
+ }
+
$taskArgumentAttribute = $parameter->getAttributes(AsCommandArgument::class, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? null;
if ($taskArgumentAttribute) {
@@ -144,11 +150,23 @@ class TaskCommand extends Command implements SignalableCommandInterface
}
}
+ /**
+ * @param Input $input
+ */
protected function execute(InputInterface $input, OutputInterface $output): int
{
$args = [];
foreach ($this->function->getParameters() as $parameter) {
+ if ($parameter->getAttributes(AsRawTokens::class, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? null) {
+ $parameters = $input->getRawParameters();
+ // Remove command name
+ array_shift($parameters);
+ $args[] = $parameters;
+
+ continue;
+ }
+
$name = $this->getParameterName($parameter);
if ($input->hasArgument($name)) {
$args[] = $input->getArgument($name);
|
1bf68d8
to
f122e89
Compare
Shipped |
f122e89
to
1650d97
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really like the usecase, thanks.
Could you add some documentation about it?
1650d97
to
468ed4f
Compare
Oups, totally forgot the doc. Sorry! Is it better? |
Really nice thanks you @lyrixx |
Thanks ❤️ |
Example :