Skip to content

Commit 82652ec

Browse files
committed
options: add exceptions
1 parent 74f5a5c commit 82652ec

File tree

10 files changed

+298
-34
lines changed

10 files changed

+298
-34
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/*
4+
*
5+
* SimpleFramework
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* @author iTX Technologies
13+
* @link https://itxtech.org
14+
*
15+
*/
16+
17+
namespace iTXTech\SimpleFramework\Console\Option\Exception;
18+
19+
use iTXTech\SimpleFramework\Console\Option\Option;
20+
use iTXTech\SimpleFramework\Console\Option\OptionGroup;
21+
22+
class AlreadySelectedException extends ParseException{
23+
/** @var OptionGroup */
24+
private $group;
25+
/** @var Option */
26+
private $option;
27+
28+
public function __construct(OptionGroup $group, Option $option){
29+
parent::__construct("The option '" . $option->getKey() . "' was specified but an option from this group "
30+
. "has already been selected: '" . $group->getSelected() . "'");
31+
$this->group = $group;
32+
$this->option = $option;
33+
}
34+
35+
/**
36+
* Returns the option that was added to the group and triggered the exception.
37+
*
38+
* @return Option
39+
*/
40+
public function getOption() : Option{
41+
return $this->option;
42+
}
43+
44+
/**
45+
* Returns the option group where another option has been selected.
46+
*
47+
* @return OptionGroup
48+
*/
49+
public function getGroup() : OptionGroup{
50+
return $this->group;
51+
}
52+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
*
5+
* SimpleFramework
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* @author iTX Technologies
13+
* @link https://itxtech.org
14+
*
15+
*/
16+
17+
namespace iTXTech\SimpleFramework\Console\Option\Exception;
18+
19+
class AmbiguousOptionException extends UnrecognizedOptionException{
20+
/** @var array */
21+
private $matchingOptions;
22+
23+
public function __construct(string $option, array $matchingOptions){
24+
parent::__construct($this->createMessage($option, $matchingOptions), $option);
25+
$this->matchingOptions = $matchingOptions;
26+
}
27+
28+
public function getMatchingOptions() : array{
29+
return $this->matchingOptions;
30+
}
31+
32+
private function createMessage(string $option, array $matchingOptions){
33+
$buf = "Ambiguous option:" . $option . "' (could be: ";
34+
foreach($matchingOptions as $option){
35+
$buf .= "'" . $option . "', ";
36+
}
37+
$buf = substr($buf, 0, strlen($buf) - 2);
38+
return $buf;
39+
}
40+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
*
5+
* SimpleFramework
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* @author iTX Technologies
13+
* @link https://itxtech.org
14+
*
15+
*/
16+
17+
namespace iTXTech\SimpleFramework\Console\Option\Exception;
18+
19+
use iTXTech\SimpleFramework\Console\Option\Option;
20+
21+
class MissingArgumentException extends ParseException{
22+
/** @var Option */
23+
private $option;
24+
25+
public function __construct(Option $option){
26+
parent::__construct("Missing argument for option: " . $option->getKey());
27+
$this->option = $option;
28+
}
29+
30+
public function getOption() : Option{
31+
return $this->option;
32+
}
33+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
*
5+
* SimpleFramework
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* @author iTX Technologies
13+
* @link https://itxtech.org
14+
*
15+
*/
16+
17+
namespace iTXTech\SimpleFramework\Console\Option\Exception;
18+
19+
use iTXTech\SimpleFramework\Console\Option\OptionGroup;
20+
21+
class MissingOptionException extends ParseException{
22+
/** @var array */
23+
private $missingOptions;
24+
25+
public function __construct(array $missingOptions){
26+
parent::__construct($this->createMessage($missingOptions));
27+
$this->missingOptions = $missingOptions;
28+
}
29+
30+
/**
31+
* @return string[]|OptionGroup[]
32+
*/
33+
public function getMissingOptions() : array{
34+
return $this->missingOptions;
35+
}
36+
37+
private function createMessage(array $missingOptions) : string{
38+
$buf = "Missing required option" . (count($missingOptions) === 1 ? "" : "s") . ": ";
39+
$buf .= implode(", ", $missingOptions);
40+
41+
return $buf;
42+
}
43+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
*
5+
* SimpleFramework
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* @author iTX Technologies
13+
* @link https://itxtech.org
14+
*
15+
*/
16+
17+
namespace iTXTech\SimpleFramework\Console\Option\Exception;
18+
19+
class ParseException extends \Exception{
20+
public function __construct(string $message){
21+
parent::__construct($message, 0, null);
22+
}
23+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
*
5+
* SimpleFramework
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* @author iTX Technologies
13+
* @link https://itxtech.org
14+
*
15+
*/
16+
17+
namespace iTXTech\SimpleFramework\Console\Option\Exception;
18+
19+
use iTXTech\SimpleFramework\Console\Option\Option;
20+
21+
class UnrecognizedOptionException extends ParseException{
22+
/** @var Option */
23+
private $option;
24+
25+
public function __construct(string $message, string $option){
26+
parent::__construct($message);
27+
$this->option = $option;
28+
}
29+
30+
/**
31+
* Returns the unrecognized option.
32+
*
33+
* @return Option
34+
*/
35+
public function getOption() : Option{
36+
return $this->option;
37+
}
38+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ public function getArgName(){
276276
* @param string|null $footer
277277
*
278278
* @return string
279-
* @throws \Exception
279+
* @throws \InvalidArgumentException
280280
*/
281281
public function generateHelp(string $cmdLineSyntax, Options $options, bool $autoUsage = false,
282282
int $width = self::DEFAULT_VALUE, int $leftPad = self::DEFAULT_VALUE,
@@ -292,7 +292,7 @@ public function generateHelp(string $cmdLineSyntax, Options $options, bool $auto
292292
}
293293

294294
if($cmdLineSyntax == null || strlen($cmdLineSyntax) == 0){
295-
throw new \Exception("cmdLineSyntax not provided");
295+
throw new \InvalidArgumentException("cmdLineSyntax not provided");
296296
}
297297

298298
$text = "";
@@ -340,8 +340,8 @@ public function generateUsage(string &$text, int $width, string $app, Options $o
340340
if($group !== null){
341341
if(!isset($processedGroups[spl_object_hash($group)])){
342342
$processedGroups[spl_object_hash($group)] = $group;
343+
$this->appendOptionGroup($buff, $group);
343344
}
344-
$this->appendOptionGroup($buff, $group);
345345
}else{
346346
$this->appendOption($buff, $option, $option->isRequired());
347347
}

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,11 @@ public function getArgs() : int{
220220
*
221221
* @param string $value
222222
*
223-
* @throws \Exception
223+
* @throws \RuntimeException
224224
*/
225225
public function addValueForProcessing(string $value){
226226
if($this->numberOfArgs == self::UNINITIALIZED){
227-
throw new \Exception("NO_ARGS_ALLOWED");
227+
throw new \RuntimeException("NO_ARGS_ALLOWED");
228228
}
229229
$this->processValue($value);
230230
}
@@ -237,8 +237,6 @@ public function addValueForProcessing(string $value){
237237
* added as a single token.
238238
*
239239
* @param string
240-
*
241-
* @throws \Exception
242240
*/
243241
private function processValue(string $value){
244242
// this Option has a separator character
@@ -278,11 +276,11 @@ private function processValue(string $value){
278276
*
279277
* @param string $value
280278
*
281-
* @throws \Exception
279+
* @throws \RuntimeException
282280
*/
283281
private function add(string $value){
284282
if(!$this->acceptsArg()){
285-
throw new \Exception("Cannot add value, list full.");
283+
throw new \RuntimeException("Cannot add value, list full.");
286284
}
287285

288286
// store value
@@ -296,8 +294,6 @@ private function add(string $value){
296294
* @param int $index
297295
*
298296
* @return string
299-
*
300-
* @throws \Exception
301297
*/
302298
public function getValue(int $index = 0) : string{
303299
return $this->hasNoValues() ? null : $this->values[$index];
@@ -310,8 +306,6 @@ public function getValue(int $index = 0) : string{
310306
* @param $defaultValue string
311307
*
312308
* @return string
313-
*
314-
* @throws \Exception
315309
*/
316310
public function getValueDef(string $defaultValue) : string{
317311
$value = $this->getValue();

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

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
namespace iTXTech\SimpleFramework\Console\Option;
1818

19+
use iTXTech\SimpleFramework\Console\Option\Exception\AlreadySelectedException;
20+
1921
class OptionGroup{
2022

2123
/** hold the options */
@@ -26,7 +28,7 @@ class OptionGroup{
2628
private $selected;
2729

2830
/** specified whether this group is required */
29-
private $required;
31+
private $required = false;
3032

3133
/**
3234
* Add the specified <code>Option</code> to this group.
@@ -53,13 +55,13 @@ public function getOptions() : array{
5355
*
5456
* @param $option Option
5557
*
56-
* @return bool
58+
* @throws AlreadySelectedException
5759
*/
58-
public function setSelected(?Option $option) : bool{
60+
public function setSelected(?Option $option){
5961
if($option == null){
6062
// reset the option previously selected
6163
$this->selected = null;
62-
return true;
64+
return;
6365
}
6466

6567
// if no option has already been selected or the
@@ -68,9 +70,8 @@ public function setSelected(?Option $option) : bool{
6870
if($this->selected == null || $this->selected === $option->getKey()){
6971
$this->selected = $option->getKey();
7072
}else{
71-
return false;
73+
throw new AlreadySelectedException($this, $option);
7274
}
73-
return true;
7475
}
7576

7677
/**
@@ -95,4 +96,24 @@ public function setRequired(bool $required){
9596
public function isRequired() : bool{
9697
return $this->required;
9798
}
99+
100+
public function __toString(){
101+
$buf = "[";
102+
foreach($this->getOptions() as $option){
103+
if($option->getOpt() !== null){
104+
$buf .= "-" . $option->getOpt();
105+
}else{
106+
$buf .= "--" . $option->getLongOpt();
107+
}
108+
109+
if($option->getDescription() !== null){
110+
$buf .= " " . $option->getDescription();
111+
}
112+
113+
$buf .= ", ";
114+
}
115+
$buf = substr($buf, 0, strlen($buf));
116+
117+
return $buf;
118+
}
98119
}

0 commit comments

Comments
 (0)