Create seperate argv parser
This commit is contained in:
parent
61a4dcd370
commit
41bc3da4f0
50
includes/argvParser.php
Normal file
50
includes/argvParser.php
Normal file
@ -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;
|
||||
}
|
||||
}
|
40
packer.php
40
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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user