From 41bc3da4f0b309d97e5498235fdde9640ba5a161 Mon Sep 17 00:00:00 2001 From: Tim <git@loken.nl> Date: Wed, 19 Mar 2025 00:19:54 +0100 Subject: [PATCH] Create seperate argv parser --- includes/argvParser.php | 50 +++++++++++++++++++++++++++++++++++++++++ packer.php | 40 ++++++++++++++++++++++----------- 2 files changed, 77 insertions(+), 13 deletions(-) create mode 100644 includes/argvParser.php diff --git a/includes/argvParser.php b/includes/argvParser.php new file mode 100644 index 0000000..ca8c49b --- /dev/null +++ b/includes/argvParser.php @@ -0,0 +1,50 @@ +<?php + +readonly class argvParser +{ + /** + * @param array<string, string> $options + * @param array<string, string> $arguments + */ + public function __construct( + private array $options = [], + private array $arguments = [], + ) {} + + public function getOptionsHelp(): string + { + $line = ''; + foreach ($this->options as $option => $description) { + $line .= sprintf('[-%s (%s)] ', $option, $description); + } + foreach ($this->arguments as $argument => $description) { + $line .= sprintf('<%s (%s)> ', $argument, $description); + } + return $line; + } + + public function parseArgv(array $argv): false|array + { + array_shift($argv); // shift the script name + if (count($argv) === 0) { + return false; + } + + $parsed = array_map(fn() => false, $this->options); + while (str_starts_with($argv[0], '-')) { + $option = substr($argv[0], 1); + if ($this->options[$option]) { + $parsed[$option] = true; + } + array_shift($argv); + } + if (count($argv) !== count($this->arguments)) { + return false; + } + foreach ($this->arguments as $arg => $description) { + $parsed[$arg] = array_shift($argv); + } + + return $parsed; + } +} diff --git a/packer.php b/packer.php index d3e7ce0..1a97535 100755 --- a/packer.php +++ b/packer.php @@ -5,29 +5,43 @@ include 'includes/fileReader.php'; include 'includes/io.php'; include 'includes/includeBuilder.php'; include 'includes/includeParser.php'; +include 'includes/argvParser.php'; -if (count($argv) < 3) { - line('Usage: ' . __FILE__ . ' <inFile> <outFile>'); +// php://stdout +$argvParser = new ArgvParser( + ['d' => 'Delete outFile'], + ['inFile' => 'Input file', 'outFile' => 'Output file'] +); +$parsed = $argvParser->parseArgv($argv); + +if ($parsed === false) { + line('Usage: ' . __FILE__ . ' ' . $argvParser->getOptionsHelp()); exit(1); } -$fromFile = $argv[1]; -if (!file_exists($fromFile)) { - line('File not found: ' . $fromFile); +$inFile = $parsed['inFile']; +if (!file_exists($inFile)) { + line('File not found: ' . $inFile); exit(1); } -$toFile = $argv[2]; -if (file_exists($toFile)) { - line('File already exists: ' . $toFile); - unlink($toFile); +$outFile = $parsed['outFile']; +if (file_exists($outFile)) { + line('File already exists: ' . $outFile); + if ($parsed['d']) { + line('Deleting...'); + unlink($outFile); + } else { + exit(1); + } } $includes = []; -buildIncludes($fromFile, $includes); -$lines = yieldIncludes($fromFile, $includes); +buildIncludes($inFile, $includes); +var_dump($includes); +$lines = yieldIncludes($inFile, $includes); -$toFileHandle = fopen($toFile, 'w'); +$toFileHandle = fopen($outFile, 'w'); foreach ($lines as $line) { fwrite($toFileHandle, $line); - chmod($toFile, fileperms($fromFile)); } +chmod($outFile, fileperms($inFile)); fclose($toFileHandle);