c – 拆分长命令行测试方法
发布时间:2020-12-16 09:23:57 所属栏目:百科 来源:网络整理
导读:我有一个很大的(读取:噩梦)方法,这种方法多年来一直在增长,以支持我的项目不断增长的命令行参数列表.我的意思是几页自述文档,每个参数都有简短的模糊. 当我添加了每个功能时,我只是通过在该方法中添加几行来“注册”一种处理该参数的方法. 但是,这种方法现
我有一个很大的(读取:噩梦)方法,这种方法多年来一直在增长,以支持我的项目不断增长的命令行参数列表.我的意思是几页自述文档,每个参数都有简短的模糊.
当我添加了每个功能时,我只是通过在该方法中添加几行来“注册”一种处理该参数的方法. 但是,这种方法现在不雅观,容易出错,而且难以理解.以下是当前处理此方法的两种方法中较短的一个示例: //All double dash arguments modify global options of the program,//such as --all --debug --timeout etc. void consoleParser::wordArgParse(std::vector<criterion *> *results) { TCHAR const *compareCurWordArg = curToken.c_str()+2; if (!_tcsicmp(compareCurWordArg,_T("all"))) { globalOptions::showall = TRUE; } else if (!_tcsnicmp(compareCurWordArg,_T("custom"),6)) { if (curToken[9] == L':') { globalOptions::display = curToken.substr(10,curToken.length()-11); } else { globalOptions::display = curToken.substr(9,curToken.length()-10); } } else if (*compareCurWordArg == L'c' || *compareCurWordArg == L'C') { if (curToken[3] == L':') { globalOptions::display = curToken.substr(5,curToken.length()-6); } else { globalOptions::display = curToken.substr(4,curToken.length()-5); } } else if (!_tcsicmp(compareCurWordArg,_T("debug"))) { globalOptions::debug = TRUE; } else if (!_tcsicmp(compareCurWordArg,L"expand")) { globalOptions::expandRegex = false; } else if (!_tcsicmp(compareCurWordArg,L"fileLook")) { globalOptions::display = L"---- #f ----#nCompany: #d#nFile Description: [/!--empirenews.page--]nFile Version: #g" L"#nProduct Name: #i#nCopyright: #j#nOriginal file name: #k#nFile Size: #u#nCreated Time: #c" L"#nModified Time: #m#nAccessed Time: #a#nMD5: #5#nSHA1: #1"; } else if (!_tcsicmp(compareCurWordArg,_T("peinfo"))) { globalOptions::display = _T("[#p] #f"); } else if (!_tcsicmp(compareCurWordArg,L"enable-filesystem-redirector-64")) { globalOptions::disable64Redirector = false; } else if (!_tcsnicmp(compareCurWordArg,_T("encoding"),8)) { //Performance enhancement -- encoding compare only done once. compareCurWordArg += 8; if (!_tcsicmp(compareCurWordArg,_T("acp"))) { globalOptions::encoding = globalOptions::ENCODING_TYPE_ACP; } else if (!_tcsicmp(compareCurWordArg,_T("oem"))) { globalOptions::encoding = globalOptions::ENCODING_TYPE_OEM; } else if (!_tcsicmp(compareCurWordArg,_T("utf8"))) { globalOptions::encoding = globalOptions::ENCODING_TYPE_UTF8; } else if (!_tcsicmp(compareCurWordArg,_T("utf16"))) { globalOptions::encoding = globalOptions::ENCODING_TYPE_UTF16; } else { throw eMsg(L"Unrecognised encoding word argument!rnValid choices are --encodingACP --encodingOEM --encodingUTF8 and --encodingUTF16. Terminate."); } } else if (!_tcsnicmp(compareCurWordArg,L"files",5)) { compareCurWordArg += 5; if (*compareCurWordArg == L':') compareCurWordArg++; std::wstring filePath(compareCurWordArg); globalOptions::regexes.insert(globalOptions::regexes.end(),new filesRegexPlaceHolder); results->insert(results->end(),new filesRegexPlaceHolder); boost::algorithm::trim_if(filePath,std::bind2nd(std::equal_to<wchar_t>(),L'"')); loadFiles(filePath); } else if (!_tcsicmp(compareCurWordArg,_T("full"))) { globalOptions::fullPath = TRUE; } else if (!_tcsicmp(compareCurWordArg,_T("fs32"))) { globalOptions::disable64Redirector = false; } else if (!_tcsicmp(compareCurWordArg,_T("long"))) { globalOptions::display = _T("#t #s #m #f"); globalOptions::summary = TRUE; } else if (!_tcsnicmp(compareCurWordArg,_T("limit"),5)) { compareCurWordArg += 5; if (*compareCurWordArg == _T(':')) compareCurWordArg++; globalOptions::lineLimit = _tcstoui64(compareCurWordArg,NULL,10); if (!globalOptions::lineLimit) { std::wcerr << eMsg(L"Warning: You are limiting to infinity lines. Check one of your --limit options!rn"); } } else if (!_tcsicmp(compareCurWordArg,_T("short"))) { globalOptions::display = _T("#8"); } else if (!_tcsicmp(compareCurWordArg,_T("summary"))) { globalOptions::summary = TRUE; } else if (!_tcsicmp(compareCurWordArg,_T("norecursion"))) { globalOptions::noSubDirs = TRUE; } else if (!_tcsnicmp(compareCurWordArg,_T("timeout"),7)) { compareCurWordArg += 7; if (*compareCurWordArg == _T(':')) compareCurWordArg++; globalOptions::timeout = _tcstoul(compareCurWordArg,10); if (!globalOptions::timeout) { std::wcerr << eMsg(L"Warning: You are limiting to infinite time. Check one of your --timeout options!rn"); } } else if (!_tcsnicmp(compareCurWordArg,_T("tx"),2)) { compareCurWordArg += 2; if (*compareCurWordArg == _T(':')) compareCurWordArg++; globalOptions::timeout = _tcstoul(compareCurWordArg,10); if (!globalOptions::timeout) { std::wcerr << eMsg(L"Warning: You are limiting to infinite time. Check one of your --timeout options!rn"); } } else { throw eMsg(L"Could not understand word argument! Ensure all of your directives are spelled correctly. Terminate."); } } 我发布了一个长的,但它超过500行. 是否有更好的方法来处理这个特定问题,或者我应该把它作为一个长方法? 编辑:我不是在寻找一个令牌化的库 – 我已经完成了那个肮脏的工作.我很好奇是否有必要从更大的脏方法中创建存根方法. Billy3 解决方法
我确信Windows的等效getopt(3)功能.
这是谷歌的首次亮相 – Pete Wilson. 或者你可以查看 Boost Program Options的一个体面的C库. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |