Skip to content

Commit

Permalink
Use GETOPT for the command line scripts #326
Browse files Browse the repository at this point in the history
  • Loading branch information
pkiraly committed Nov 1, 2023
1 parent 6812aed commit 3a6e67f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 38 deletions.
30 changes: 30 additions & 0 deletions scripts/cli-generator/common.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
options.addOption("m", "marcVersion", true, "MARC version ('OCLC' or 'DNB')");
options.addOption("h", "help", false, "display help");
options.addOption("n", "nolog", false, "do not display log messages");
options.addOption("l", "limit", true, "limit the number of records to process");
options.addOption("o", "offset", true, "the first record to process");
options.addOption("i", "id", true, "the MARC identifier (content of 001)");
options.addOption("d", "defaultRecordType", true, "the default record type if the record's type is undetectable");
options.addOption("q", "fixAlephseq", false, "fix the known issues of Alephseq format");
options.addOption("a", "fixAlma", false, "fix the known issues of Alma format");
options.addOption("b", "fixKbr", false, "fix the known issues of Alma format");
options.addOption("p", "alephseq", false, "the source is in Alephseq format");
options.addOption("x", "marcxml", false, "the source is in MARCXML format");
options.addOption("y", "lineSeparated", false, "the source is in line separated MARC format");
options.addOption("t", "outputDir", true, "output directory");
options.addOption("r", "trimId", false, "remove spaces from the end of record IDs");
options.addOption("z", "ignorableFields", true, "ignore fields from the analysis");
options.addOption("v", "ignorableRecords", true, "ignore records from the analysis");
options.addOption("f", "marcFormat", true, "MARC format (like 'ISO' or 'MARCXML')");
options.addOption("s", "dataSource", true, "data source (file of stream)");
options.addOption("g", "defaultEncoding", true, "default character encoding");
options.addOption("1", "alephseqLineType", true, "Alephseq line type");
options.addOption("2", "picaIdField", true, "PICA id field");
options.addOption("u", "picaSubfieldSeparator", true, "PICA subfield separator");
options.addOption("j", "picaSchemaFile", true, "Avram PICA schema file");
options.addOption("w", "schemaType", true, "metadata schema type ('MARC21', 'UNIMARC', or 'PICA')");
options.addOption("k", "picaRecordType", true, "picaRecordType");
options.addOption("c", "allowableRecords", true, "allow records for the analysis");
options.addOption("e", "groupBy", true, "group the results by the value of this data element (e.g. the ILN of library)");
options.addOption("3", "groupListFile", true, "the file which contains a list of ILN codes");
options.addOption("4", "solrForScoresUrl", true, "the URL of the Solr server used to store scores");
16 changes: 8 additions & 8 deletions scripts/cli-generator/generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
}

$maxLong = 0;
$options = readOptions($fileName);
$index = (object)['longs' => [], 'shorts' => []];
$options = readOptions('common.txt', $index);
$options = readOptions($fileName, $index);

createHelp($options);
echo LN;
Expand Down Expand Up @@ -157,11 +159,10 @@ function createGetopt($options) {
END;
}

function readOptions($fileName) {
function readOptions($fileName, $index) {
global $maxLong;
$data = [];
$handle = fopen($fileName, "r");
$shorts = $longs = [];
if ($handle) {
while (($line = fgets($handle)) !== false) {
if (preg_match('/\s*options.addOption\("(.)", "([^"]+)", (true|false), "([^"]+)"\);$/', $line, $matches)) { //
Expand All @@ -174,15 +175,15 @@ function readOptions($fileName) {
if ($maxLong < strlen($matches[2]))
$maxLong = strlen($matches[2]);

if (in_array($matches[1], $shorts)) {
if (in_array($matches[1], $index->shorts)) {
error_log('repeated short: ' . $matches[1] . ' -- ' . $line);
}
$shorts[] = $matches[1];
$index->shorts[] = $matches[1];

if (in_array($matches[2], $longs)) {
if (in_array($matches[2], $index->longs)) {
error_log('repeated long: ' . $matches[2] . ' -- ' . $line);
}
$longs[] = $matches[2];
$index->longs[] = $matches[2];

} else {
error_log('line does not fit to the pattern: ', $line);
Expand All @@ -192,4 +193,3 @@ function readOptions($fileName) {
}
return $data;
}

30 changes: 0 additions & 30 deletions scripts/cli-generator/validate.txt
Original file line number Diff line number Diff line change
@@ -1,33 +1,3 @@
options.addOption("m", "marcVersion", true, "MARC version ('OCLC' or 'DNB')");
options.addOption("h", "help", false, "display help");
options.addOption("n", "nolog", false, "do not display log messages");
options.addOption("l", "limit", true, "limit the number of records to process");
options.addOption("o", "offset", true, "the first record to process");
options.addOption("i", "id", true, "the MARC identifier (content of 001)");
options.addOption("d", "defaultRecordType", true, "the default record type if the record's type is undetectable");
options.addOption("q", "fixAlephseq", false, "fix the known issues of Alephseq format");
options.addOption("a", "fixAlma", false, "fix the known issues of Alma format");
options.addOption("b", "fixKbr", false, "fix the known issues of Alma format");
options.addOption("p", "alephseq", false, "the source is in Alephseq format");
options.addOption("x", "marcxml", false, "the source is in MARCXML format");
options.addOption("y", "lineSeparated", false, "the source is in line separated MARC format");
options.addOption("t", "outputDir", true, "output directory");
options.addOption("r", "trimId", false, "remove spaces from the end of record IDs");
options.addOption("z", "ignorableFields", true, "ignore fields from the analysis");
options.addOption("v", "ignorableRecords", true, "ignore records from the analysis");
options.addOption("f", "marcFormat", true, "MARC format (like 'ISO' or 'MARCXML')");
options.addOption("s", "dataSource", true, "data source (file of stream)");
options.addOption("g", "defaultEncoding", true, "default character encoding");
options.addOption("1", "alephseqLineType", true, "Alephseq line type");
options.addOption("2", "picaIdField", true, "PICA id field");
options.addOption("u", "picaSubfieldSeparator", true, "PICA subfield separator");
options.addOption("j", "picaSchemaFile", true, "Avram PICA schema file");
options.addOption("w", "schemaType", true, "metadata schema type ('MARC21', 'UNIMARC', or 'PICA')");
options.addOption("k", "picaRecordType", true, "picaRecordType");
options.addOption("c", "allowableRecords", true, "allow records for the analysis");
options.addOption("e", "groupBy", true, "group the results by the value of this data element (e.g. the ILN of library)");
options.addOption("3", "groupListFile", true, "the file which contains a list of ILN codes");
options.addOption("4", "solrForScoresUrl", true, "the URL of the Solr server used to store scores");
options.addOption("G", "summaryFileName", true, "the summary file name (provides a summary of issues, such as the number of instance and number of records having the particular issue)");
options.addOption("S", "summary", false, "show summary instead of record level display");
options.addOption("H", "details", false, "show record level display");
Expand Down

0 comments on commit 3a6e67f

Please sign in to comment.