Skip to content

Commit af32810

Browse files
committed
Merge branch 'cli'
2 parents 774eee4 + 0883a76 commit af32810

File tree

16 files changed

+462
-102
lines changed

16 files changed

+462
-102
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ jobs:
2727
with:
2828
php-version: "8.3"
2929
coverage: none
30-
tools: phive
3130

3231
- name: Get Composer cache directory
3332
id: get-composer-cache
@@ -42,9 +41,7 @@ jobs:
4241
restore-keys: composer-cache-${{ runner.os }}-
4342

4443
- name: Install PHP dependencies
45-
run: |
46-
composer install --no-interaction --no-progress
47-
phive --no-progress install --trust-gpg-keys E82B2FB314E9906E,E8CC5BC780B581F2
44+
run: composer install --no-interaction --no-progress
4845

4946
- name: Check generated files
5047
run: scripts/generate.php --check

.phive/phars.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phive xmlns="https://phar.io/phive">
3-
<phar name="lkrms/pretty-php" version="^0.4.48" installed="0.4.50" location="./tools/pretty-php" copy="false"/>
4-
<phar name="php-cs-fixer" version="^3.46.0" installed="3.48.0" location="./tools/php-cs-fixer" copy="false"/>
5-
<phar name="salient-labs/php-changelog" version="^1.0.0" installed="1.0.1" location="./tools/changelog" copy="false"/>
3+
<phar name="lkrms/pretty-php" version="^0.4.48" installed="0.4.51" location="./tools/pretty-php" copy="true"/>
4+
<phar name="php-cs-fixer" version="^3.46.0" installed="3.48.0" location="./tools/php-cs-fixer" copy="true"/>
5+
<phar name="salient-labs/php-changelog" version="^1.0.0" installed="1.0.1" location="./tools/changelog" copy="true"/>
66
</phive>

src/Cli/Catalog/CliOptionVisibility.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ final class CliOptionVisibility extends Enumeration
5353

5454
/**
5555
* Show the option everywhere
56+
*
57+
* This is the default.
5658
*/
5759
public const ALL =
5860
CliOptionVisibility::SYNOPSIS

src/Cli/CliApplication.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class CliApplication extends Application implements ICliApplication
4141

4242
private int $LastExitStatus = 0;
4343

44+
/**
45+
* @inheritDoc
46+
*/
4447
public function __construct(
4548
?string $basePath = null,
4649
?string $appName = null,

src/Cli/CliCommand.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,15 @@ final protected function applyOptionValues(
791791
$_values[$name] = $_value;
792792
$this->OptionValues[$option->Key] = $_value;
793793
if ($asArguments) {
794+
// If the option has an optional value and no value was given,
795+
// store null to ensure it's not expanded on export
796+
if (
797+
$option->ValueOptional &&
798+
$option->ValueType !== CliOptionValueType::BOOLEAN &&
799+
$value === true
800+
) {
801+
$value = null;
802+
}
794803
$this->ArgumentValues[$option->Key] = $value;
795804
}
796805
}
@@ -834,21 +843,29 @@ final protected function normaliseOptionValues(
834843
* returned.
835844
* @param bool $schema If `true`, an array that maps schema option names to
836845
* values is returned.
846+
* @param bool $unexpand If `true` and an option has an optional value not
847+
* given on the command line, replace its value with `null` or `true`.
837848
* @return array<string,mixed>
838849
*/
839850
final protected function getOptionValues(
840851
bool $export = false,
841-
bool $schema = false
852+
bool $schema = false,
853+
bool $unexpand = false
842854
): array {
843855
$this->assertHasRun()->loadOptions();
844856
$options = $schema ? $this->SchemaOptions : $this->Options;
845857
foreach ($options as $key => $option) {
846-
if ($export && !array_key_exists($option->Key, $this->ArgumentValues)) {
858+
$given = array_key_exists($option->Key, $this->ArgumentValues);
859+
if ($export && !$given) {
860+
continue;
861+
}
862+
if ($option->ValueOptional && !$option->Required && !$given) {
847863
continue;
848864
}
849865
$name = $schema ? $key : $option->Name;
850866
if (
851-
$export &&
867+
$unexpand &&
868+
$given &&
852869
$option->ValueOptional &&
853870
$this->ArgumentValues[$option->Key] === null
854871
) {

src/Cli/CliOption.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,8 @@ final class CliOption implements Buildable, HasJsonSchema, IImmutable, IReadable
320320
* @param array<string|int|bool>|string|int|bool|null $defaultValue
321321
* @param (callable(array<string|int|bool>|string|int|bool): mixed)|null $valueCallback
322322
* @param int-mask-of<CliOptionVisibility::*> $visibility
323+
* @param bool $inSchema True if the option should be included when
324+
* generating a JSON Schema.
323325
* @param bool $hide True if the option's visibility should be
324326
* {@see CliOptionVisibility::NONE}.
325327
* @param mixed $bindTo Bind the option's value to a variable.
@@ -344,6 +346,7 @@ public function __construct(
344346
?string $delimiter = ',',
345347
?callable $valueCallback = null,
346348
int $visibility = CliOptionVisibility::ALL,
349+
bool $inSchema = false,
347350
bool $hide = false,
348351
&$bindTo = null
349352
) {
@@ -377,6 +380,10 @@ public function __construct(
377380
$this->ValueCallback = $valueCallback;
378381
$this->Visibility = $hide ? CliOptionVisibility::NONE : $visibility;
379382

383+
if ($inSchema) {
384+
$this->Visibility |= CliOptionVisibility::SCHEMA;
385+
}
386+
380387
if (func_num_args() >= 20) {
381388
$this->BindTo = &$bindTo;
382389
$this->IsBound = true;

src/Cli/CliOptionBuilder.php

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Container/Application.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class Application extends Container implements IApplication
4343

4444
private string $BasePath;
4545

46+
private string $WorkingDirectory;
47+
4648
private bool $RunningFromSource;
4749

4850
// @phpstan-ignore-next-line
@@ -300,6 +302,8 @@ public function __construct(
300302

301303
$this->BasePath = $basePath;
302304

305+
$this->WorkingDirectory = File::cwd();
306+
303307
$this->RunningFromSource =
304308
!extension_loaded('Phar') ||
305309
Phar::running() === '';
@@ -348,6 +352,18 @@ final public function getBasePath(): string
348352
return $this->BasePath;
349353
}
350354

355+
/**
356+
* @inheritDoc
357+
*/
358+
public function restoreWorkingDirectory()
359+
{
360+
if (File::cwd() !== $this->WorkingDirectory) {
361+
chdir($this->WorkingDirectory);
362+
}
363+
364+
return $this;
365+
}
366+
351367
/**
352368
* @inheritDoc
353369
*/

src/Contract/IApplication.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ public function syncNamespace(
187187
?string $resolver = null
188188
);
189189

190+
/**
191+
* Change to the directory in which the application was started
192+
*
193+
* @return $this
194+
*/
195+
public function restoreWorkingDirectory();
196+
190197
/**
191198
* Print a summary of the application's runtime performance metrics and
192199
* system resource usage when it terminates

src/Facade/App.php

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)