Skip to content

Commit b2cf381

Browse files
committed
feat: support read flag value from ENV var
1 parent 64bacf0 commit b2cf381

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ Generic PHP command line flags parse library
1010

1111
## Features
1212

13-
- Generic parse CLI options and arguments.
13+
- Generic CLI options and arguments parser.
1414
- Support set value data type(`int,string,bool,array`), will auto format input value.
1515
- Support set default value for option/argument.
16+
- Support read flag value from ENV var.
1617
- Support set option/argument is required.
1718
- Support set validator for check input value.
1819
- Support auto generate help message.
@@ -24,7 +25,7 @@ Generic PHP command line flags parse library
2425

2526
**Options**:
2627

27-
- Support long option `--long`
28+
- Support long option. eg: `--long` `--long value`
2829
- Support short option `-s -a value`, allow set multi short names.
2930
- Support define array option
3031
- eg: `--tag php --tag go` will get `tag: [php, go]`

src/AbstractFlags.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ abstract class AbstractFlags implements ParserInterface
6767
'showType' => '', // use for show help
6868
// 'index' => 0, // only for argument
6969
'required' => false,
70+
'envVar' => '', // support read value from ENV var
7071
'default' => null,
7172
'shorts' => [], // only for option. ['a', 'b']
7273
// value validator

src/Flag/AbstractFlag.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
use Toolkit\PFlag\Exception\FlagException;
1717
use Toolkit\PFlag\FlagType;
1818
use Toolkit\Stdlib\Obj;
19+
use Toolkit\Stdlib\OS;
1920
use function is_array;
2021
use function is_bool;
22+
use function trim;
2123

2224
/**
2325
* Class Flag
@@ -55,6 +57,13 @@ abstract class AbstractFlag implements ArrayAccess, FlagInterface
5557
*/
5658
protected $showType = '';
5759

60+
/**
61+
* ENV var name. support read value from ENV var
62+
*
63+
* @var string
64+
*/
65+
protected $envVar = '';
66+
5867
/**
5968
* The default value
6069
*
@@ -155,6 +164,11 @@ public function init(): void
155164
$this->default = FlagType::fmtBasicTypeValue($this->type, $this->default);
156165
$this->value = $this->default;
157166
}
167+
168+
// support set value from ENV.
169+
if ($this->envVar && ($envVal = OS::getEnvVal($this->envVar))) {
170+
$this->value = FlagType::fmtBasicTypeValue($this->type, $envVal);
171+
}
158172
}
159173

160174
/**
@@ -303,6 +317,7 @@ public function toArray(): array
303317
'desc' => $this->desc,
304318
'type' => $this->type,
305319
'default' => $this->default,
320+
'envVar' => $this->envVar,
306321
'required' => $this->required,
307322
'isArray' => $this->isArray(),
308323
'showType' => $this->getShowType(),
@@ -364,4 +379,20 @@ public function setValidator(?callable $validator): void
364379
{
365380
$this->validator = $validator;
366381
}
382+
383+
/**
384+
* @return string
385+
*/
386+
public function getEnvVar(): string
387+
{
388+
return $this->envVar;
389+
}
390+
391+
/**
392+
* @param string $envVar
393+
*/
394+
public function setEnvVar(string $envVar): void
395+
{
396+
$this->envVar = trim($envVar);
397+
}
367398
}

src/SFlags.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
namespace Toolkit\PFlag;
1111

12-
use Closure;
1312
use Toolkit\Cli\Helper\FlagHelper;
1413
use Toolkit\PFlag\Exception\FlagException;
14+
use Toolkit\Stdlib\OS;
1515
use function array_slice;
1616
use function current;
1717
use function explode;
@@ -577,16 +577,22 @@ protected function parseOptRules(array $rules): void
577577

578578
// has default value
579579
if (isset($define['default'])) {
580+
if ($define['required']) {
581+
throw new FlagException("cannot set a default value, if flag is required. flag: $name");
582+
}
583+
580584
$default = FlagType::fmtBasicTypeValue($type, $define['default']);
581585

582586
// save as value.
583587
$this->opts[$name] = $define['default'] = $default;
588+
}
584589

585-
if ($define['required']) {
586-
throw new FlagException("cannot set a default value, if flag is required. flag: $name");
587-
}
590+
// support read value from ENV var
591+
if ($define['envVar'] && ($envVal = OS::getEnvVal($define['envVar']))) {
592+
$this->opts[$name] = FlagType::fmtBasicTypeValue($type, $envVal);
588593
}
589594

595+
// has shorts
590596
if ($define['shorts']) {
591597
foreach ($define['shorts'] as $short) {
592598
$this->setAlias($name, $short, true);

0 commit comments

Comments
 (0)