Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit89ee5db

Browse files
committed
[Console] Optionally stop options parsing at "--",fixes#21869
Looking for options in the (raw) arguments should not look after anargument of "--" has been encountered.Not doing so prevents invoking the command or passing these arguments tothe command w/o interfering with the wrong application or command.Places where the application looks for global flags like --verbose or--help make use of the new OPTION_FLAG_POSIX that stops parsing optionsat the options-from-operands delimiter "--" as in 12.2 Utility SyntaxGuidelines Guideline 10.Refs:-http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_02
1 parentfd94048 commit89ee5db

File tree

5 files changed

+206
-16
lines changed

5 files changed

+206
-16
lines changed

‎src/Symfony/Component/Console/Application.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,14 @@ public function run(InputInterface $input = null, OutputInterface $output = null
160160
*/
161161
publicfunctiondoRun(InputInterface$input,OutputInterface$output)
162162
{
163-
if (true ===$input->hasParameterOption(array('--version','-V'))) {
163+
if (true ===$input->hasParameterOption(array('--version','-V'),$input::OPTION_FLAG_POSIX)) {
164164
$output->writeln($this->getLongVersion());
165165

166166
return0;
167167
}
168168

169169
$name =$this->getCommandName($input);
170-
if (true ===$input->hasParameterOption(array('--help','-h'))) {
170+
if (true ===$input->hasParameterOption(array('--help','-h'),$input::OPTION_FLAG_POSIX)) {
171171
if (!$name) {
172172
$name ='help';
173173
$input =newArrayInput(array('command' =>'help'));
@@ -789,13 +789,13 @@ public function setTerminalDimensions($width, $height)
789789
*/
790790
protectedfunctionconfigureIO(InputInterface$input,OutputInterface$output)
791791
{
792-
if (true ===$input->hasParameterOption(array('--ansi'))) {
792+
if (true ===$input->hasParameterOption(array('--ansi'),$input::OPTION_FLAG_POSIX)) {
793793
$output->setDecorated(true);
794-
}elseif (true ===$input->hasParameterOption(array('--no-ansi'))) {
794+
}elseif (true ===$input->hasParameterOption(array('--no-ansi'),$input::OPTION_FLAG_POSIX)) {
795795
$output->setDecorated(false);
796796
}
797797

798-
if (true ===$input->hasParameterOption(array('--no-interaction','-n'))) {
798+
if (true ===$input->hasParameterOption(array('--no-interaction','-n'),$input::OPTION_FLAG_POSIX)) {
799799
$input->setInteractive(false);
800800
}elseif (function_exists('posix_isatty') &&$this->getHelperSet()->has('question')) {
801801
$inputStream =$this->getHelperSet()->get('question')->getInputStream();
@@ -804,15 +804,15 @@ protected function configureIO(InputInterface $input, OutputInterface $output)
804804
}
805805
}
806806

807-
if (true ===$input->hasParameterOption(array('--quiet','-q'))) {
807+
if (true ===$input->hasParameterOption(array('--quiet','-q'),$input::OPTION_FLAG_POSIX)) {
808808
$output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
809809
$input->setInteractive(false);
810810
}else {
811-
if ($input->hasParameterOption('-vvv') ||$input->hasParameterOption('--verbose=3') ||$input->getParameterOption('--verbose') ===3) {
811+
if ($input->hasParameterOption('-vvv',$input::OPTION_FLAG_POSIX) ||$input->hasParameterOption('--verbose=3',$input::OPTION_FLAG_POSIX) ||$input->getParameterOption('--verbose',false,$input::OPTION_FLAG_POSIX) ===3) {
812812
$output->setVerbosity(OutputInterface::VERBOSITY_DEBUG);
813-
}elseif ($input->hasParameterOption('-vv') ||$input->hasParameterOption('--verbose=2') ||$input->getParameterOption('--verbose') ===2) {
813+
}elseif ($input->hasParameterOption('-vv') ||$input->hasParameterOption('--verbose=2',$input::OPTION_FLAG_POSIX) ||$input->getParameterOption('--verbose',false,$input::OPTION_FLAG_POSIX) ===2) {
814814
$output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE);
815-
}elseif ($input->hasParameterOption('-v') ||$input->hasParameterOption('--verbose=1') ||$input->hasParameterOption('--verbose') ||$input->getParameterOption('--verbose')) {
815+
}elseif ($input->hasParameterOption('-v',$input::OPTION_FLAG_POSIX) ||$input->hasParameterOption('--verbose=1',$input::OPTION_FLAG_POSIX) ||$input->hasParameterOption('--verbose',$input::OPTION_FLAG_POSIX) ||$input->getParameterOption('--verbose',false,$input::OPTION_FLAG_POSIX)) {
816816
$output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
817817
}
818818
}

‎src/Symfony/Component/Console/Input/ArgvInput.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,14 @@ public function getFirstArgument()
275275
/**
276276
* {@inheritdoc}
277277
*/
278-
publicfunctionhasParameterOption($values)
278+
publicfunctionhasParameterOption($values,$flags = InputInterface::OPTION_FLAG_DEFAULT)
279279
{
280280
$values = (array)$values;
281281

282282
foreach ($this->tokensas$token) {
283+
if ($this->isEndOfOptions($token,$flags)) {
284+
break;
285+
}
283286
foreach ($valuesas$value) {
284287
if ($token ===$value ||0 ===strpos($token,$value.'=')) {
285288
returntrue;
@@ -293,14 +296,16 @@ public function hasParameterOption($values)
293296
/**
294297
* {@inheritdoc}
295298
*/
296-
publicfunctiongetParameterOption($values,$default =false)
299+
publicfunctiongetParameterOption($values,$default =false,$flags = InputInterface::OPTION_FLAG_DEFAULT)
297300
{
298301
$values = (array)$values;
299302
$tokens =$this->tokens;
300303

301304
while (0 <count($tokens)) {
302305
$token =array_shift($tokens);
303-
306+
if ($this->isEndOfOptions($token,$flags)) {
307+
break;
308+
}
304309
foreach ($valuesas$value) {
305310
if ($token ===$value ||0 ===strpos($token,$value.'=')) {
306311
if (false !==$pos =strpos($token,'=')) {
@@ -315,6 +320,23 @@ public function getParameterOption($values, $default = false)
315320
return$default;
316321
}
317322

323+
/**
324+
* private helper method to detect end of options in command-line arguments, used
325+
* to assist with a more POSIX compatible parsing of arguments.
326+
*
327+
* @param string $token
328+
* @param int $flags
329+
*
330+
* @return bool
331+
*/
332+
privatefunctionisEndOfOptions($token,$flags = InputInterface::OPTION_FLAG_DEFAULT)
333+
{
334+
return
335+
$flags & InputInterface::OPTION_FLAG_POSIX
336+
&& InputInterface::POSIX_OPTIONS_END ===$token
337+
;
338+
}
339+
318340
/**
319341
* Returns a stringified representation of the args passed to the command.
320342
*

‎src/Symfony/Component/Console/Input/ArrayInput.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function getFirstArgument()
5454
/**
5555
* {@inheritdoc}
5656
*/
57-
publicfunctionhasParameterOption($values)
57+
publicfunctionhasParameterOption($values,$flags = InputInterface::OPTION_FLAG_DEFAULT)
5858
{
5959
$values = (array)$values;
6060

@@ -63,6 +63,10 @@ public function hasParameterOption($values)
6363
$v =$k;
6464
}
6565

66+
if ($this->isEndOfOptions($v,$flags)) {
67+
break;
68+
}
69+
6670
if (in_array($v,$values)) {
6771
returntrue;
6872
}
@@ -74,11 +78,14 @@ public function hasParameterOption($values)
7478
/**
7579
* {@inheritdoc}
7680
*/
77-
publicfunctiongetParameterOption($values,$default =false)
81+
publicfunctiongetParameterOption($values,$default =false,$flags = InputInterface::OPTION_FLAG_DEFAULT)
7882
{
7983
$values = (array)$values;
8084

8185
foreach ($this->parametersas$k =>$v) {
86+
if ($this->isEndOfOptions(is_int($k) ?$v :$k,$flags)) {
87+
break;
88+
}
8289
if (is_int($k)) {
8390
if (in_array($v,$values)) {
8491
returntrue;
@@ -91,6 +98,23 @@ public function getParameterOption($values, $default = false)
9198
return$default;
9299
}
93100

101+
/**
102+
* private helper method to detect end of options in command-line arguments, used
103+
* to assist with a more POSIX compatible parsing of arguments.
104+
*
105+
* @param string $token
106+
* @param int $flags
107+
*
108+
* @return bool
109+
*/
110+
privatefunctionisEndOfOptions($token,$flags = InputInterface::OPTION_FLAG_DEFAULT)
111+
{
112+
return
113+
$flags & InputInterface::OPTION_FLAG_POSIX
114+
&& InputInterface::POSIX_OPTIONS_END ===$token
115+
;
116+
}
117+
94118
/**
95119
* Returns a stringified representation of the args passed to the command.
96120
*

‎src/Symfony/Component/Console/Input/InputInterface.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
*/
1919
interface InputInterface
2020
{
21+
/**
22+
* @see hasParameterOption()
23+
*/
24+
constOPTION_FLAG_DEFAULT =0;
25+
constOPTION_FLAG_POSIX =1;
26+
constPOSIX_OPTIONS_END ='--';
27+
2128
/**
2229
* Returns the first argument from the raw parameters (not parsed).
2330
*
@@ -32,23 +39,32 @@ public function getFirstArgument();
3239
* before they have been validated. It must be used carefully.
3340
*
3441
* @param string|array $values The values to look for in the raw parameters (can be an array)
42+
* @param int $flags Parse flags
43+
*
44+
* How far to look for options:
45+
*
46+
* OPTION_FLAG_DEFAULT: (default) scan all arguments incl. operands
47+
* OPTION_FLAG_POSIX: Do not search for option(s) after POSIX_OPTIONS_END ("--")
3548
*
3649
* @return bool true if the value is contained in the raw parameters
3750
*/
38-
publicfunctionhasParameterOption($values);
51+
publicfunctionhasParameterOption($values,$flags =self::OPTION_FLAG_DEFAULT);
3952

4053
/**
4154
* Returns the value of a raw option (not parsed).
4255
*
4356
* This method is to be used to introspect the input parameters
4457
* before they have been validated. It must be used carefully.
4558
*
59+
* @see hasParameterOption() for $flags
60+
*
4661
* @param string|array $values The value(s) to look for in the raw parameters (can be an array)
4762
* @param mixed $default The default value to return if no result is found
63+
* @param int $flags Parse flags
4864
*
4965
* @return mixed The option value
5066
*/
51-
publicfunctiongetParameterOption($values,$default =false);
67+
publicfunctiongetParameterOption($values,$default =false,$flags =self::OPTION_FLAG_DEFAULT);
5268

5369
/**
5470
* Binds the current Input instance with the given arguments and options.
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Tom Klingenberg <https://github.com/ktomk/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Console\Tests\Input;
13+
14+
usePHPUnit\Framework\TestCase;
15+
useSymfony\Component\Console\Input\ArgvInput;
16+
useSymfony\Component\Console\Input\ArrayInput;
17+
useSymfony\Component\Console\Input\InputInterface;
18+
19+
/**
20+
* Extra test-case to cover the introduction of $flags for InputInterface::hasParameterOption()
21+
* and InputInterface::getParameterOption().
22+
*/
23+
class ExtraInputsPosixOptionsTestextends TestCase
24+
{
25+
publicfunctiontestArrayInput()
26+
{
27+
$input =newArrayInput(array('name' =>'Fabien','--foo' =>'bar','--','--bar','--baz' =>'foo'));
28+
$this->hasParameterAssertions($input);
29+
$this->getParameterAssertions($input);
30+
}
31+
32+
publicfunctiontestArgvInput()
33+
{
34+
$input =newArgvInput(array('Fabien','--foo','bar','--','--bar','--baz','foo'));
35+
$this->hasParameterAssertions($input);
36+
$this->getParameterAssertions($input);
37+
}
38+
39+
privatefunctionhasParameterAssertions(InputInterface$input)
40+
{
41+
$this->assertInputHasParameterOption($input,'--foo','--foo always exists');
42+
$this->assertInputNeverHasParameterOption($input,'--zzz','--zzz never exists');
43+
$this->assertInputHasParameterOption(
44+
$input,
45+
array('--zzz','--foo'),
46+
'--foo is found with --zzz that never exists'
47+
);
48+
$this->assertInputHasOnlyNotParameterOption($input,'--bar','--bar is not found');
49+
$this->assertInputHasParameterOption($input,array('--bar','--foo'),'--foo is found even --bar is not found');
50+
$this->assertInputHasOnlyNotParameterOption($input,'--baz');
51+
52+
$this->assertTrue($input->hasParameterOption('--bar'),'Default behaviour');
53+
$this->assertFalse($input->hasParameterOption('--bar',$input::OPTION_FLAG_POSIX),'Posix flag');
54+
55+
$this->assertFalse($input->hasParameterOption(array('--bar'),$input::OPTION_FLAG_POSIX),'Posix flag');
56+
$this->assertFalse(
57+
$input->hasParameterOption(array('--bar','--baz'),$input::OPTION_FLAG_POSIX),
58+
'Posix flag'
59+
);
60+
$this->assertTrue(
61+
$input->hasParameterOption(array('--bar','--baz','--foo'),$input::OPTION_FLAG_POSIX),
62+
'Posix flag'
63+
);
64+
}
65+
66+
/**
67+
* Assert that regardless of flags, input never has an option.
68+
*
69+
* @param InputInterface $input
70+
* @param $values
71+
* @param null $message
72+
*/
73+
privatefunctionassertInputNeverHasParameterOption(InputInterface$input,$values,$message =null)
74+
{
75+
if (strlen($message)) {
76+
$message =" ($message)";
77+
}
78+
79+
$this->assertFalse($input->hasParameterOption($values),"default flag$message");
80+
$this->assertFalse($input->hasParameterOption($values,$input::OPTION_FLAG_POSIX),"posix flag$message");
81+
}
82+
83+
/**
84+
* Assert that regardless of the flag the values are available.
85+
*
86+
* @param InputInterface $input
87+
* @param $values
88+
* @param null $message
89+
*/
90+
privatefunctionassertInputHasParameterOption(InputInterface$input,$values,$message =null)
91+
{
92+
if (strlen($message)) {
93+
$message =" ($message)";
94+
}
95+
96+
$this->assertTrue($input->hasParameterOption($values),"default flag$message");
97+
$this->assertTrue($input->hasParameterOption($values,$input::OPTION_FLAG_POSIX),"posix flag$message");
98+
}
99+
100+
/**
101+
* Assert that with POSIX flag the values are not available, but are available by default.
102+
*
103+
* @param InputInterface $input
104+
* @param $values
105+
* @param null $message
106+
*/
107+
privatefunctionassertInputHasOnlyNotParameterOption(InputInterface$input,$values,$message =null)
108+
{
109+
if (strlen($message)) {
110+
$message =" ($message)";
111+
}
112+
113+
$this->assertTrue($input->hasParameterOption($values),"default flag$message");
114+
$this->assertFalse($input->hasParameterOption($values,$input::OPTION_FLAG_POSIX),"posix flag$message");
115+
}
116+
117+
/**
118+
* @param $input
119+
*/
120+
privatefunctiongetParameterAssertions(InputInterface$input)
121+
{
122+
$this->assertEquals('bar',$input->getParameterOption('--foo'));
123+
$this->assertSame('bar',$input->getParameterOption('--foo',false,$input::OPTION_FLAG_POSIX));
124+
125+
$this->assertEquals('foo',$input->getParameterOption('--baz'));
126+
$this->assertSame(false,$input->getParameterOption('--baz',false,$input::OPTION_FLAG_POSIX));
127+
}
128+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp