Skip to content

Commit

Permalink
Merge pull request #58 from tikoflano/argument-parsing-enhance
Browse files Browse the repository at this point in the history
Argument parsing enhance
  • Loading branch information
j-guyon committed Feb 26, 2017
2 parents 0b0a122 + 2a2e217 commit 614926c
Show file tree
Hide file tree
Showing 4 changed files with 3,040 additions and 7 deletions.
17 changes: 10 additions & 7 deletions Entity/ScheduledCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,20 @@ public function getArguments($toArray = false)

$argsArray = array();
if (null !== $this->arguments || '' != $this->arguments) {
$flatArgsArray = explode(' ', preg_replace('/\s+/', ' ', $this->arguments));
foreach ($flatArgsArray as $argument) {
$tmpArray = explode('=', $argument);
if (count($tmpArray) == 1) {
$argsArray[$tmpArray[0]] = true;
preg_match_all('/((?:[^ "\']|"[^"]*"|\'[^\']*\')+)/', $this->arguments, $flatArgsArray);

foreach ($flatArgsArray[0] as $argument) {
if (preg_match_all('/(.*?)=(?:["\'](.*)["\']|(.*))/', $argument, $tmpArray) === 0) {
$argsArray[$argument] = true;
} else if($tmpArray[2][0]) {
$argsArray[$tmpArray[1][0]] = $tmpArray[2][0];
} else if($tmpArray[3][0]) {
$argsArray[$tmpArray[1][0]] = $tmpArray[3][0];
} else {
$argsArray[$tmpArray[0]] = $tmpArray[1];
$argsArray[$tmpArray[1][0]] = "";
}
}
}

return $argsArray;
}

Expand Down
8 changes: 8 additions & 0 deletions Resources/doc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ From this screen, you can do following actions :
- Enable or disable scheduling (by clicking the "Power Off/On" switch)
- Manually execute a command (It will be launched during the next `scheduler:execute`, regardless of the cron expression)
- Unlock a task (if the lock is due to an unrecoverable error for example)

When creating a new scheduling, you can provide your commands arguments and options as follows:
- **Arguments:** `argument-name="my value"`
You can use single quotes, or ignore quotes when there is no white spaces in the value. This differs the way a command is called directly from the console (here you HAVE to specify the argument's name)
- **Options:** `--option-name="my value"`
You can use single quotes, or ignore quotes when there is no white spaces in the value.
- **Flags (options):** `--my-flag`
This will result in setting the 'my-flag' option to `true`.

After that, you have to set (every few minutes, it depends of your needs) the following command in your system :
``` bash
Expand Down
87 changes: 87 additions & 0 deletions Tests/Entity/ScheduledCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace JMose\CommandSchedulerBundle\Tests\Entity;

use Liip\FunctionalTestBundle\Test\WebTestCase;
use JMose\CommandSchedulerBundle\Entity\ScheduledCommand;

/**
* Class ScheduledCommandTest
* @package JMose\CommandSchedulerBundle\Tests\Entity
*/
class ScheduledCommandTest extends WebTestCase
{

/**
* Test CommandScheduler parameters parsing
*/
public function testParametersParsing()
{
$scheduledCommand = new ScheduledCommand();

$scheduledCommand->setArguments("argument1='my new value'");
$parsedArguments = $scheduledCommand->getArguments(true);
$this->assertArrayHasKey('argument1', $parsedArguments);
$this->assertEquals($parsedArguments['argument1'], 'my new value');

$scheduledCommand->setArguments("argument2=\"my new value\"");
$parsedArguments = $scheduledCommand->getArguments(true);
$this->assertArrayHasKey('argument2', $parsedArguments);
$this->assertEquals($parsedArguments['argument2'], 'my new value');

$scheduledCommand->setArguments("argument3=\"my new ' value\"");
$parsedArguments = $scheduledCommand->getArguments(true);
$this->assertArrayHasKey('argument3', $parsedArguments);
$this->assertEquals($parsedArguments['argument3'], "my new ' value");

$scheduledCommand->setArguments("argument4='my new \" value'");
$parsedArguments = $scheduledCommand->getArguments(true);
$this->assertArrayHasKey('argument4', $parsedArguments);
$this->assertEquals($parsedArguments['argument4'], 'my new " value');

$scheduledCommand->setArguments("argument5=value");
$parsedArguments = $scheduledCommand->getArguments(true);
$this->assertArrayHasKey('argument5', $parsedArguments);
$this->assertEquals($parsedArguments['argument5'], 'value');

$scheduledCommand->setArguments("argument5=wrong value");
$parsedArguments = $scheduledCommand->getArguments(true);
$this->assertArrayHasKey('argument5', $parsedArguments);
$this->assertNotEquals($parsedArguments['argument5'], 'wrong value');

$scheduledCommand->setArguments("--option1='my new value'");
$parsedArguments = $scheduledCommand->getArguments(true);
$this->assertArrayHasKey('--option1', $parsedArguments);
$this->assertEquals($parsedArguments['--option1'], 'my new value');

$scheduledCommand->setArguments("--option2=\"my new value\"");
$parsedArguments = $scheduledCommand->getArguments(true);
$this->assertArrayHasKey('--option2', $parsedArguments);
$this->assertEquals($parsedArguments['--option2'], 'my new value');

$scheduledCommand->setArguments("--option3=\"my new ' value\"");
$parsedArguments = $scheduledCommand->getArguments(true);
$this->assertArrayHasKey('--option3', $parsedArguments);
$this->assertEquals($parsedArguments['--option3'], "my new ' value");

$scheduledCommand->setArguments("--option4='my new \" value'");
$parsedArguments = $scheduledCommand->getArguments(true);
$this->assertArrayHasKey('--option4', $parsedArguments);
$this->assertEquals($parsedArguments['--option4'], 'my new " value');

$scheduledCommand->setArguments("--option5=value");
$parsedArguments = $scheduledCommand->getArguments(true);
$this->assertArrayHasKey('--option5', $parsedArguments);
$this->assertEquals($parsedArguments['--option5'], 'value');

$scheduledCommand->setArguments("--option5=wrong value");
$parsedArguments = $scheduledCommand->getArguments(true);
$this->assertArrayHasKey('--option5', $parsedArguments);
$this->assertNotEquals($parsedArguments['--option5'], 'wrong value');

$scheduledCommand->setArguments("--flag");
$parsedArguments = $scheduledCommand->getArguments(true);
$this->assertArrayHasKey('--flag', $parsedArguments);
$this->assertEquals($parsedArguments['--flag'], true);
}
}
Loading

0 comments on commit 614926c

Please sign in to comment.