From 2a391f4e0a4d65dabae3e3ae54e5b03d7e867cde Mon Sep 17 00:00:00 2001 From: Alejandro Sosa Date: Thu, 26 Mar 2026 12:27:12 +0100 Subject: [PATCH] Task: #53687 Simplify cfg argument handling and keep --siteDir with JSON write input (-i) --- bin/cfg | 32 +------------------------------ tests/CfgTest.php | 49 ++++++++++++++++++++++------------------------- 2 files changed, 24 insertions(+), 57 deletions(-) diff --git a/bin/cfg b/bin/cfg index a8cbca5..0fef170 100755 --- a/bin/cfg +++ b/bin/cfg @@ -56,36 +56,6 @@ function readJsonInputFile(string $path): array return $data; } -// Pre-parse siteDir so it can be passed after positional args. -// The input helper library does not handle this case reliably. -$preparsedSiteDir = null; -$rawArgv = $_SERVER['argv'] ?? $GLOBALS['argv'] ?? null; -if (is_array($rawArgv) && !empty($rawArgv)) -{ - $filteredArgv = [$rawArgv[0]]; - for ($idx = 1; $idx < count($rawArgv); $idx++) - { - $arg = $rawArgv[$idx]; - - if ($arg === '--siteDir') - { - $preparsedSiteDir = $rawArgv[$idx + 1] ?? ''; - if (isset($rawArgv[$idx + 1])) $idx++; - continue; - } - if (str_starts_with($arg, '--siteDir=')) - { - $preparsedSiteDir = substr($arg, strlen('--siteDir=')); - continue; - } - - $filteredArgv[] = $arg; - } - - $_SERVER['argv'] = $filteredArgv; - $GLOBALS['argv'] = $filteredArgv; -} - $version = '0.4'; $actions = [ 'show', 'write', 'help' ]; @@ -289,7 +259,7 @@ if ($pkgPath = $argv->find('pkgPath')) $cfg->pkgPath(rtrim($pkgPath, '/').'/'); $site = null; $siteFlag = 0x01; -$siteInput = ($preparsedSiteDir !== null) ? $preparsedSiteDir : $argv->find('siteDir'); +$siteInput = $argv->find('siteDir'); if ($siteInput !== null && $siteInput !== false) { $siteInput = trim((string)$siteInput); diff --git a/tests/CfgTest.php b/tests/CfgTest.php index 5aebc06..be9b3c6 100644 --- a/tests/CfgTest.php +++ b/tests/CfgTest.php @@ -151,14 +151,13 @@ class CfgTest extends TestCase public function testWriteWithSiteDirCreatesAndWritesSiteConfig(): void { $result = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'Extension', - 'module:code="X100"', - '--siteDir', - 'owner_xyz', - ]); + '-a', + $this->tmpDir, + 'write', + 'Extension', + 'module:code="X100"', + '--siteDir=owner_xyz', + ]); $this->assertSame(0, $result['code'], $result['output']); @@ -173,25 +172,23 @@ class CfgTest extends TestCase public function testWriteWithSiteDirMergesIntoExistingSiteConfig(): void { $firstWrite = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'Extension', - 'module:code="X100"', - '--siteDir', - 'owner_xyz', - ]); + '-a', + $this->tmpDir, + 'write', + 'Extension', + 'module:code="X100"', + '--siteDir=owner_xyz', + ]); $this->assertSame(0, $firstWrite['code'], $firstWrite['output']); $secondWrite = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'Extension', - 'module:label="demo-module"', - '--siteDir', - 'owner_xyz', - ]); + '-a', + $this->tmpDir, + 'write', + 'Extension', + 'module:label="demo-module"', + '--siteDir=owner_xyz', + ]); $this->assertSame(0, $secondWrite['code'], $secondWrite['output']); $siteFile = $this->tmpDir.'/config/owner_xyz/Extension.conf.php'; @@ -327,10 +324,10 @@ class CfgTest extends TestCase 'Extension', 'module:code="X100"', '--siteDir=', - ]); + ]); $this->assertSame(1, $result['code']); - $this->assertStringContainsString('Option --siteDir is empty.', $result['output']); + $this->assertStringContainsString('a value is required for --siteDir', $result['output']); } private function runCfg(array $args): array