Skip to content

Commit 8c84296

Browse files
committed
feat: support set option value by setOpt
1 parent 148d8db commit 8c84296

File tree

4 files changed

+61
-6
lines changed

4 files changed

+61
-6
lines changed

src/Contract/ParserInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ public function hasOpt(string $name): bool;
108108
*/
109109
public function getOpt(string $name, $default = null);
110110

111+
/**
112+
* @param string $name
113+
* @param mixed $value
114+
*
115+
* @return mixed
116+
*/
117+
public function setOpt(string $name, $value): void;
118+
111119
/**
112120
* @param string|int $nameOrIndex
113121
*

src/Flag/AbstractFlag.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Toolkit\Stdlib\OS;
2020
use function is_array;
2121
use function is_bool;
22+
use function is_scalar;
2223
use function trim;
2324

2425
/**
@@ -190,7 +191,8 @@ public function setValue($value): void
190191
$value = FlagType::fmtBasicTypeValue($this->type, $value);
191192

192193
// has validator
193-
if ($cb = $this->validator) {
194+
$cb = $this->validator;
195+
if ($cb && is_scalar($value)) {
194196
$ok = true;
195197
$ret = $cb($value, $this->name);
196198

@@ -206,7 +208,11 @@ public function setValue($value): void
206208
}
207209

208210
if ($this->isArray()) {
209-
$this->value[] = $value;
211+
if (is_array($value)) {
212+
$this->value = $value;
213+
} else {
214+
$this->value[] = $value;
215+
}
210216
} else {
211217
$this->value = $value;
212218
}
@@ -285,6 +291,14 @@ public function setName(string $name): void
285291
$this->name = $name;
286292
}
287293

294+
/**
295+
* @return array|false|float|int|string|null
296+
*/
297+
public function getTypeDefault()
298+
{
299+
return FlagType::getDefault($this->type);
300+
}
301+
288302
/**
289303
* @return mixed
290304
*/

src/Flags.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ protected function addMatched(Option $option): void
783783
*/
784784
public function hasOpt(string $name): bool
785785
{
786-
return isset($this->matched[$name]);
786+
return isset($this->options[$name]);
787787
}
788788

789789
/**
@@ -804,11 +804,30 @@ public function hasMatched(string $name): bool
804804
*/
805805
public function getOpt(string $name, $default = null)
806806
{
807-
if ($opt = $this->getOption($name)) {
807+
$opt = $this->getDefinedOption($name);
808+
if (!$opt) { // not exist option
809+
throw new FlagException("flag option '$name' is undefined");
810+
}
811+
812+
if ($opt->hasValue()) {
808813
return $opt->getValue();
809814
}
810815

811-
return $default;
816+
return $default ?? $opt->getTypeDefault();
817+
}
818+
819+
/**
820+
* @param string $name
821+
* @param mixed $value
822+
*/
823+
public function setOpt(string $name, $value): void
824+
{
825+
$opt = $this->getDefinedOption($name);
826+
if (!$opt) { // not exist option
827+
throw new FlagException("flag option '$name' is undefined");
828+
}
829+
830+
$opt->setValue($value);
812831
}
813832

814833
/**

src/SFlags.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ protected function addArgDefine(array $define): void
740740
*/
741741
public function hasOpt(string $name): bool
742742
{
743-
return isset($this->opts[$name]);
743+
return isset($this->optDefines[$name]);
744744
}
745745

746746
/**
@@ -784,6 +784,20 @@ public function getOpt(string $name, $default = null)
784784
return $default ?? FlagType::getDefault($define['type']);
785785
}
786786

787+
/**
788+
* @param string $name
789+
* @param mixed $value
790+
*/
791+
public function setOpt(string $name, $value): void
792+
{
793+
$define = $this->optDefines[$name] ?? [];
794+
if (!$define) { // not exist option
795+
throw new FlagException("flag option '$name' is undefined");
796+
}
797+
798+
$this->opts[$name] = FlagType::fmtBasicTypeValue($define['type'], $value);
799+
}
800+
787801
/**
788802
* @return array
789803
*/

0 commit comments

Comments
 (0)