📄 ncbiargs.hpp
字号:
/// Available argument types. enum EType { eString = 0, ///< An arbitrary string eBoolean, ///< {'true', 't', 'false', 'f'}, case-insensitive eInteger, ///< Convertible into an integer number (int) eDouble, ///< Convertible into a floating point number (double) eInputFile, ///< Name of file (must exist and be readable) eOutputFile, ///< Name of file (must be writeable) k_EType_Size ///< For internal use only }; /// Get argument type's name string. static const string& GetTypeName(EType type); /// File related flags. /// /// Must match the argument type, or an exception will be thrown. /// Used for eInputFile and eOutputFiler argument types. enum EFlags { fPreOpen = 0x1, ///< Open file right away for eInputFile, eOutputFile fBinary = 0x2, ///< Open as binary file for eInputFile, eOutputFile fAppend = 0x10 ///< Append to end-of-file for eOutputFile only }; typedef unsigned int TFlags; ///< Binary OR of "EFlags" /// Add description for mandatory key. /// /// Mandatory key has the syntax: /// /// arg_key := -<key> <value> /// /// Will throw exception CArgException if: /// - description with name "name" already exists /// - "name" contains symbols other than {alnum} /// - "synopsis" contains symbols other than {alnum, '_'} /// - "flags" are inconsistent with "type" /// /// Any argument can be later referenced using its unique name "name". void AddKey(const string& name, ///< Name of argument key const string& synopsis, ///< Synopis for argument const string& comment, ///< Argument description EType type, ///< Argument type TFlags flags = 0 ///< Optional file related flags ); /// Add description for optional key without default value. /// /// Optional key without default value has the following syntax: /// /// arg_key_opt := [-<key> <value>] /// /// Will throw exception CArgException if: /// - description with name "name" already exists /// - "name" contains symbols other than {alnum} /// - "synopsis" contains symbols other than {alnum, '_'} /// - "flags" are inconsistent with "type" /// /// Any argument can be later referenced using its unique name "name". void AddOptionalKey(const string& name, ///< Name of argument key const string& synopsis, ///< Synopis for argument const string& comment, ///< Argument description EType type, ///< Argument type TFlags flags = 0 ///< Optional file flags ); /// Add description for optional key with default value. /// /// Optional key with default value has the following syntax: /// /// arg_key_dflt := [-<key> <value>] /// /// Will throw exception CArgException if: /// - description with name "name" already exists /// - "name" contains symbols other than {alnum} /// - "synopsis" contains symbols other than {alnum, '_'} /// - "flags" are inconsistent with "type" /// /// Any argument can be later referenced using its unique name "name". void AddDefaultKey(const string& name, ///< Name of argument key const string& synopsis, ///< Synopis for argument const string& comment, ///< Argument description EType type, ///< Argument type const string& default_value, ///< Default value TFlags flags = 0 ///< Optional file flags ); /// Add description for flag argument. /// /// Flag argument has the following syntax: /// /// arg_flag := -<flag>, <flag> := "name" /// /// If argument "set_value" is TRUE, then: /// - if the flag is provided (in the command-line), then the resultant /// CArgValue::HasValue() will be TRUE; /// - else it will be FALSE. /// /// Setting argument "set_value" to FALSE will reverse the above meaning. /// /// NOTE: If CArgValue::HasValue() is TRUE, then AsBoolean() is /// always TRUE. void AddFlag(const string& name, ///< Name of argument const string& comment, ///< Argument description bool set_value = true ///< Is value set or not? ); /// Add description for mandatory postional argument. /// /// Mandatory positional argument has the following syntax: /// /// arg_pos := <value> /// /// NOTE: For all types of positional arguments: /// - The order is important! That is, the N-th positional argument passed /// in the cmd.-line will be matched against (and processed according to) /// the N-th added named positional argument description. /// - Mandatory positional args always go first. /// /// Will throw exception CArgException if: /// - description with name "name" already exists /// - "name" contains symbols other than {alnum} /// - "flags" are inconsistent with "type" /// /// Any argument can be later referenced using its unique name "name". void AddPositional(const string& name, ///< Name of argument const string& comment, ///< Argument description EType type, ///< Argument type TFlags flags = 0 ///< Optional file flags ); /// Add description for optional positional argument without default /// value. /// /// Optional positional argument, without default value has the following /// syntax: /// /// arg_pos_opt := [<value>] /// /// Will throw exception CArgException if: /// - description with name "name" already exists /// - "name" contains symbols other than {alnum} /// - "flags" are inconsistent with "type" /// /// Any argument can be later referenced using its unique name "name". /// @sa /// NOTE for AddPositional() void AddOptionalPositional(const string& name, ///< Name of argument const string& comment, ///< Argument descr. EType type, ///< Argument type TFlags flags = 0 ///< Optional file flgs ); /// Add description for optional positional argument with default value. /// /// Optional positional argument with default value has the following /// syntax: /// /// arg_pos_dflt := [<value>] /// /// Will throw exception CArgException if: /// - description with name "name" already exists /// - "name" contains symbols other than {alnum} /// - "flags" are inconsistent with "type" /// /// @sa /// NOTE for AddPositional() void AddDefaultPositional(const string& name, ///< Name of argument const string& comment,///< Argument description EType type, ///< Argument type const string& default_value, ///< Default value TFlags flags = 0 ///< Optional file flags ); /// Add description for extra unnamed positional arguments. /// /// Provide description for the extra unnamed positional arguments. /// By default, no extra args are allowed. /// The name of this description is always an empty string. /// Names of the resulting arg.values will be: "#1", "#2", ... /// /// Will throw exception CArgException if: /// - description with name "name" already exists /// - "flags" are inconsistent with "type" void AddExtra(unsigned n_mandatory, ///< Number of mandatory args. unsigned n_optional, ///< Number of optional args. const string& comment, ///< Argument description EType type, ///< Argument type TFlags flags = 0 ///< Optional file flags ); /// Set additional user defined constraint on argument value. /// /// Constraint is defined by CArgAllow and its derived classes. /// The constraint object must be allocated by "new", and it must NOT be /// freed by "delete" after it has been passed to CArgDescriptions! /// @sa /// See "CArgAllow_***" classes for some pre-defined constraints void SetConstraint(const string& name, CArgAllow* constraint); /// Check if there is already an argument description with specified name. bool Exist(const string& name) const; /// Delete description with name "name". /// /// Throw the CArgException (eSynposis error code) exception if the /// specified name cannot be found. void Delete(const string& name); /// Set extra info to be used by PrintUsage(). void SetUsageContext(const string& usage_name, ///< Program name const string& usage_description, ///< Usage descr. bool usage_sort_args = false,///< Sort args. SIZE_TYPE usage_width = 78); ///< Format width /// Print usage and exit. /// /// Force to print USAGE unconditionally (and then exit) if no /// command-line args are present. void PrintUsageIfNoArgs(bool do_print = true); /// Print usage message to end of specified string. /// /// Printout USAGE and append to the string "str" using provided /// argument descriptions and usage context. /// @return /// Appended "str" virtual string& PrintUsage(string& str) const; /// Verify if argument "name" is spelled correctly. /// /// Argument name can contain only alphanumeric characters and /// underscore ('_'), or be empty. static bool VerifyName(const string& name, bool extended = false);private: typedef set< AutoPtr<CArgDesc> > TArgs; ///< Argument descr. type typedef TArgs::iterator TArgsI; ///< Arguments iterator typedef TArgs::const_iterator TArgsCI; ///< Const arguments iterator typedef /*deque*/vector<string> TPosArgs; ///< Positional arg. vector typedef list<string> TKeyFlagArgs; ///< List of flag arguments TArgs m_Args; ///< Assoc.map of arguments' name/descr TPosArgs m_PosArgs; ///< Pos. args, ordered by position in cmd.-line TKeyFlagArgs m_KeyFlagArgs; ///< Key/flag args, in order of insertion unsigned m_nExtra; ///> # of mandatory extra args unsigned m_nExtraOpt; ///< # of optional extra args // Extra USAGE info string m_UsageName; ///< Program name string m_UsageDescription; ///< Program description bool m_UsageSortArgs; ///< Sort alphabetically on usage printout SIZE_TYPE m_UsageWidth; ///< Maximum length of a usage line bool m_AutoHelp; ///< Special flag "-h" activated bool m_UsageIfNoArgs; ///< Print usage and exit if no args passed // Internal methods /// Helper method to find named parameter. TArgsI x_Find(const string& name); /// Helper method to find named parameter -- const version. TArgsCI x_Find(const string& name) const; // Helper method for adding description. void x_AddDesc(CArgDesc& arg); /// Helper method for doing pre-processing consistency checks. void x_PreCheck(void) const; /// Helper method for checking if auto help requested and throw /// CArgHelpException if help requested. void x_CheckAutoHelp(const string& arg) const; /// Process arguments. /// /// Helper method to process arguments and build a CArgs object that is /// passed as the args parameter. /// @return /// TRUE if specified "arg2" was used. bool x_CreateArg(const string& arg1, ///< Argument to process bool have_arg2, ///< Is there an arg. that follows? const string& arg2, ///< Following argument unsigned* n_plain, ///< Indicates number of args CArgs& args ///< Contains processed args ) const; /// Helper method for doing post-processing consistency checks. void x_PostCheck(CArgs& args, unsigned n_plain) const;public: /// Create parsed arguments in CArgs object. /// /// Parse command-line arguments, and create "CArgs" args object /// from the passed command-line arguments "argc" and "argv". /// /// Throw /// - CArgException on error /// - CArgHelpException if USAGE printout was requested ("-h" flag) /// /// NOTE: You can deallocate the resulting "CArgs" object using 'delete'. /// /// Examples: /// /// (1) int main(int argc, const char* argv[]) { /// CreateArgs(desc, argc, argv); /// } /// /// (2) CNcbiArguments ncbi_args; /// CreateArgs(desc, ncbi_args.Size(), ncbi_args); template<class TSize, class TArray> CArgs* CreateArgs(TSize argc, TArray argv) const { // Pre-processing consistency checks x_PreCheck(); // Check for "-h" flag if ( m_AutoHelp ) { for (TSize i = 1; i < argc; i++) { x_CheckAutoHelp(argv[i]); } } // Create new "CArgs" to fill up, and parse cmd.-line args into it auto_ptr<CArgs> args(new CArgs()); unsigned int n_plain = kMax_UInt; for (TSize i = 1; i < argc; i++) { bool have_arg2 = (i + 1 < argc); if ( x_CreateArg(argv[i], have_arg2, have_arg2 ? (string) argv[i+1] : kEmptyStr, &n_plain, *args) ) i++; } // Check if there were any arguments at all if (n_plain == kMax_UInt) { n_plain = 0; } // Post-processing consistency checks x_PostCheck(*args, n_plain); return args.release(); } CArgs* CreateArgs(const CNcbiArguments& argv) const;};/////////////////////////////////////////////////////////////////////////////////// CArgAllow --////// Abstract base class for defining argument constraints.////// Other user defined constraints are defined by deriving from this abstract/// base class:////// - CArgAllow_Symbols -- symbol from a set of allowed symbols/// - CArgAllow_String -- string to contain only allowed symbols /// - CArgAllow_Strings -- string from a set of allowed strings/// - CArgAllow_Integers -- integer value to fall within a given interval/// - CArgAllow_Doubles -- floating-point value to fall in a given intervalclass NCBI_XNCBI_EXPORT CArgAllow : public CObject{public: /// Verify if specified value is allowed. virtual bool Verify(const string &value) const = 0; /// Get usage information. virtual string GetUsage(void) const = 0;protected: /// Protected destructor. /// /// Prohibit from the allocation on stack or in the static data segment, /// and from the explicit deallocation by "delete". virtual ~CArgAllow(void);};/////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -