Skip to content

Commit a458692

Browse files
committed
fix: not set value, should return typed default value
1 parent b2cf381 commit a458692

File tree

2 files changed

+86
-29
lines changed

2 files changed

+86
-29
lines changed

src/FlagType.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,42 @@ public static function getHelpName(string $type, bool $toUpper = true): string
114114
return $toUpper ? strtoupper($name) : $name;
115115
}
116116

117+
/**
118+
* Get type default value.
119+
*
120+
* @param string $type
121+
*
122+
* @return array|false|float|int|string|null
123+
*/
124+
public static function getDefault(string $type)
125+
{
126+
$value = null;
127+
switch ($type) {
128+
case self::INT:
129+
$value = 0;
130+
break;
131+
case self::BOOL:
132+
$value = false;
133+
break;
134+
case self::FLOAT:
135+
$value = (float)0;
136+
break;
137+
case self::STRING:
138+
$value = '';
139+
break;
140+
case self::INTS:
141+
case self::ARRAY:
142+
case self::STRINGS:
143+
$value = [];
144+
break;
145+
default:
146+
// nothing
147+
break;
148+
}
149+
150+
return $value;
151+
}
152+
117153
/**
118154
* @param string $type
119155
* @param mixed $value

src/SFlags.php

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ class SFlags extends AbstractFlags
122122
*
123123
* ```php
124124
* [
125-
* 'name' => self::DEFINE_ITEM,
125+
* self::DEFINE_ITEM,
126126
* ]
127127
* ```
128128
*
129-
* @var array
129+
* @var array[]
130130
*/
131131
private $argDefines = [];
132132

@@ -704,7 +704,16 @@ public function getOption(string $name, $default = null)
704704
*/
705705
public function getOpt(string $name, $default = null)
706706
{
707-
return $this->opts[$name] ?? $default;
707+
if (isset($this->opts[$name])) {
708+
return $this->opts[$name];
709+
}
710+
711+
$define = $this->optDefines[$name] ?? [];
712+
if (!$define) { // not exist option
713+
throw new FlagException("flag option '$name' is undefined");
714+
}
715+
716+
return $default ?? FlagType::getDefault($define['type']);
708717
}
709718

710719
/**
@@ -722,17 +731,9 @@ public function getOpts(): array
722731
*/
723732
public function hasArg($nameOrIndex): bool
724733
{
725-
if (is_string($nameOrIndex)) {
726-
if (!isset($this->name2index[$nameOrIndex])) {
727-
return false;
728-
}
729-
730-
$index = $this->name2index[$nameOrIndex];
731-
} else {
732-
$index = (int)$nameOrIndex;
733-
}
734+
$index = $this->getArgIndex($nameOrIndex);
734735

735-
return isset($this->args[$index]);
736+
return $index > -1 && isset($this->args[$index]);
736737
}
737738

738739
/**
@@ -754,17 +755,18 @@ public function getArgument($nameOrIndex, $default = null)
754755
*/
755756
public function getArg($nameOrIndex, $default = null)
756757
{
757-
if (is_string($nameOrIndex)) {
758-
if (!isset($this->name2index[$nameOrIndex])) {
759-
throw new FlagException("flag argument name '$nameOrIndex' is undefined");
760-
}
758+
$index = $this->getArgIndex($nameOrIndex);
759+
if ($index < 0) {
760+
throw new FlagException("flag argument '$nameOrIndex' is undefined");
761+
}
761762

762-
$index = $this->name2index[$nameOrIndex];
763-
} else {
764-
$index = (int)$nameOrIndex;
763+
if (isset($this->args[$index])) {
764+
return $this->args[$index];
765765
}
766766

767-
return $this->args[$index] ?? $default;
767+
// get default with type format
768+
$define = $this->argDefines[$index];
769+
return $default ?? FlagType::getDefault($define['type']);
768770
}
769771

770772
/**
@@ -780,19 +782,16 @@ public function getFirstArg($default = null)
780782
/**
781783
* @param string|int $nameOrIndex
782784
*
783-
* @return int
785+
* @return int Will return -1 if arg not exists
784786
*/
785-
public function getArgIndex($nameOrIndex): int
787+
protected function getArgIndex($nameOrIndex): int
786788
{
787789
if (!is_string($nameOrIndex)) {
788-
return (int)$nameOrIndex;
789-
}
790-
791-
if (!isset($this->name2index[$nameOrIndex])) {
792-
throw new FlagException("flag argument name '$nameOrIndex' is undefined");
790+
$index = (int)$nameOrIndex;
791+
return isset($this->argDefines[$index]) ? $index : -1;
793792
}
794793

795-
return $this->name2index[$nameOrIndex];
794+
return $this->name2index[$nameOrIndex] ?? -1;
796795
}
797796

798797
/**
@@ -842,6 +841,18 @@ public function addArgRule(string $name, $rule): self
842841
return $this;
843842
}
844843

844+
/**
845+
* @param string|int $nameOrIndex
846+
*
847+
* @return bool
848+
*/
849+
public function hasDefineArg($nameOrIndex): bool
850+
{
851+
$index = $this->getArgIndex($nameOrIndex);
852+
853+
return isset($this->argDefines[$index]);
854+
}
855+
845856
/**
846857
* @return array
847858
*/
@@ -881,6 +892,16 @@ public function addOptRule(string $name, $rule): self
881892
return $this;
882893
}
883894

895+
/**
896+
* @param string $name
897+
*
898+
* @return bool
899+
*/
900+
public function hasDefineOpt(string $name): bool
901+
{
902+
return isset($this->optDefines[$name]);
903+
}
904+
884905
/**
885906
* @return array
886907
*/

0 commit comments

Comments
 (0)