Skip to content

Commit cceec25

Browse files
committed
Add flag: valueEscape to option methods
1 parent c4d357d commit cceec25

4 files changed

Lines changed: 24 additions & 14 deletions

File tree

src/Concerns/HasSettings.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,19 @@ public function addCutOption(string $option): ShellCommandInterface
7272
return $this;
7373
}
7474

75-
public function addCutOptionWithValue(string $option, string $value): ShellCommandInterface
76-
{
77-
$this->addSetting(new CutOption($option, $value));
75+
public function addCutOptionWithValue(
76+
string $option,
77+
string $value,
78+
bool $valueEscape = false
79+
): ShellCommandInterface {
80+
$this->addSetting(new CutOption($option, $value, $valueEscape));
7881

7982
return $this;
8083
}
8184

82-
public function addOptionWithValue(string $option, string $value): ShellCommandInterface
85+
public function addOptionWithValue(string $option, string $value, bool $valueEscape = false): ShellCommandInterface
8386
{
84-
$this->addSetting(new Option($option, $value));
87+
$this->addSetting(new Option($option, $value, $valueEscape));
8588

8689
return $this;
8790
}

src/Interfaces/ShellCommandInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ public function addCutOption(string $option): self;
4949
* Add cut option with value into command line
5050
* @return $this
5151
*/
52-
public function addCutOptionWithValue(string $option, string $value): self;
52+
public function addCutOptionWithValue(string $option, string $value, bool $valueEscape = false): self;
5353

5454
/**
5555
* Add option with value into command line
5656
*/
57-
public function addOptionWithValue(string $option, string $value): self;
57+
public function addOptionWithValue(string $option, string $value, bool $valueEscape = false): self;
5858

5959
/**
6060
* @return string

src/Settings/CutOption.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,4 @@
55
class CutOption extends Option
66
{
77
protected static $prefix = '-';
8-
9-
public function __toString(): string
10-
{
11-
return '-'. $this->option . ($this->value ? '=' . $this->value : '');
12-
}
138
}

src/Settings/Option.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@ class Option implements ShellSettingInterface
1212

1313
protected $value;
1414

15-
public function __construct(string $option, ?string $value = null)
15+
protected $valueEscape;
16+
17+
public function __construct(string $option, ?string $value = null, bool $valueEscape = false)
1618
{
1719
$this->option = $option;
1820
$this->value = $value;
21+
$this->valueEscape = $valueEscape;
1922
}
2023

2124
public function __toString(): string
2225
{
23-
return '--'. $this->option . ($this->value ? '=' . $this->value : '');
26+
return static::$prefix. $this->option . ($this->value ? '=' . $this->valueToString() : '');
2427
}
2528

2629
public static function is(string $raw): bool
@@ -46,4 +49,13 @@ public static function explodeAttributesFromRaw(string $raw): array
4649

4750
return explode('=', $raw);
4851
}
52+
53+
protected function valueToString(): string
54+
{
55+
if ($this->value === null) {
56+
return '';
57+
}
58+
59+
return $this->valueEscape ? escapeshellarg($this->value) : $this->value;
60+
}
4961
}

0 commit comments

Comments
 (0)