📄 test_ncbiargs.cpp
字号:
/* * =========================================================================== * PRODUCTION $Log: test_ncbiargs.cpp,v $ * PRODUCTION Revision 1000.1 2004/06/01 19:09:55 gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R6.22 * PRODUCTION * =========================================================================== *//* $Id: test_ncbiargs.cpp,v 1000.1 2004/06/01 19:09:55 gouriano Exp $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Authors: Anton Butanayev, Denis Vakatov * * File Description: * Test for the command-line arguments' processing ("ncbiargs.[ch]pp"): * */#include <ncbi_pch.hpp>#include <corelib/ncbiapp.hpp>#include <corelib/ncbienv.hpp>#include <corelib/ncbiargs.hpp>#include <test/test_assert.h> /* This header must go last */USING_NCBI_SCOPE;// Extended test for all different types of arguments [default test]static void s_InitTest0(CArgDescriptions& arg_desc){ // Describe the expected command-line arguments arg_desc.AddOptionalPositional ("logfile", "This is an optional named positional argument without default value", CArgDescriptions::eOutputFile, CArgDescriptions::fPreOpen | CArgDescriptions::fBinary); arg_desc.AddFlag ("f2", "This is another flag argument: FALSE if set, TRUE if not set", false); arg_desc.AddPositional ("barfooetc", "This is a mandatory plain (named positional) argument", CArgDescriptions::eString); arg_desc.SetConstraint ("barfooetc", &(*new CArgAllow_Strings, "foo", "bar", "etc")); arg_desc.AddDefaultKey ("kd", "DefaultKey", "This is an optional integer key argument, with default value", CArgDescriptions::eInteger, "123"); arg_desc.SetConstraint ("kd", new CArgAllow_Integers(0, 200)); arg_desc.AddExtra (0, // no mandatory extra args 3, // up to 3 optional extra args "These are the optional extra (unnamed positional) arguments. " "They will be printed out to the file specified by the " "2nd positional argument,\n\"logfile\"", CArgDescriptions::eBoolean); arg_desc.AddKey ("k", "MandatoryKey", "This is a mandatory alpha-num key argument", CArgDescriptions::eString); arg_desc.SetConstraint ("k", new CArgAllow_String(CArgAllow_Symbols::eAlnum)); arg_desc.AddOptionalKey ("ko", "OptionalKey", "This is another optional key argument, without default value", CArgDescriptions::eBoolean); arg_desc.AddFlag ("f1", "This is a flag argument: TRUE if set, FALSE if not set"); arg_desc.AddDefaultPositional ("one_symbol", "This is an optional named positional argument with default value", CArgDescriptions::eString, "a"); arg_desc.SetConstraint ("one_symbol", new CArgAllow_Symbols(" aB\tCd"));}static void s_RunTest0(const CArgs& args, ostream& os){ assert(!args.Exist(kEmptyStr)); // never exists; use #1, #2, ... instead assert(args.Exist("f1")); assert(args.Exist("logfile")); assert(args["barfooetc"]); if ( !args["logfile"] ) return; // Printout argument values os << "Printing arguments to file `" << args["logfile"].AsString() << "'..." << endl; ostream& lg = args["logfile"].AsOutputFile(); lg << "k: " << args["k"].AsString() << endl; lg << "barfooetc: " << args["barfooetc"].AsString() << endl; lg << "logfile: " << args["logfile"].AsString() << endl; if ( args["ko"] ) { lg << "ko: " << NStr::BoolToString(args["ko"].AsBoolean()) << endl; } else { lg << "ko: not provided" << endl; bool is_thrown = false; try { (void) args["ko"].AsString(); } catch (CArgException& e) { is_thrown = true; NCBI_REPORT_EXCEPTION("CArgException is thrown:",e); } assert(is_thrown); } if ( args["f1"] ) { assert(args["f1"].AsBoolean()); } if ( args["f2"] ) { assert(args["f2"].AsBoolean()); } // Extra (unnamed positional) arguments if ( args.GetNExtra() ) { for (size_t extra = 1; extra <= args.GetNExtra(); extra++) { lg << "#" << extra << ": " << NStr::BoolToString(args[extra].AsBoolean()) << " (passed as `" << args[extra].AsString() << "')" << endl; } } else { lg << "(no unnamed positional arguments passed in the cmd-line)" << endl; } // Separator lg << string(44, '-') << endl;}// Allowingstatic void s_InitTest9(CArgDescriptions& arg_desc){ arg_desc.AddKey("a", "alphaNumericKey", "This is a test alpha-num argument", CArgDescriptions::eString); arg_desc.AddKey("i", "integerKey", "This is a test integer argument", CArgDescriptions::eInteger); arg_desc.SetConstraint("a", &(*new CArgAllow_Strings, "foo", "bar", "qq")); arg_desc.SetConstraint("i", new CArgAllow_Integers(-3, 34));}static void s_RunTest9(const CArgs& args, ostream& os){ os << "a=" << args["a"].AsString() << endl; os << "i=" << args["i"].AsInteger() << endl;}// Argument with default waluestatic void s_InitTest8(CArgDescriptions& arg_desc){ arg_desc.AddDefaultKey ("k", "alphaNumericKey", "This is an optional argument with default value", CArgDescriptions::eString, "CORELIB");}static void s_RunTest8(const CArgs& args, ostream& os){ os << "k=" << args["k"].AsString() << endl;}// Position arguments - advancedstatic void s_InitTest7(CArgDescriptions& arg_desc){ arg_desc.AddPositional ("p2", "This is a plain argument", CArgDescriptions::eString); arg_desc.AddExtra (1, 3, "These are extra arguments", CArgDescriptions::eInteger); arg_desc.AddPositional ("p1", "This is a plain argument", CArgDescriptions::eBoolean);}static void s_RunTest7(const CArgs& args, ostream& os){ os << "p1=" << args["p1"].AsString() << endl; os << "p2=" << args["p2"].AsString() << endl; os << "#1=" << args["#1"].AsInteger() << endl; if ( args["#2"] ) { os << "#2=" << args["#2"].AsInteger() << endl; } if ( args["#3"] ) { os << "#3=" << args["#3"].AsInteger() << endl; }}// Position argumentsstatic void s_InitTest6(CArgDescriptions& arg_desc){ arg_desc.AddDefaultPositional ("p1", "This is a positional argument with default value", CArgDescriptions::eDouble, "1.23"); arg_desc.AddPositional ("p2", "This is a mandatory positional argument", CArgDescriptions::eBoolean);}static void s_RunTest6(const CArgs& args, ostream& os){ os << "p1=" << args["p1"].AsString() << endl; os << "p2=" << args["p2"].AsString() << endl;}// Files - advancedstatic void s_InitTest5(CArgDescriptions& arg_desc){ arg_desc.AddKey("if", "inputFile", "This is an input file argument", CArgDescriptions::eInputFile, CArgDescriptions::fPreOpen); arg_desc.AddKey("of", "outputFile", "This is an output file argument", CArgDescriptions::eOutputFile, CArgDescriptions::fAppend);}static void s_RunTest5(const CArgs& args, ostream& /*os*/){ // Close args["if"].CloseFile(); (void) args["of"].AsOutputFile(); args["of"].CloseFile(); // Auto-reopen (first time only) while ( !args["if"].AsInputFile().eof() ) { string tmp; args["if"].AsInputFile () >> tmp; args["of"].AsOutputFile() << tmp << endl; }}// Filesstatic void s_InitTest4(CArgDescriptions& arg_desc){ arg_desc.AddKey("if", "inputFile", "This is an input file argument", CArgDescriptions::eInputFile); arg_desc.AddKey("of", "outputFile", "This is an output file argument", CArgDescriptions::eOutputFile);}static void s_RunTest4(const CArgs& args, ostream& /*os*/){ while ( !args["if"].AsInputFile().eof() ) { string tmp;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -