Skip to content

Commit d10563b

Browse files
committed
feat: add --help global option and fix pdodb dump behavior
- Add --help to global options list in Application::showHelp() - Add handling for global --help option in Application::run() - Fix pdodb dump to show help when no arguments provided (consistent with other commands) - Allow pdodb dump to dump entire database when dump-specific options are provided (--schema-only, --data-only, --output) - Add tests for new dump command behavior: - testDumpCommandShowsHelpWhenNoArguments() - testDumpCommandDumpsDatabaseWhenOutputOptionProvided()
1 parent 6a6314c commit d10563b

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

src/cli/Application.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ public function run(array $argv): int
117117
return 0;
118118
}
119119

120+
// Check for global --help option
121+
if ($argv[0] === '--help') {
122+
$this->showHelp();
123+
return 0;
124+
}
125+
120126
$commandName = $argv[0];
121127
$command = $this->getCommand($commandName);
122128

@@ -185,6 +191,7 @@ protected function showHelp(): void
185191
}
186192

187193
echo "\nGlobal options:\n";
194+
echo " --help Show help message\n";
188195
echo " --connection=<name> Use a named connection from config/db.php\n";
189196
echo " --config=<path> Path to db.php configuration file\n";
190197
echo " --env=<path> Path to .env file\n";

src/cli/commands/DumpCommand.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ public function __construct()
2828
*/
2929
public function execute(): int
3030
{
31-
// Show help only if explicitly requested via option or argument
31+
// Show help if explicitly requested via option or argument
3232
if ($this->getOption('help', false)) {
3333
$this->showHelp();
3434
return 0;
3535
}
3636

3737
$firstArg = $this->getArgument(0);
3838

39-
if ($firstArg === 'help') {
39+
if ($firstArg === '--help' || $firstArg === 'help') {
4040
$this->showHelp();
4141
return 0;
4242
}
@@ -45,7 +45,19 @@ public function execute(): int
4545
return $this->restore();
4646
}
4747

48-
// Default: dump (first arg is table name if provided, null = entire database)
48+
// If no arguments, check if dump-specific options are provided
49+
// If yes, dump entire database; if no, show help
50+
if ($firstArg === null) {
51+
$hasDumpOptions = $this->getOption('schema-only', false) !== false
52+
|| $this->getOption('data-only', false) !== false
53+
|| $this->getOption('output') !== null;
54+
if (!$hasDumpOptions) {
55+
$this->showHelp();
56+
return 0;
57+
}
58+
}
59+
60+
// First arg is table name (dump specific table), or null for entire database
4961
return $this->dump();
5062
}
5163

tests/shared/DumpCommandCliTests.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,38 @@ public function testDumpCommandHelp(): void
143143
$this->assertStringContainsString('Database Dump and Restore', $output);
144144
$this->assertStringContainsString('Usage:', $output);
145145
}
146+
147+
public function testDumpCommandShowsHelpWhenNoArguments(): void
148+
{
149+
$app = new Application();
150+
ob_start();
151+
$exitCode = $app->run(['pdodb', 'dump']);
152+
$output = ob_get_clean();
153+
154+
$this->assertEquals(0, $exitCode);
155+
$this->assertStringContainsString('Database Dump and Restore', $output);
156+
$this->assertStringContainsString('Usage:', $output);
157+
}
158+
159+
public function testDumpCommandDumpsDatabaseWhenOutputOptionProvided(): void
160+
{
161+
$dumpFile = sys_get_temp_dir() . '/pdodb_cli_test_' . uniqid() . '.sql';
162+
$app = new Application();
163+
putenv('PDODB_DRIVER=sqlite');
164+
putenv('PDODB_PATH=' . self::$dbPath);
165+
putenv('PDODB_NON_INTERACTIVE=1');
166+
167+
ob_start();
168+
$exitCode = $app->run(['pdodb', 'dump', '--output=' . $dumpFile]);
169+
$output = ob_get_clean();
170+
171+
$this->assertEquals(0, $exitCode);
172+
$this->assertFileExists($dumpFile);
173+
$sql = file_get_contents($dumpFile);
174+
$this->assertStringContainsString('CREATE TABLE', $sql);
175+
$this->assertStringContainsString('users', $sql);
176+
$this->assertStringContainsString('posts', $sql);
177+
178+
unlink($dumpFile);
179+
}
146180
}

0 commit comments

Comments
 (0)