-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from chop1k/v2.1.0-dev
v2.1.0
- Loading branch information
Showing
21 changed files
with
1,643 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Argument | ||
Argument is a helper class used by Distributor and Formatter. | ||
|
||
You have to use this class if you want to override Distributor or Formatter instead of implementing the appropriate interfaces, because both use the constants and the methods of this class everywhere. | ||
|
||
Argument defines argument-types: | ||
- TYPE_OPTION - Option type. The option looks like "--option". | ||
- TYPE_ABBREVIATION - Abbreviated option type. The abbreviation looks like "-a". | ||
- TYPE_ABBREVIATIONS - List of abbreviated options. The options look like "-abs". | ||
- TYPE_VALUE - Value type. The value looks like "value". | ||
- TYPE_PURE_VALUE - Pure value type. It looks like value type but without quotes. | ||
- TYPE_EQUAL_SEPARATED_OPTION - Equal-separated option type. It looks like "--option=value". | ||
- TYPE_EQUAL_SEPARATED_ABBREVIATION - Equal-separated abbreviation type. It looks like "-a=value". | ||
- TYPE_EQUAL_SEPARATED_ABBREVIATIONS - Equal-separated abbreviations type. It looks like "-abs=value". | ||
|
||
Argument defines functions for defining types: | ||
- isOption - Returns true if argument is option. | ||
- isAbbreviation - Returns true if the argument is an abbreviated option. | ||
- isAbbreviations - Returns true if the argument is an array of abbreviated options. | ||
- isValue - Returns true if the argument is a value. | ||
- isPureValue - Returns true if the argument is a pure value. | ||
- isEqualSeparated - Returns true if option is an equal separated option. | ||
- isCommand - Returns true if the argument is a command. | ||
- isUtility - Returns true if the argument is a utility. | ||
|
||
Argument defines functions for type conversion: | ||
- toOption - Adds prefix to the argument and returns it. | ||
- toAbbreviation - Adds prefix to the argument and returns it. | ||
- toValue - Adds quotes to the argument and returns it. | ||
- toPureValue - Clears the argument and returns it. | ||
- toEqualSeparated - Adds equal-separation between option and value. | ||
|
||
Other methods: | ||
- clear - Trims special symbols. | ||
- explodeEqualSeparated - Shortcut for exploding a given argument if the argument is equal separated option. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# Formatter | ||
Formatter is used by Distributor to formatting given arguments to given types. Also, it defines a type of the argument and returns it. | ||
|
||
Formatter implements FormatterInterface interface. | ||
|
||
```php | ||
use Consolly\Formatter\FormatterInterface; | ||
|
||
class MyFormatter implements FormatterInterface | ||
{ | ||
public function format($value, string $type): string | ||
{ | ||
// here you should format the value to given type. | ||
} | ||
|
||
public function parse(string $argument): string | ||
{ | ||
// here you should define type and return it. | ||
} | ||
} | ||
``` | ||
|
||
## Formatter class | ||
Formatter class is a default implementation of FormatterInterface interface which used by default by Distributor class. | ||
|
||
Formatter and Distributor both uses Argument helper class. | ||
|
||
```php | ||
use Consolly\Formatter\Formatter; | ||
use Consolly\Helper\Argument; | ||
|
||
$formatter = new Formatter(); | ||
|
||
// will return --option. | ||
$formatter->format('option', Argument::TYPE_OPTION); | ||
|
||
// will return -a. | ||
$formatter->format('a', Argument::TYPE_ABBREVIATION); | ||
|
||
// will return -abs. | ||
$formatter->format('abs', Argument::TYPE_ABBREVIATIONS); | ||
|
||
// will return "value". | ||
$formatter->format('value', Argument::TYPE_VALUE); | ||
|
||
// will return value. | ||
$formatter->format('value', Argument::TYPE_PURE_VALUE); | ||
$formatter->format('"value"', Argument::TYPE_PURE_VALUE); | ||
|
||
// will return --option=value | ||
$formatter->format(['option', 'value'], Argument::TYPE_EQUAL_SEPARATED_OPTION); | ||
|
||
// will return -a=value | ||
$formatter->format(['a', 'value'], Argument::TYPE_EQUAL_SEPARATED_ABBREVIATION); | ||
|
||
// will return -abs=value | ||
$formatter->format(['abs', 'value'], Argument::TYPE_EQUAL_SEPARATED_ABBREVIATIONS); | ||
|
||
// will return value of Argument::TYPE_ABBREVIATION constant. | ||
$formatter->parse('-a'); | ||
|
||
// will return value of Argument::TYPE_ABBREVIATIONS constant. | ||
$formatter->parse('-abs'); | ||
|
||
// will return value of Argument::TYPE_VALUE constant. | ||
$formatter->parse('"value"'); | ||
|
||
// will return value of Argument::TYPE_VALUE constant. | ||
$formatter->parse("'value'"); | ||
|
||
// will return value of Argument::TYPE_PURE_VALUE constant. | ||
$formatter->parse('value'); | ||
|
||
// will return value of Argument::EQUAL_SEPARATED_OPTION constant. | ||
$formatter->parse('--option=value'); | ||
|
||
// will return value of Argument::EQUAL_SEPARATED_ABBREVIATION constant. | ||
$formatter->parse('-a=value'); | ||
|
||
// will return value of Argument::EQUAL_SEPARATED_ABBREVIATIONS constant. | ||
$formatter->parse('-abs=value'); | ||
``` | ||
|
||
By default, the formatter does not distinguish the command and utility from pure value, because in order to distinguish a command or utility from a pure value, you need to know the location and type of the remaining arguments, the formatter is not able to do this because it only takes one value. | ||
|
||
```php | ||
use Consolly\Formatter\Formatter; | ||
use Consolly\Helper\Argument; | ||
|
||
$formatter = new Formatter(); | ||
|
||
// will return utility. | ||
$formatter->format('utility', Argument::TYPE_UTILITY); | ||
|
||
// will return command. | ||
$formatter->format('command', Argument::TYPE_COMMAND); | ||
|
||
// will return value of Argument::TYPE_PURE_VALUE constant. | ||
$formatter->parse('utility'); | ||
|
||
// will return value of Argument::TYPE_PURE_VALUE constant. | ||
$formatter->parse('command'); | ||
|
||
``` | ||
|
||
You can override the formatter isCommand, isUtility, toCommand, toUtility methods and write custom handling logic. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.