Skip to content

Commit 74f5a5c

Browse files
committed
options: add Parser
1 parent 3637534 commit 74f5a5c

File tree

5 files changed

+423
-16
lines changed

5 files changed

+423
-16
lines changed

src/iTXTech/SimpleFramework/Console/Option/CommandLine.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function getOptionValue($option, ?string $defaultValue = null) : ?string{
5959
if(is_string($option)){
6060
$option = $this->resolveOption($option);
6161
}
62-
if($option == null){
62+
if($option === null){
6363
return null;
6464
}
6565

@@ -83,7 +83,7 @@ public function getOptionValues($option){
8383

8484
foreach($this->options as $processedOption){
8585
if($processedOption === $option){
86-
array_merge($values, $processedOption->getValues());
86+
$values = array_merge($values, $processedOption->getValues());
8787
}
8888
}
8989

@@ -134,7 +134,7 @@ public function addArg(string $arg){
134134
*
135135
* @param Option $opt
136136
*/
137-
protected function addOption(Option $opt){
137+
public function addOption(Option $opt){
138138
$this->options[] = $opt;
139139
}
140140

src/iTXTech/SimpleFramework/Console/Option/OptionGroup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function getOptions() : array{
5555
*
5656
* @return bool
5757
*/
58-
public function setSelected(Option $option) : bool{
58+
public function setSelected(?Option $option) : bool{
5959
if($option == null){
6060
// reset the option previously selected
6161
$this->selected = null;

src/iTXTech/SimpleFramework/Console/Option/Options.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Options{
4545
*/
4646
public function addOptionGroup(OptionGroup $group){
4747
if($group->isRequired()){
48-
$this->requiredOpts[] = $group;
48+
$this->requiredOpts[spl_object_hash($group)] = $group;
4949
}
5050
foreach($group->getOptions() as $option){
5151
$option->setRequired(false);
@@ -101,14 +101,8 @@ public function addOption(Option $opt){
101101

102102
// if the option is required add it to the required list
103103
if($opt->isRequired()){
104-
if(in_array($key, $this->requiredOpts)){
105-
foreach($this->requiredOpts as $k => $opt){
106-
if($k === $key){
107-
unset($this->requiredOpts[$k]);
108-
}
109-
}
110-
}
111-
$this->requiredOpts[] = $key;
104+
unset($this->requiredOpts[$key]);
105+
$this->requiredOpts[$key] = $key;
112106
}
113107

114108
$this->shortOpts[$key] = $opt;
@@ -141,14 +135,14 @@ public function getRequiredOptions() : array{
141135
*
142136
* @return Option
143137
*/
144-
public function getOption(string $opt) : Option{
138+
public function getOption(string $opt) : ?Option{
145139
$opt = Util::stripLeadingHyphens($opt);
146140

147141
if(isset($this->shortOpts[$opt])){
148142
return $this->shortOpts[$opt];
149143
}
150144

151-
return $this->longOpts[$opt];
145+
return $this->longOpts[$opt] ?? null;
152146
}
153147

154148
/**
@@ -158,7 +152,7 @@ public function getOption(string $opt) : Option{
158152
*
159153
* @return string[]
160154
*/
161-
public function getMatchingOptions(string $opt) : string{
155+
public function getMatchingOptions(string $opt) : array{
162156
$opt = Util::stripLeadingHyphens($opt);
163157

164158
$matchingOpts = [];

0 commit comments

Comments
 (0)