From 8219b6d93bd82ea065ae7c6e5589c580fea1b96f Mon Sep 17 00:00:00 2001 From: nkindlon <nek3d@virginia.edu> Date: Thu, 19 Sep 2013 18:21:14 -0400 Subject: [PATCH] Improved help and error msgs in Context --- src/intersectFile/intersectMain.cpp | 4 ++-- src/utils/Contexts/Context.cpp | 34 ++++++++++++++++++++++++++--- src/utils/Contexts/Context.h | 3 ++- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/intersectFile/intersectMain.cpp b/src/intersectFile/intersectMain.cpp index 9fcb8a1b..a220b24f 100644 --- a/src/intersectFile/intersectMain.cpp +++ b/src/intersectFile/intersectMain.cpp @@ -23,8 +23,8 @@ void intersect_help(void); int intersect_main(int argc, char* argv[]) { Context *context = new Context(); - context->parseCmdArgs(argc, argv, 1); - if (context->getShowHelp() || !context->isValidState()) { + + if (!context->parseCmdArgs(argc, argv, 1) || context->getShowHelp() || !context->isValidState()) { if (!context->getErrorMsg().empty()) { cerr << context->getErrorMsg() << endl; } diff --git a/src/utils/Contexts/Context.cpp b/src/utils/Contexts/Context.cpp index 97143c22..6b858065 100644 --- a/src/utils/Contexts/Context.cpp +++ b/src/utils/Contexts/Context.cpp @@ -44,6 +44,8 @@ Context::Context() _reportNames(false), _reportScores(false) { + _programNames["intersect"] = INTERSECT; + _validScoreOps.insert("sum"); _validScoreOps.insert("max"); _validScoreOps.insert("min"); @@ -90,15 +92,17 @@ void Context::openGenomeFile(const BamTools::RefVector &refVector) _genomeFile = new NewGenomeFile(refVector); } -void Context::parseCmdArgs(int argc, char **argv, int skipFirstArgs) { +bool Context::parseCmdArgs(int argc, char **argv, int skipFirstArgs) { _argc = argc; _argv = argv; _skipFirstArgs = skipFirstArgs; if (argc < 2) { setShowHelp(true); - return; + return false; } + setProgram(_programNames[argv[0]]); + _argsProcessed.resize(argc - skipFirstArgs, false); for (int i=skipFirstArgs; i < argc; i++) { @@ -112,6 +116,10 @@ void Context::parseCmdArgs(int argc, char **argv, int skipFirstArgs) { i++; markUsed(i - skipFirstArgs); } else if (strcmp(argv[i], "-g") == 0) { + if (argc <= i+1) { + _errorMsg = "Error: -g option given, but no genome file specified."; + return false; + } openGenomeFile(argv[i+1]); markUsed(i - skipFirstArgs); i++; @@ -128,6 +136,11 @@ void Context::parseCmdArgs(int argc, char **argv, int skipFirstArgs) { markUsed(i - skipFirstArgs); } if (strcmp(argv[i], "-a") == 0) { + if (argc <= i+1) { + _errorMsg = "Error: -a option given, but no query file specified."; + return false; + } + addInputFile(argv[i+1]); _queryFileIdx = getNumInputFiles() -1; markUsed(i - skipFirstArgs); @@ -135,6 +148,10 @@ void Context::parseCmdArgs(int argc, char **argv, int skipFirstArgs) { markUsed(i - skipFirstArgs); } else if(strcmp(argv[i], "-abam") == 0) { + if (argc <= i+1) { + _errorMsg = "Error: -abam option given, but no query BAM file specified."; + return false; + } addInputFile(argv[i+1]); _queryFileIdx = getNumInputFiles() -1; markUsed(i - skipFirstArgs); @@ -143,6 +160,10 @@ void Context::parseCmdArgs(int argc, char **argv, int skipFirstArgs) { setInputFileType(_queryFileIdx, FileRecordTypeChecker::BAM_FILE_TYPE); } else if (strcmp(argv[i], "-b") == 0) { + if (argc <= i+1) { + _errorMsg = "Error: -b option given, but no database file specified."; + return false; + } addInputFile(argv[i+1]); _databaseFileIdx = getNumInputFiles() -1; markUsed(i - skipFirstArgs); @@ -218,13 +239,20 @@ void Context::parseCmdArgs(int argc, char **argv, int skipFirstArgs) { markUsed(i - skipFirstArgs); } } + return true; } bool Context::isValidState() { - if (!Context::cmdArgsValid()) { + if (!cmdArgsValid()) { return false; } + + if (getProgram() == INTERSECT && (_queryFileIdx == -1 || _databaseFileIdx == -1)) { + _errorMsg = "Error: Intersect program was not given a query and database file."; + return false; + } + if (getAnyHit() && getNoHit()) { _errorMsg = "Error: request either -u for anyHit OR -v for noHit, not both."; return false; diff --git a/src/utils/Contexts/Context.h b/src/utils/Contexts/Context.h index f65528d0..5638e087 100644 --- a/src/utils/Contexts/Context.h +++ b/src/utils/Contexts/Context.h @@ -74,7 +74,7 @@ public: void setOutputFileType(ContextFileType fileType) { _outputFileType = fileType; } ContextFileType getOutputFileType() const { return _outputFileType; } - void parseCmdArgs(int argc, char **argv, int skipFirstArgs); + bool parseCmdArgs(int argc, char **argv, int skipFirstArgs); //isValidState checks that parameters to context are in an acceptable state. // If not, the error msg string will be set with a reason why it failed. @@ -193,6 +193,7 @@ protected: ContextRecordType _recordType; }; vector<FileEntryType> _inputFiles; + map<QuickString, PROGRAM_TYPE> _programNames; bool _useMergedIntervals; NewGenomeFile *_genomeFile; -- GitLab