// file: $(NEDC_NFC)/class/cpp/Cmdl/Cmdl.h // // Revision History: // 20200518 (JP): upgraded to use Posix-style command line arguments // 20160209 (JP): revised and restructured // 20160205 (SL): initial version // // make sure definitions are only made once // #ifndef NEDC_CMDL #define NEDC_CMDL // local boost-related include files: // these file are required to build the boost command line parser // #include // set up associated namespaces // namespace bpo = boost::program_options; // local include files // #include // Cmdl: a class that handles the parsing of command line arguments. // This class is designed to make command line parsing common across // all our C++ tools. // class Cmdl { //--------------------------------------------------------------------------- // // public constants // //--------------------------------------------------------------------------- public: // define the class name // static const char* CLASS_NAME; //---------------------------------------- // // other important constants // //---------------------------------------- // default values for message-related parameters // static const char* ARG_USAGE; static const char* ARG_HELP; static const char* ARG_HELP_NAME; static const char* ARG_DBGL; static const char* ARG_VRBL; static const char* ARG_ARGS; // define the max environment variable name size // static const long MAX_ENV_SIZE = 99; // define the max filename size // static const long MAX_FNAME_SIZE = 499; // define the max help line size for a help message // static const long MAX_HLINE_SIZE = 99; // define the character used to delimit environment variables // static const char DELIM_ENV_VAR = '$'; // define the character used to delimit a directory // static const char DELIM_DIR = '/'; //--------------------------------------------------------------------------- // // protected data // //--------------------------------------------------------------------------- protected: // define debug level and verbosity // static Dbgl dbgl_d; static Vrbl vrbl_d; // help message related variables // char* usage_fname_d; char* help_fname_d; // boost data structures: // these are used to do the heavy lifting on parsing // bpo::options_description* bst_desc_d; bpo::positional_options_description* bst_pos_d; bpo::variables_map bst_vm_d; //--------------------------------------------------------------------------- // // required public methods // //--------------------------------------------------------------------------- public: // method name // inline static const char* name() { return CLASS_NAME; } // method: destructor // ~Cmdl(); // method: default constructor // Cmdl(): Cmdl((char*)NULL, (char*)NULL) {} Cmdl(const char* usage, const char* help = (char*)NULL); //--------------------------------------------------------------------------- // // other public methods // //--------------------------------------------------------------------------- public: // set methods // bool set_msgs(const char* usage, const char* help); // method: add_argument // // this version is used for arguments that take values. this method // has to be in the header file because it is a template. // template bool add_argument(const char* name) { bst_desc_d->add_options() (name, bpo::value(), ""); return true; } // method: add_argument // this version is used for arguments that do not take any values. // this method is an overload of the template version above. // bool add_argument(const char* name) { bst_desc_d->add_options() (name, ""); return true; } // parse methods // bool parse_args(int argc, const char** argv); // method: get_value // this method has to be in the header file because it is a template // function. note that parse will fail if an unknown argument name is // specified, and this function is called after parse, so this will // only be called with valid argument names. therefore, no error-checking // is performed. // template bool get_value(T& value, const char* name) { if (bst_vm_d.count(name) == false) { return false; } else { value = bst_vm_d[name].as(); return true; } } // method: count // this method is used to check the existence of an argument. it passes // through the value returned from the boost parser. // int count(const char* name) { return bst_vm_d.count(name); } // display methods // bool display_usage(FILE* fp); bool display_help(FILE* fp); // environment methods // bool expand_filename(char* exp_name, const char* orig_name); //--------------------------------------------------------------------------- // // private methods // //--------------------------------------------------------------------------- private: }; // end of include file // #endif