Skip to content

Commit 2db776c

Browse files
committed
fix: parse option name error on start with --
1 parent 4b66138 commit 2db776c

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

src/AbstractFlags.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828
use function is_callable;
2929
use function is_object;
3030
use function ksort;
31+
use function ltrim;
3132
use function method_exists;
3233
use function sprintf;
3334
use function strlen;
3435
use function strpos;
3536
use function trim;
37+
use function vdump;
3638

3739
/**
3840
* class AbstractFlags
@@ -693,15 +695,16 @@ protected function parseRuleOptName(string $key): array
693695

694696
// only name.
695697
if (strpos($key, ',') === false) {
696-
return [$key, []];
698+
$name = ltrim($key, '-');
699+
return [$name, []];
697700
}
698701

699702
$name = '';
700703
$keys = Str::explode($key, ',');
701704

702705
// TIP: first is the option name. remaining is shorts.
703706
$shorts = [];
704-
foreach ($keys as $i => $k) {
707+
foreach ($keys as $k) {
705708
// support like '--name, -n'
706709
$k = ltrim($k, '-');
707710

src/Contract/ParserInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,10 @@ public function getOpts(): array;
7474
* @return array
7575
*/
7676
public function getArgs(): array;
77+
78+
/**
79+
* @return array
80+
* @psalm-return array<string, string>
81+
*/
82+
public function getOptSimpleDefines(): array;
7783
}

src/Flags.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,24 @@ public function getOpts(): array
853853
return $opts;
854854
}
855855

856+
/**
857+
* @return array
858+
*/
859+
public function getOptSimpleDefines(): array
860+
{
861+
$map = [];
862+
foreach ($this->options as $name => $define) {
863+
$names = $define['shorts'];
864+
$names[] = $name;
865+
866+
$helpName = FlagUtil::buildOptHelpName($names);
867+
868+
$map[$helpName] = $define['desc'];
869+
}
870+
871+
return $map;
872+
}
873+
856874
/**
857875
* @param string $name
858876
*

src/SFlags.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use function strpos;
2424
use function substr;
2525
use function trim;
26+
use function vdump;
2627

2728
/**
2829
* Class SFlags
@@ -761,6 +762,24 @@ public function getArgs(): array
761762
* helper methods
762763
***************************************************************/
763764

765+
/**
766+
* @return array
767+
*/
768+
public function getOptSimpleDefines(): array
769+
{
770+
$map = [];
771+
foreach ($this->optDefines as $name => $define) {
772+
$names = $define['shorts'];
773+
$names[] = $name;
774+
775+
$helpName = FlagUtil::buildOptHelpName($names);
776+
777+
$map[$helpName] = $define['desc'];
778+
}
779+
780+
return $map;
781+
}
782+
764783
/**
765784
* @param string|int $nameOrIndex
766785
*

0 commit comments

Comments
 (0)