From 1f565c2efcc5b52f6579c1b63c271ce600d354a6 Mon Sep 17 00:00:00 2001 From: Gustavo Arnosti Neves Date: Thu, 26 Jul 2018 06:41:11 -0300 Subject: [PATCH] v0.2.1 - usage function, better erron handling; a bit more option parsing; fix run with empty JSON options; added more excluded files; changed excluded parsing; a bit more error messages; added PHP hashbang and made file executable --- dir2json.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/dir2json.php b/dir2json.php index 7ce7562..7e7fb5c 100755 --- a/dir2json.php +++ b/dir2json.php @@ -13,7 +13,7 @@ // > php dir2json.php --help // ------------------------------------------------------ -$DIR2JSON = '0.2.0'; +$DIR2JSON = '0.2.1'; function dir2json($dir) { @@ -46,6 +46,12 @@ function dir2json($dir) return $dirList; } +function usage($str='', $ret=2) { + if (!empty($str)) echo $str."\n"; + echo "For help, try:\n ./dir2json.php -h\n"; + exit($ret); +} + // Long and short help opts $param = isset($argv[1]) ? $argv[1] : ''; if ($param === "-h" || $param === "--help") @@ -93,21 +99,19 @@ $jsonOptions = empty($jsonOptions) ? 0 : constant($jsonOptions); // If we have a folder to read if (!is_dir($targetFolder)) { - echo "Cannot open folder $targetFolder\n"; - exit(2); + if (empty($targetFolder)) $targetFolder = '(empty)'; + usage("Cannot open folder $targetFolder", 2); } // If we have an output file name if (empty($outputFile)) { - echo "Need a valid output file name (empty)\n"; - exit(3); + usage("Need a valid output file name (empty)", 3); } $arr = dir2json($targetFolder); $json = json_encode($arr, $jsonOptions); if (!file_put_contents($outputFile, $json)) { - echo "Could not save output file: $outputFile\n"; - exit(4); + usage("Could not save output file: $outputFile", 4); } exit(0);