Skip to content

Commit 7399699

Browse files
committed
Merge branch 'review-clioption-value-name'
2 parents 496db41 + a1ea3aa commit 7399699

File tree

7 files changed

+50
-12
lines changed

7 files changed

+50
-12
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"license": "MIT",
55
"description": "A lightweight PHP toolkit for expressive backend/CLI apps",
66
"require": {
7-
"php": ">=7.4 <8.3",
7+
"php": ">=7.4",
88
"composer-runtime-api": "^2.2",
99
"filp/whoops": "^2",
1010
"lkrms/dice": "^4.1.6",

src/Cli/CliOption.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Lkrms\Contract\IReadable;
1919
use Lkrms\Facade\Assert;
2020
use Lkrms\Facade\Console;
21+
use Lkrms\Support\Catalog\CharacterSequence as Char;
2122
use Lkrms\Utility\Convert;
2223
use Lkrms\Utility\Env;
2324
use Lkrms\Utility\Test;
@@ -327,7 +328,7 @@ public function __construct(
327328
$this->Short = $this->IsPositional ? null : ($short ?: null);
328329
$this->Key = $this->IsPositional ? $long : ($short . '|' . $long);
329330
$this->ValueName = $this->IsFlag ? null : ($valueName ?: ($this->IsPositional ? Convert::toSnakeCase($long, '=') : null) ?: 'value');
330-
$this->DisplayName = $this->IsPositional ? $this->getFriendlyValueName() : ($long ? '--' . $long : '-' . $short);
331+
$this->DisplayName = $this->IsPositional ? $this->getFriendlyValueName(false) : ($long ? '--' . $long : '-' . $short);
331332
$this->ValueType = $this->IsFlag ? ($multipleAllowed ? CliOptionValueType::INTEGER : CliOptionValueType::BOOLEAN) : $valueType;
332333
$this->Delimiter = $multipleAllowed && !$this->IsFlag ? $delimiter : null;
333334
$this->Description = $description;
@@ -411,16 +412,28 @@ public function getNames(): array
411412
/**
412413
* Format the option's value name
413414
*
415+
* If {@see CliOption::$ValueName} contains angle brackets (`<`, `>`), it is
416+
* returned as-is, otherwise:
417+
*
418+
* - if it contains uppercase characters and no lowercase characters, it is
419+
* converted to kebab-case and capitalised
420+
* - if not, it is converted to kebab-case and enclosed between angle
421+
* brackets
422+
*
423+
* In conversions to kebab-case, `=` is preserved.
414424
*/
415-
public function getFriendlyValueName(): ?string
425+
public function getFriendlyValueName(bool $withMarkup = true): ?string
416426
{
417-
$name = $this->ValueName;
418-
$upper = $name === strtoupper($name);
419-
$name = Convert::toKebabCase($name, '=');
420-
if ($upper) {
421-
return '<' . strtoupper($name) . '>';
427+
if ($this->ValueName === null || strpbrk($this->ValueName, '<>') !== false) {
428+
return $this->ValueName;
429+
}
430+
431+
if (strpbrk($this->ValueName, Char::ALPHABETIC_UPPER) !== false &&
432+
strpbrk($this->ValueName, Char::ALPHABETIC_LOWER) === false) {
433+
[$before, $after] = $withMarkup ? ['<', '>'] : ['', ''];
434+
return $before . strtoupper(Convert::toKebabCase($this->ValueName, '=')) . $after;
422435
}
423-
return "<$name>";
436+
return '<' . Convert::toKebabCase($this->ValueName, '=') . '>';
424437
}
425438

426439
/**

src/Cli/CliOptionBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* @method static $this build(?IContainer $container = null) Create a new CliOptionBuilder (syntactic sugar for 'new CliOptionBuilder()')
1616
* @method $this long(?string $value) The long form of the option, e.g. 'verbose' (see {@see CliOption::$Long})
1717
* @method $this short(?string $value) The short form of the option, e.g. 'v' (see {@see CliOption::$Short})
18-
* @method $this valueName(?string $value) The name of the option's value as it appears in help messages (see {@see CliOption::$ValueName})
18+
* @method $this valueName(?string $value) The name of the option's value as it appears in usage information (see {@see CliOption::$ValueName})
1919
* @method $this description(?string $value) A description of the option (see {@see CliOption::$Description})
2020
* @method $this optionType(CliOptionType::* $value) The option's type (see {@see CliOption::$OptionType})
2121
* @method $this valueType(CliOptionValueType::* $value) The data type of the option's value (see {@see CliOption::$ValueType})

src/Facade/Compute.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Lkrms\Facade;
44

55
use Lkrms\Concept\Facade;
6+
use Lkrms\Support\Catalog\CharacterSequence as Char;
67
use Lkrms\Utility\Computations;
78

89
/**
@@ -14,7 +15,7 @@
1415
* @method static void unload() Clear the underlying Computations instance
1516
* @method static string binaryHash(...$value) Generate a unique non-crypto hash and return raw binary data
1617
* @method static string hash(...$value) Generate a unique non-crypto hash and return a hexadecimal string
17-
* @method static string randomText(int $length, string $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') Generate a cryptographically secure string
18+
* @method static string randomText(int $length, string $chars = Char::ALPHANUMERIC) Generate a cryptographically secure string
1819
* @method static float textDistance(string $string1, string $string2, bool $normalise = true) Returns the Levenshtein distance between two strings relative to the length of the longest string (see {@see Computations::textDistance()})
1920
* @method static float textSimilarity(string $string1, string $string2, bool $normalise = true) Returns the similarity of two strings relative to the length of the longest string (see {@see Computations::textSimilarity()})
2021
* @method static string uuid(bool $binary = false) Generate a cryptographically secure random UUID (see {@see Computations::uuid()})
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Lkrms\Support\Catalog;
4+
5+
use Lkrms\Concept\Dictionary;
6+
7+
/**
8+
* Character sequences
9+
*
10+
* @extends Dictionary<string>
11+
*/
12+
final class CharacterSequence extends Dictionary
13+
{
14+
public const ALPHABETIC = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
15+
16+
public const ALPHABETIC_LOWER = 'abcdefghijklmnopqrstuvwxyz';
17+
18+
public const ALPHABETIC_UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
19+
20+
public const NUMERIC = '0123456789';
21+
22+
public const ALPHANUMERIC = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
23+
}

src/Utility/Computations.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Lkrms\Utility;
44

5+
use Lkrms\Support\Catalog\CharacterSequence as Char;
56
use Lkrms\Utility\Convert;
67

78
/**
@@ -41,7 +42,7 @@ public function uuid(bool $binary = false): string
4142
* Generate a cryptographically secure string
4243
*
4344
*/
44-
public function randomText(int $length, string $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'): string
45+
public function randomText(int $length, string $chars = Char::ALPHANUMERIC): string
4546
{
4647
$max = strlen($chars) - 1;
4748
$text = '';
File renamed without changes.

0 commit comments

Comments
 (0)