diff options
author | Håvard Pettersen <havardpe@oath.com> | 2022-03-31 11:44:10 +0000 |
---|---|---|
committer | Håvard Pettersen <havardpe@oath.com> | 2022-04-01 10:24:32 +0000 |
commit | d71f76a73fe54558f613e75a3a8cfd70fc46a7e8 (patch) | |
tree | 7d170e5b792dbf5fed06ff7c7b2db398b6cb64c7 /searchlib/src | |
parent | 1bb3dd192f4bcb91e710fe19ca54b2b8935ffb83 (diff) |
use getopt/getopt_long directly
Diffstat (limited to 'searchlib/src')
9 files changed, 104 insertions, 121 deletions
diff --git a/searchlib/src/apps/vespa-attribute-inspect/vespa-attribute-inspect.cpp b/searchlib/src/apps/vespa-attribute-inspect/vespa-attribute-inspect.cpp index 2d722a53adf..5cf7528ff1c 100644 --- a/searchlib/src/apps/vespa-attribute-inspect/vespa-attribute-inspect.cpp +++ b/searchlib/src/apps/vespa-attribute-inspect/vespa-attribute-inspect.cpp @@ -9,6 +9,7 @@ #include <vespa/fastlib/io/bufferedfile.h> #include <vespa/fastos/app.h> +#include <unistd.h> namespace search { @@ -112,11 +113,9 @@ LoadAttribute::Main() bool doFastSearch = false; bool doHuge = false; - int idx = 1; int opt; - const char * arg; bool optError = false; - while ((opt = GetOpt("pasf:h", arg, idx)) != -1) { + while ((opt = getopt(_argc, _argv, "pasf:h")) != -1) { switch (opt) { case 'p': doPrintContent = true; @@ -128,11 +127,11 @@ LoadAttribute::Main() doHuge = true; break; case 'f': - if (strcmp(arg, "search") == 0) { + if (strcmp(optarg, "search") == 0) { doFastSearch = true; } else { std::cerr << "Expected 'search' or 'aggregate', got '" << - arg << "'" << std::endl; + optarg << "'" << std::endl; optError = true; } break; @@ -145,12 +144,12 @@ LoadAttribute::Main() } } - if (_argc != (idx + 1) || optError) { + if (_argc != (optind + 1) || optError) { usage(); return -1; } - vespalib::string fileName(_argv[idx]); + vespalib::string fileName(_argv[optind]); vespalib::FileHeader fh; { vespalib::string datFileName(fileName + ".dat"); diff --git a/searchlib/src/apps/vespa-fileheader-inspect/vespa-fileheader-inspect.cpp b/searchlib/src/apps/vespa-fileheader-inspect/vespa-fileheader-inspect.cpp index bdc841ab235..349064eb2a8 100644 --- a/searchlib/src/apps/vespa-fileheader-inspect/vespa-fileheader-inspect.cpp +++ b/searchlib/src/apps/vespa-fileheader-inspect/vespa-fileheader-inspect.cpp @@ -6,6 +6,7 @@ #include <iostream> #include <vespa/vespalib/data/fileheader.h> #include <vespa/vespalib/stllike/asciistream.h> +#include <unistd.h> #include <vespa/log/log.h> LOG_SETUP("vespa-fileheader-inspect"); @@ -58,15 +59,13 @@ int Application::parseOpts() { int c = '?'; - const char *optArg = NULL; - int optInd = 0; - while ((c = GetOpt("d:f:qh", optArg, optInd)) != -1) { + while ((c = getopt(_argc, _argv, "d:f:qh")) != -1) { switch (c) { case 'd': - _delimiter = optArg[0]; + _delimiter = optarg[0]; break; case 'f': - _fileName = optArg; + _fileName = optarg; break; case 'q': _quiet = true; @@ -79,8 +78,8 @@ Application::parseOpts() return EXIT_FAILURE; } } - if (_argc == optInd + 1) { - _fileName = _argv[optInd]; + if (_argc == optind + 1) { + _fileName = _argv[optind]; } if (_fileName.empty()) { std::cerr << "No filename given." << std::endl; diff --git a/searchlib/src/apps/vespa-index-inspect/vespa-index-inspect.cpp b/searchlib/src/apps/vespa-index-inspect/vespa-index-inspect.cpp index 25e5a7bd8a3..79d205d7ad6 100644 --- a/searchlib/src/apps/vespa-index-inspect/vespa-index-inspect.cpp +++ b/searchlib/src/apps/vespa-index-inspect/vespa-index-inspect.cpp @@ -16,6 +16,7 @@ #include <iostream> #include <getopt.h> #include <cstdlib> +#include <unistd.h> #include <vespa/log/log.h> LOG_SETUP("vespa-index-inspect"); @@ -262,7 +263,6 @@ bool ShowPostingListSubApp::getOptions() { int c; - const char *optArgument = NULL; int longopt_index = 0; static struct option longopts[] = { { "indexdir", 1, NULL, 0 }, @@ -279,36 +279,33 @@ ShowPostingListSubApp::getOptions() LONGOPT_DOCIDLIMIT, LONGOPT_MINDOCID }; - int optIndex = 2; - _app.resetOptIndex(optIndex); - while ((c = _app.GetOptLong("di:mv", - optArgument, - optIndex, - longopts, - &longopt_index)) != -1) { + optind = 2; + while ((c = getopt_long(_app._argc, _app._argv, "di:mv", + longopts, + &longopt_index)) != -1) { switch (c) { case 0: switch (longopt_index) { case LONGOPT_INDEXDIR: - _indexDir = optArgument; + _indexDir = optarg; break; case LONGOPT_FIELD: - _fieldOptions.addField(optArgument); + _fieldOptions.addField(optarg); break; case LONGOPT_TRANSPOSE: _transpose = true; break; case LONGOPT_DOCIDLIMIT: - _docIdLimit = atoi(optArgument); + _docIdLimit = atoi(optarg); break; case LONGOPT_MINDOCID: - _minDocId = atoi(optArgument); + _minDocId = atoi(optarg); break; default: - if (optArgument != NULL) { + if (optarg != NULL) { LOG(error, "longopt %s with arg %s", - longopts[longopt_index].name, optArgument); + longopts[longopt_index].name, optarg); } else { LOG(error, "longopt %s", @@ -320,7 +317,7 @@ ShowPostingListSubApp::getOptions() _directio = true; break; case 'i': - _indexDir = optArgument; + _indexDir = optarg; break; case 'm': _readmmap = true; @@ -339,13 +336,13 @@ ShowPostingListSubApp::getOptions() if (_fieldOptions._fields.size() > 1) return false; } - _optIndex = optIndex; + _optIndex = optind; if (_transpose) { } else { if (_optIndex >= _app._argc) { return false; } - _word = _app._argv[optIndex]; + _word = _app._argv[optind]; } return true; } @@ -719,7 +716,6 @@ bool DumpWordsSubApp::getOptions() { int c; - const char *optArgument = NULL; int longopt_index = 0; static struct option longopts[] = { { "indexdir", 1, NULL, 0 }, @@ -736,24 +732,21 @@ DumpWordsSubApp::getOptions() LONGOPT_VERBOSE, LONGOPT_WORDNUM }; - int optIndex = 2; - _app.resetOptIndex(optIndex); - while ((c = _app.GetOptLong("i:", - optArgument, - optIndex, - longopts, - &longopt_index)) != -1) { + optind = 2; + while ((c = getopt_long(_app._argc, _app._argv, "i:", + longopts, + &longopt_index)) != -1) { switch (c) { case 0: switch (longopt_index) { case LONGOPT_INDEXDIR: - _indexDir = optArgument; + _indexDir = optarg; break; case LONGOPT_FIELD: - _fieldOptions.addField(optArgument); + _fieldOptions.addField(optarg); break; case LONGOPT_MINNUMDOCS: - _minNumDocs = atol(optArgument); + _minNumDocs = atol(optarg); break; case LONGOPT_VERBOSE: _verbose = true; @@ -762,10 +755,10 @@ DumpWordsSubApp::getOptions() _showWordNum = true; break; default: - if (optArgument != NULL) { + if (optarg != NULL) { LOG(error, "longopt %s with arg %s", - longopts[longopt_index].name, optArgument); + longopts[longopt_index].name, optarg); } else { LOG(error, "longopt %s", @@ -774,7 +767,7 @@ DumpWordsSubApp::getOptions() } break; case 'i': - _indexDir = optArgument; + _indexDir = optarg; break; default: return false; diff --git a/searchlib/src/tests/attribute/benchmark/attributebenchmark.cpp b/searchlib/src/tests/attribute/benchmark/attributebenchmark.cpp index 7d168407328..4a690a28cc8 100644 --- a/searchlib/src/tests/attribute/benchmark/attributebenchmark.cpp +++ b/searchlib/src/tests/attribute/benchmark/attributebenchmark.cpp @@ -11,6 +11,7 @@ #include "attributesearcher.h" #include "attributeupdater.h" #include <sys/resource.h> +#include <unistd.h> #include <vespa/log/log.h> LOG_SETUP("attributebenchmark"); @@ -485,62 +486,60 @@ AttributeBenchmark::Main() dc._prefixLength = 2; dc._prefixSearch = false; - int idx = 1; int opt; - const char * arg; bool optError = false; - while ((opt = GetOpt("n:u:v:s:q:p:r:c:l:h:i:a:e:S:E:D:L:bRPtw", arg, idx)) != -1) { + while ((opt = getopt(_argc, _argv, "n:u:v:s:q:p:r:c:l:h:i:a:e:S:E:D:L:bRPtw")) != -1) { switch (opt) { case 'n': - dc._numDocs = atoi(arg); + dc._numDocs = atoi(optarg); break; case 'u': - dc._numUpdates = atoi(arg); + dc._numUpdates = atoi(optarg); break; case 'v': - dc._numValues = atoi(arg); + dc._numValues = atoi(optarg); break; case 's': - dc._numSearchers = atoi(arg); + dc._numSearchers = atoi(optarg); break; case 'q': - dc._numQueries = atoi(arg); + dc._numQueries = atoi(optarg); break; case 'p': - dc._populateRuns = atoi(arg); + dc._populateRuns = atoi(optarg); break; case 'r': - dc._updateRuns = atoi(arg); + dc._updateRuns = atoi(optarg); break; case 'c': - dc._commitFreq = atoi(arg); + dc._commitFreq = atoi(optarg); break; case 'l': - dc._minValueCount = atoi(arg); + dc._minValueCount = atoi(optarg); break; case 'h': - dc._maxValueCount = atoi(arg); + dc._maxValueCount = atoi(optarg); break; case 'i': - dc._minStringLen = atoi(arg); + dc._minStringLen = atoi(optarg); break; case 'a': - dc._maxStringLen = atoi(arg); + dc._maxStringLen = atoi(optarg); break; case 'e': - dc._seed = atoi(arg); + dc._seed = atoi(optarg); break; case 'S': - dc._rangeStart = strtoll(arg, NULL, 10); + dc._rangeStart = strtoll(optarg, NULL, 10); break; case 'E': - dc._rangeEnd = strtoll(arg, NULL, 10); + dc._rangeEnd = strtoll(optarg, NULL, 10); break; case 'D': - dc._rangeDelta = strtoll(arg, NULL, 10); + dc._rangeDelta = strtoll(optarg, NULL, 10); break; case 'L': - dc._prefixLength = atoi(arg); + dc._prefixLength = atoi(optarg); break; case 'b': dc._searchersOnly = false; @@ -563,12 +562,12 @@ AttributeBenchmark::Main() } } - if (_argc != (idx + 1) || optError) { + if (_argc != (optind + 1) || optError) { usage(); return -1; } - dc._attribute = vespalib::string(_argv[idx]); + dc._attribute = vespalib::string(_argv[optind]); _threadPool = new FastOS_ThreadPool(256000); diff --git a/searchlib/src/tests/bitvector/bitvectorbenchmark.cpp b/searchlib/src/tests/bitvector/bitvectorbenchmark.cpp index 52888d0c659..e140814a5ad 100644 --- a/searchlib/src/tests/bitvector/bitvectorbenchmark.cpp +++ b/searchlib/src/tests/bitvector/bitvectorbenchmark.cpp @@ -6,6 +6,7 @@ #include <string> #include <vector> #include <cassert> +#include <unistd.h> LOG_SETUP("bitvectorbenchmark"); @@ -165,19 +166,17 @@ void BitVectorBenchmark::testOrSpeed2() int BitVectorBenchmark::Main() { - int idx = 1; std::string operation; size_t numBits(8*1000000); int opt; - const char * arg; bool optError = false; - while ((opt = GetOpt("n:t:", arg, idx)) != -1) { + while ((opt = getopt(_argc, _argv, "n:t:")) != -1) { switch (opt) { case 'n': - numBits = strtoll(arg, NULL, 10); + numBits = strtoll(optarg, NULL, 10); break; case 't': - operation = arg; + operation = optarg; break; default: optError = true; @@ -185,7 +184,7 @@ int BitVectorBenchmark::Main() } } - if ((_argc != idx ) || optError) { + if ((_argc != optind ) || optError) { usage(); return -1; } diff --git a/searchlib/src/tests/diskindex/fieldwriter/fieldwriter_test.cpp b/searchlib/src/tests/diskindex/fieldwriter/fieldwriter_test.cpp index 35b42223cfc..aa5f9a13e7d 100644 --- a/searchlib/src/tests/diskindex/fieldwriter/fieldwriter_test.cpp +++ b/searchlib/src/tests/diskindex/fieldwriter/fieldwriter_test.cpp @@ -21,6 +21,7 @@ #include <openssl/evp.h> #include <vespa/fastos/file.h> #include <vespa/fastos/app.h> +#include <unistd.h> #include <vespa/log/log.h> LOG_SETUP("fieldwriter_test"); @@ -665,30 +666,27 @@ testFieldWriterVariantsWithHighLids(FakeWordSet &wordSet, uint32_t docIdLimit, int FieldWriterTest::Main() { - int argi; int c; - const char *optArg; if (_argc > 0) { DummyFileHeaderContext::setCreator(_argv[0]); } - argi = 1; - while ((c = GetOpt("c:d:vw:", optArg, argi)) != -1) { + while ((c = getopt(_argc, _argv, "c:d:vw:")) != -1) { switch(c) { case 'c': - _commonDocFreq = atoi(optArg); + _commonDocFreq = atoi(optarg); if (_commonDocFreq == 0) _commonDocFreq = 1; break; case 'd': - _numDocs = atoi(optArg); + _numDocs = atoi(optarg); break; case 'v': _verbose = true; break; case 'w': - _numWordsPerClass = atoi(optArg); + _numWordsPerClass = atoi(optarg); break; default: Usage(); diff --git a/searchlib/src/tests/features/featurebenchmark.cpp b/searchlib/src/tests/features/featurebenchmark.cpp index f4fc830335d..5a70f1752fc 100644 --- a/searchlib/src/tests/features/featurebenchmark.cpp +++ b/searchlib/src/tests/features/featurebenchmark.cpp @@ -17,6 +17,7 @@ #include <iomanip> #include <iostream> #include <string> +#include <unistd.h> using namespace search::features; using namespace search::fef; @@ -596,19 +597,17 @@ Benchmark::Main() setup_fef_test_plugin(_factory); setup_search_features(_factory); - int idx = 1; int opt; - const char * arg; bool optError = false; vespalib::string file; vespalib::string feature; - while ((opt = GetOpt("c:f:", arg, idx)) != -1) { + while ((opt = getopt(_argc, _argv, "c:f:")) != -1) { switch (opt) { case 'c': - file.assign(arg); + file.assign(optarg); break; case 'f': - feature.assign(arg); + feature.assign(optarg); break; default: optError = true; @@ -616,7 +615,7 @@ Benchmark::Main() } } - if (_argc != idx || optError) { + if (_argc != optind || optError) { //usage(); return -1; } diff --git a/searchlib/src/tests/postinglistbm/postinglistbm.cpp b/searchlib/src/tests/postinglistbm/postinglistbm.cpp index 198ff57435a..d0552958611 100644 --- a/searchlib/src/tests/postinglistbm/postinglistbm.cpp +++ b/searchlib/src/tests/postinglistbm/postinglistbm.cpp @@ -12,6 +12,7 @@ #include <vespa/searchlib/test/fakedata/fpfactory.h> #include <vespa/vespalib/util/rand48.h> #include <vespa/vespalib/util/size_literals.h> +#include <unistd.h> #include <vespa/log/log.h> @@ -109,52 +110,49 @@ PostingListBM::~PostingListBM() = default; int PostingListBM::Main() { - int argi; int c; - const char *optArg; - argi = 1; bool hasElements = false; bool hasElementWeights = false; - while ((c = GetOpt("C:c:m:r:d:l:s:t:o:uw:T:q", optArg, argi)) != -1) { + while ((c = getopt(_argc, _argv, "C:c:m:r:d:l:s:t:o:uw:T:q")) != -1) { switch(c) { case 'C': - _skipCommonPairsRate = atoi(optArg); + _skipCommonPairsRate = atoi(optarg); break; case 'T': - if (strcmp(optArg, "single") == 0) { + if (strcmp(optarg, "single") == 0) { hasElements = false; hasElementWeights = false; - } else if (strcmp(optArg, "array") == 0) { + } else if (strcmp(optarg, "array") == 0) { hasElements = true; hasElementWeights = false; - } else if (strcmp(optArg, "weightedSet") == 0) { + } else if (strcmp(optarg, "weightedSet") == 0) { hasElements = true; hasElementWeights = true; } else { - printf("Bad collection type: '%s'\n", optArg); + printf("Bad collection type: '%s'\n", optarg); printf("Supported types: single, array, weightedSet\n"); return 1; } break; case 'c': - _commonDocFreq = atoi(optArg); + _commonDocFreq = atoi(optarg); break; case 'm': - _mediumDocFreq = atoi(optArg); + _mediumDocFreq = atoi(optarg); break; case 'r': - _rareDocFreq = atoi(optArg); + _rareDocFreq = atoi(optarg); break; case 'd': - _numDocs = atoi(optArg); + _numDocs = atoi(optarg); break; case 'l': - _loops = atoi(optArg); + _loops = atoi(optarg); break; case 's': - _stride = atoi(optArg); + _stride = atoi(optarg); break; case 't': do { @@ -163,17 +161,17 @@ PostingListBM::Main() DataType::STRING, CollectionType::SINGLE); schema.addIndexField(indexField); - std::unique_ptr<FPFactory> ff(getFPFactory(optArg, schema)); + std::unique_ptr<FPFactory> ff(getFPFactory(optarg, schema)); if (ff.get() == nullptr) { - badPostingType(optArg); + badPostingType(optarg); return 1; } } while (0); - _postingTypes.push_back(optArg); + _postingTypes.push_back(optarg); break; case 'o': { - vespalib::string operatorType(optArg); + vespalib::string operatorType(optarg); if (operatorType == "direct") { _operatorType = StressRunner::OperatorType::Direct; } else if (operatorType == "and") { @@ -191,7 +189,7 @@ PostingListBM::Main() _unpack = true; break; case 'w': - _numWordsPerClass = atoi(optArg); + _numWordsPerClass = atoi(optarg); break; default: usage(); diff --git a/searchlib/src/tests/transactionlogstress/translogstress.cpp b/searchlib/src/tests/transactionlogstress/translogstress.cpp index de0e1b60ca0..cfd2138f2e8 100644 --- a/searchlib/src/tests/transactionlogstress/translogstress.cpp +++ b/searchlib/src/tests/transactionlogstress/translogstress.cpp @@ -12,6 +12,7 @@ #include <iostream> #include <sstream> #include <thread> +#include <unistd.h> #include <vespa/log/log.h> #include <vespa/vespalib/util/time.h> @@ -642,44 +643,42 @@ TransLogStress::Main() vespalib::duration sleepTime = 4s; - int idx = 1; int opt; - const char * arg; bool optError = false; - while ((opt = GetOpt("d:p:t:f:s:v:c:e:g:i:a:b:h", arg, idx)) != -1) { + while ((opt = getopt(_argc, _argv, "d:p:t:f:s:v:c:e:g:i:a:b:h")) != -1) { switch (opt) { case 'd': - _cfg.domainPartSize = atol(arg); + _cfg.domainPartSize = atol(optarg); break; case 'p': - _cfg.packetSize = atol(arg); + _cfg.packetSize = atol(optarg); break; case 't': - _cfg.stressTime = std::chrono::milliseconds(1000 * atol(arg)); + _cfg.stressTime = std::chrono::milliseconds(1000 * atol(optarg)); break; case 'f': - _cfg.feedRate = atoi(arg); + _cfg.feedRate = atoi(optarg); break; case 'v': - _cfg.numVisitors = atoi(arg); + _cfg.numVisitors = atoi(optarg); break; case 'c': - _cfg.visitorInterval = std::chrono::milliseconds(atol(arg)); + _cfg.visitorInterval = std::chrono::milliseconds(atol(optarg)); break; case 'e': - _cfg.pruneInterval = vespalib::from_s(atol(arg)); + _cfg.pruneInterval = vespalib::from_s(atol(optarg)); break; case 'g': - _cfg.numPreGeneratedBuffers = atoi(arg); + _cfg.numPreGeneratedBuffers = atoi(optarg); break; case 'i': - _cfg.minStrLen = atoi(arg); + _cfg.minStrLen = atoi(optarg); break; case 'a': - _cfg.maxStrLen = atoi(arg); + _cfg.maxStrLen = atoi(optarg); break; case 'b': - _cfg.baseSeed = atol(arg); + _cfg.baseSeed = atol(optarg); break; case 'h': usage(); @@ -693,7 +692,7 @@ TransLogStress::Main() printConfig(); std::this_thread::sleep_for(sleepTime); - if (_argc != idx || optError) { + if (_argc != optind || optError) { usage(); return -1; } |