📄 test_ncbiargs.cpp
字号:
args["if"].AsInputFile () >> tmp; args["of"].AsOutputFile() << tmp << endl; }}// Optionalstatic void s_InitTest3(CArgDescriptions& arg_desc){ arg_desc.AddOptionalKey("k1", "fistOptionalKey", "This is an optional argument", CArgDescriptions::eString); arg_desc.AddOptionalKey("k2", "secondOptionalKey", "This is an optional argument", CArgDescriptions::eString); arg_desc.AddFlag("f1", "Flag 1"); arg_desc.AddFlag("f2", "Flag 2");}static void s_RunTest3(const CArgs& args, ostream& os){ if (args["k1"]) os << "k1=" << args["k1"].AsString() << endl; if (args["k2"]) os << "k2=" << args["k2"].AsString() << endl;}// Data typesstatic void s_InitTest2(CArgDescriptions& arg_desc){ arg_desc.AddKey("ka", "alphaNumericKey", "This is a test alpha-num key argument", CArgDescriptions::eString); arg_desc.AddKey("kb", "booleanKey", "This is a test boolean key argument", CArgDescriptions::eBoolean); arg_desc.AddKey("ki", "integerKey", "This is a test integer key argument", CArgDescriptions::eInteger); arg_desc.AddKey("kd", "doubleKey", "This is a test double key argument", CArgDescriptions::eDouble);}static void s_RunTest2(const CArgs& args, ostream& os){ os << "ka=" << args["ka"].AsString() << endl; os << "kb=" << NStr::BoolToString( args["kb"].AsBoolean() ) << endl; os << "ki=" << args["ki"].AsInteger() << endl; os << "kd=" << args["kd"].AsDouble() << endl;}// The simplest teststatic void s_InitTest1(CArgDescriptions& arg_desc){ arg_desc.AddKey("k", "key", "This is a key argument", CArgDescriptions::eString);}static void s_RunTest1(const CArgs& args, ostream& os){ os << "k=" << args["k"].AsString() << endl;}/////////////////////////////////////////////////////////////////////////////// Tests' arraystruct STest { void (*init)(CArgDescriptions& arg_desc); void (*run)(const CArgs& args, ostream& os);};static STest s_Test[] ={ {s_InitTest0, s_RunTest0}, // default {s_InitTest1, s_RunTest1}, {s_InitTest2, s_RunTest2}, {s_InitTest3, s_RunTest3}, {s_InitTest4, s_RunTest4}, {s_InitTest5, s_RunTest5}, {s_InitTest6, s_RunTest6}, {s_InitTest7, s_RunTest7}, {s_InitTest8, s_RunTest8}, {s_InitTest9, s_RunTest9}, {s_InitTest0, s_RunTest0}, {0, 0}};/////////////////////////////////////////////////////////////////////////////// CArgTestApplication::class CArgTestApplication : public CNcbiApplication{public: CArgTestApplication();private: virtual void Init(void); virtual int Run(void); virtual void Exit(void); size_t m_TestNo;};// Constructor -- setting version testCArgTestApplication::CArgTestApplication(){ SetVersion(CVersionInfo(1,2,3,"NcbiArgTest"));} // Choose the test to run, and// Setup arg.descriptions accordinglyvoid CArgTestApplication::Init(void){ // Set err.-posting and tracing on maximum SetDiagTrace(eDT_Enable); SetDiagPostFlag(eDPF_All); SetDiagPostLevel(eDiag_Info); // Get test # from env.variable $TEST_NO m_TestNo = 0; size_t max_test = sizeof(s_Test) / sizeof(s_Test[0]) - 2; const string& test_str = GetEnvironment().Get("TEST_NO"); if ( !test_str.empty() ) { try { m_TestNo = NStr::StringToULong(test_str); } catch (...) { m_TestNo = 0; } if (m_TestNo > max_test) { m_TestNo = 0; } } // The "no-test" case if ( !s_Test[m_TestNo].init ) return; // Create cmd-line argument descriptions class auto_ptr<CArgDescriptions> arg_desc(new CArgDescriptions); // Specify USAGE context string prog_description = "This is a test program for command-line argument processing.\n" "TEST #" + NStr::UIntToString(m_TestNo) + " (To run another test, set env.variable $TEST_NO to 0.." + NStr::UIntToString(max_test) + ")"; bool usage_sort_args = (m_TestNo == 10); arg_desc->SetUsageContext(GetArguments().GetProgramBasename(), prog_description, usage_sort_args); // Describe cmd-line arguments according to the chosen test # s_Test[m_TestNo].init(*arg_desc); // Setup arg.descriptions for this application SetupArgDescriptions(arg_desc.release());}// Printout arguments obtained from cmd.-lineint CArgTestApplication::Run(void){ cout << string(72, '=') << endl; // The "no-test" case if ( !s_Test[m_TestNo].run ) { cout << "No arguments described." << endl; return 0; } // Do run s_Test[m_TestNo].run(GetArgs(), cout); // Printout obtained argument values string str; cout << GetArgs().Print(str) << endl; return 0;}// Cleanupvoid CArgTestApplication::Exit(void){ SetDiagStream(0);}/////////////////////////////////////////////////////////////////////////////// MAINint main(int argc, const char* argv[]){ // Execute main application function return CArgTestApplication().AppMain(argc, argv, 0, eDS_Default, 0);}/* * =========================================================================== * $Log: test_ncbiargs.cpp,v $ * Revision 1000.1 2004/06/01 19:09:55 gouriano * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R6.22 * * Revision 6.22 2004/05/14 13:59:51 gorelenk * Added include of ncbi_pch.hpp * * Revision 6.21 2002/12/26 17:13:28 ivanov * Added version info test * * Revision 6.20 2002/07/15 18:17:25 gouriano * renamed CNcbiException and its descendents * * Revision 6.19 2002/07/11 14:18:29 gouriano * exceptions replaced by CNcbiException-type ones * * Revision 6.18 2002/04/16 18:49:07 ivanov * Centralize threatment of assert() in tests. * Added #include <test/test_assert.h>. CVS log moved to end of file. * * Revision 6.17 2001/03/26 16:52:19 vakatov * Fixed a minor warning * * Revision 6.16 2000/12/24 00:13:00 vakatov * Radically revamped NCBIARGS. * Introduced optional key and posit. args without default value. * Added new arg.value constraint classes. * Passed flags to be detected by HasValue() rather than AsBoolean. * Simplified constraints on the number of mandatory and optional extra args. * Improved USAGE info and diagnostic messages. Etc... * * Revision 6.15 2000/11/29 00:09:19 vakatov * Added test #10 -- to auto-sort flag and key args alphabetically * * Revision 6.14 2000/11/24 23:37:46 vakatov * The test is now "CNcbiApplication" based (rather than "bare main()") * -- to use and to test standard processing of cmd.-line arguments implemented * in "CNcbiApplication". * Also, test for CArgValue::CloseFile(). * * Revision 6.13 2000/11/22 22:04:32 vakatov * Added special flag "-h" and special exception CArgHelpException to * force USAGE printout in a standard manner * * Revision 6.12 2000/11/22 19:40:51 vakatov * s_Test3() -- fixed: Exist() --> IsProvided() * * Revision 6.11 2000/11/20 19:49:40 vakatov * Test0:: printout all arg values * * Revision 6.10 2000/11/17 22:04:31 vakatov * CArgDescriptions:: Switch the order of optional args in methods * AddOptionalKey() and AddPlain(). Also, enforce the default value to * match arg. description (and constraints, if any) at all times. * * Revision 6.9 2000/11/13 20:31:09 vakatov * Wrote new test, fixed multiple bugs, ugly "features", and the USAGE. * * Revision 6.8 2000/10/20 20:26:11 butanaev * Modified example #9. * * Revision 6.7 2000/10/11 21:03:50 vakatov * Cleanup to avoid 64-bit to 32-bit values truncation, etc. * (reported by Forte6 Patch 109490-01) * * Revision 6.6 2000/10/06 21:57:07 butanaev * Added Allow() function. Added classes CArgAllowValue, CArgAllowIntInterval. * * Revision 6.5 2000/09/29 17:11:01 butanaev * Got rid of IsDefaultValue(), added IsProvided(). * * Revision 6.4 2000/09/28 21:00:21 butanaev * fPreOpen with opposite meaning took over fDelayOpen. * IsDefaultValue() added which returns true if no * value for an optional argument was provided in cmd. line. * * Revision 6.3 2000/09/22 21:26:28 butanaev * Added example with default arg values. * * Revision 6.2 2000/09/12 15:01:30 butanaev * Examples now switching by environment variable EXAMPLE_NUM. * * Revision 6.1 2000/08/31 23:55:35 vakatov * Initial revision * * =========================================================================== */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -