/** ** Copyright (c) 2007-2009 Illumina, Inc. ** ** This software is covered by the "Illumina Genome Analyzer Software ** License Agreement" and the "Illumina Source Code License Agreement", ** and certain third party copyright/licenses, and any user of this ** source file is bound by the terms therein (see accompanying files ** Illumina_Genome_Analyzer_Software_License_Agreement.pdf and ** Illumina_Source_Code_License_Agreement.pdf and third party ** copyright/license notices). ** ** This file is part of the Consensus Assessment of Sequence And VAriation ** (CASAVA) software package. ** ** \file Alignment.hh ** ** \brief Support for command-line options ** ** \deprecated ** ** \author Come Raczy **/ #ifndef CASAVA_COMMON_COMMAND_LINE_HH #define CASAVA_COMMON_COMMAND_LINE_HH #include #include #include namespace casava { namespace common { /** ** Base class for all command line option exceptions. **/ class OptionException: public std::invalid_argument { public: OptionException(const std::string &what_arg) : std::invalid_argument(what_arg) { } }; class UnknownOptionException: public OptionException { public: UnknownOptionException(const std::string &what_arg) : OptionException(what_arg) { } }; class MissingOptionException: public OptionException { public: MissingOptionException(const std::string &what_arg) : OptionException(what_arg) { } }; class InvalidArgumentException: public OptionException { public: InvalidArgumentException(const std::string &what_arg) : OptionException(what_arg) { } }; // traits for conversion operations from string to any target type template struct convert_traits; /** ** Base class for the processing of the command line options. ** The methode "init" provides a common behavior for the processing of the command line options. ** It is the responsibility of the used code to implement the three pure virtual ** methods getOptions, processOptions and getParameters. ** ** - getOptions: sets flags and copies the arguments of the options into ** members of the derived class. Does *NOT* perform any ** processing other than converting the strings into the ** target types on a 1-1 basis (for instance using the ** provided 'convert' template). It is the responsibility of ** 'processOptions' to do any further processing, such as ** validation, parsing, combination, etc. ** ** - processOptions: validate the values set by getOptions and consolidate ** the command line options into useful options for the ** application. ** ** - getArguments: get all the arguments which were not recognized as ** command line options (for instance a list of file names). ** **/ class Options { public: static const int RUN = 0; static const int NO_RUN = -1; virtual ~Options() { } inline int init(int argc, char * const argv[]) throw () { try { const int retCode = getOptions(argc, argv); if (RUN == retCode) { processOptions(); getParameters(argc, argv); return RUN; } else { return retCode; } } catch (const OptionException &e) { return usage(argc, argv, e.what()); } } protected: // template to directly convert an argument string into the right (streamable) type template static T convert(const std::string &arg); private: virtual int getOptions(int argc, char * const argv[]) throw (UnknownOptionException, MissingOptionException, InvalidArgumentException) = 0; virtual void processOptions() throw (InvalidArgumentException) = 0; virtual void getParameters(int argc, char * const argv[]) throw (InvalidArgumentException) = 0; virtual int usage(int argc, char * const argv[], const std::string &message) = 0; }; template struct convert_traits { // provide some information about the targeted type of the argument static const std::string name() { return "unknown"; } }; template T Options::convert(const std::string &arg) { std::istringstream is(arg); T result; is >> result; if (is.fail() || !is.eof()) throw InvalidArgumentException(std::string("expected a ") + convert_traits::name() + ", got " + std::string(arg)); return result; } template<> struct convert_traits { static const std::string name() { return "float"; } }; template<> struct convert_traits { static const std::string name() { return "double"; } }; template<> struct convert_traits { static const std::string name() { return "int"; } }; template<> struct convert_traits { static const std::string name() { return "unsigned int"; } }; template<> struct convert_traits { static const std::string name() { return "long"; } }; template<> struct convert_traits { static const std::string name() { return "unsigned long"; } }; } // namespace common } // namespace casava #endif // #ifndef CASAVA_COMMON_COMMAND_LINE_HH