This is a DEMO of script "scripts/new_project.sh" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~/tmp> $NCBI/c++/scripts/new_project.sh USAGE: new_project.sh [builddir] SYNOPSIS: Create a model makefile "Makefile._" to build a library or an application that uses pre-built NCBI C++ toolkit. Also include sample code when creating applications. ARGUMENTS: -- name of the project (will be subst. to the makefile name) -- one of the following: lib to build a library app[/basic] to build a simple application app/cgi to build a CGI or FastCGI application app/objects to build an application using ASN.1 objects app/objmgr to build an application using the object manager [builddir] -- path to the pre-built NCBI C++ toolkit (default = /netopt/ncbi_tools/c++/Debug/build) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~/tmp> $NCBI/c++/scripts/new_project.sh foo app/basic Created a model makefile "~/tmp/foo/Makefile.foo_app". Created a model source file "~/tmp/foo/foo.cpp". ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~/tmp> cd foo; ls Makefile.foo_app foo.cpp ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~/tmp/foo> cat Makefile.foo_app # # Makefile: # # This file was originally generated by shell script "new_project.sh" # # ### PATH TO A PRE-BUILT C++ TOOLKIT builddir = /netopt/ncbi_tools/c++/Debug/build # builddir = $(NCBI)/c++/Release/build ### DEFAULT COMPILATION FLAGS -- DON'T EDIT OR MOVE THESE 4 LINES !!! include $(builddir)/Makefile.mk srcdir = . BINCOPY = @: LOCAL_CPPFLAGS = -I. ############################################################################# ### EDIT SETTINGS FOR THE DEFAULT (APPLICATION) TARGET HERE ### APP = foo SRC = foo # OBJ = # PRE_LIBS = $(NCBI_C_LIBPATH) ..... LIB = xncbi # LIB = xser xhtml xcgi xconnect xutil xncbi ## If you need the C toolkit... # LIBS = $(NCBI_C_LIBPATH) -lncbi $(NETWORK_LIBS) $(ORIG_LIBS) # CPPFLAGS = $(ORIG_CPPFLAGS) $(NCBI_C_INCLUDE) # CFLAGS = $(ORIG_CFLAGS) # CXXFLAGS = $(ORIG_CXXFLAGS) # LDFLAGS = $(ORIG_LDFLAGS) # ### ############################################################################# ### APPLICATION BUILD RULES -- DON'T EDIT OR MOVE THIS LINE !!! include $(builddir)/Makefile.app ### PUT YOUR OWN ADDITIONAL TARGETS (MAKE COMMANDS/RULES) HERE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~/tmp/foo:124} make -f Makefile.foo_app /netopt/forte6u2/bin/CC -c +w +w2 -g -I. -D_DEBUG -I/netopt/ncbi_tools/c++/Debug/inc -I/netopt/ncbi_tools/c++/include -I/netopt/ncbi_tools/c++/include/ctools -I/netopt/ncbi_tools/c++/include/internal foo.cpp -o foo.o 2>&1 | /netopt/ncbi_tools/c++/compilers/cxx_filter.WorkShop53.sh /netopt/forte6u2/bin/CC -xildoff -g foo.o -R/netopt/ncbi_tools/c++/Debug/lib -L/netopt/ncbi_tools/c++/Debug/lib -lxncbi -o foo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~/tmp/foo> ./foo -h ======================================================================== USAGE foo -h -f1 [-kd DefaultKey] -k MandatoryKey [-ko OptionalKey] -f2 [logfile] [one_symbol] [....] DESCRIPTION CArgDescriptions demo program REQUIRED ARGUMENTS -k This is a mandatory alpha-num key argument barfooetc This is a mandatory plain (named positional) argument OPTIONAL ARGUMENTS -h Print this USAGE message; ignore other arguments -f1 This is a flag argument: TRUE if set, FALSE if not set -kd This is an optional integer key argument, with default value Default = `123' -ko This is another optional key argument, without default value -f2 This is another flag argument: FALSE if set, TRUE if not set logfile This is an optional named positional argument without default value one_symbol This is an optional named positional argument with default value Default = `a' .... These are the optional extra (unnamed positional) arguments. They will be printed out to the file specified by the 2nd positional argument, "logfile" NOTE: Specify no more than 3 arguments in "...." ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~/tmp/foo> ./foo -k hello bar ======================================================================== k: hello barfooetc: bar ko: not provided (no unnamed positional arguments passed in the cmd-line) -------------------------------------------- barfooetc = `bar' f1: f2 = `true' h: k = `hello' kd = `123' ko: logfile: one_symbol = `a' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /tmp/foo> cat foo.cpp /* $Id$ * =========================================================================== * * 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: Denis Vakatov, Vladimir Ivanov * * File Description: * Sample for the command-line arguments' processing ("ncbiargs.[ch]pp"): * */ #include #include #include USING_NCBI_SCOPE; ///////////////////////////////////////////////////////////////////////////// // CFooApplication:: class CFooApplication : public CNcbiApplication { private: virtual void Init(void); virtual int Run(void); virtual void Exit(void); }; ///////////////////////////////////////////////////////////////////////////// // Init test for all different types of arguments void CFooApplication::Init(void) { // Create command-line argument descriptions class auto_ptr arg_desc(new CArgDescriptions); // Specify USAGE context arg_desc->SetUsageContext(GetArguments().GetProgramBasename(), "CArgDescriptions demo program"); // 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 ("f1", "This is a flag argument: TRUE if set, FALSE if not set"); 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 ("f2", "This is another flag argument: FALSE if set, TRUE if not set", false); 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")); // Setup arg.descriptions for this application SetupArgDescriptions(arg_desc.release()); } ///////////////////////////////////////////////////////////////////////////// // Run test (printout arguments obtained from command-line) int CFooApplication::Run(void) { // Get arguments CArgs args = GetArgs(); // Do run cout << string(72, '=') << endl; // Stream to result output // (NOTE: "x_lg" is just a workaround for bug in SUN WorkShop 5.1 compiler) ostream* x_lg = args["logfile"] ? &args["logfile"].AsOutputFile() : &cout; ostream& lg = *x_lg; if ( args["logfile"] ) cout << "Printing arguments to file `" << args["logfile"].AsString() << "'..." << endl; // Printout argument values lg << "k: " << args["k"].AsString() << endl; lg << "barfooetc: " << args["barfooetc"].AsString() << endl; if ( args["logfile"] ) 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&) { is_thrown = true; } } // 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; // Printout obtained argument values string str; cout << args.Print(str) << endl; return 0; } ///////////////////////////////////////////////////////////////////////////// // Cleanup void CFooApplication::Exit(void) { SetDiagStream(0); } ///////////////////////////////////////////////////////////////////////////// // MAIN int main(int argc, const char* argv[]) { // Execute main application function return CFooApplication().AppMain(argc, argv, 0, eDS_Default, 0); } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~