jpayne@69: // Copyright © 2015, Battelle National Biodefense Institute (BNBI); jpayne@69: // all rights reserved. Authored by: Brian Ondov, Todd Treangen, jpayne@69: // Sergey Koren, and Adam Phillippy jpayne@69: // jpayne@69: // See the LICENSE.txt file included with this software for license information. jpayne@69: jpayne@69: #ifndef INCLUDED_Command jpayne@69: #define INCLUDED_Command jpayne@69: jpayne@69: #include jpayne@69: #include jpayne@69: #include jpayne@69: #include jpayne@69: jpayne@69: namespace mash { jpayne@69: jpayne@69: class Command jpayne@69: { jpayne@69: jpayne@69: // A base class for handling command line parsing and usage messages. Specific jpayne@69: // commands can be implemented by subclassing and overriding the run() method. jpayne@69: // The run() method will be called by this base class from run(argc, argv) once jpayne@69: // it has parsed options, which can be accessed from the 'options' map using the jpayne@69: // name strings given when adding them (not 'identifier', which is its parsing jpayne@69: // name to appear after the '-'). Arguments (i.e. operands given after the jpayne@69: // options on the command line) will be available in the 'arguments' vector. jpayne@69: jpayne@69: public: jpayne@69: jpayne@69: class Option jpayne@69: { jpayne@69: public: jpayne@69: jpayne@69: enum Type jpayne@69: { jpayne@69: Boolean, jpayne@69: Number, jpayne@69: Integer, jpayne@69: Size, jpayne@69: File, jpayne@69: String jpayne@69: } jpayne@69: type; jpayne@69: jpayne@69: std::string category; jpayne@69: std::string identifier; jpayne@69: std::string description; jpayne@69: std::string argument; jpayne@69: std::string argumentDefault; jpayne@69: jpayne@69: float argumentAsNumber; jpayne@69: float argumentMin; jpayne@69: float argumentMax; jpayne@69: jpayne@69: bool active; jpayne@69: bool changed; jpayne@69: jpayne@69: Option() {} jpayne@69: Option(Type typeNew, std::string identifierNew, std::string categoryNew, std::string descriptionNew, std::string argumentDefaultNew = "", float argumentMinNew = 0, float argumentMaxNew = 0); jpayne@69: jpayne@69: float getArgumentAsNumber() const {return argumentAsNumber;} jpayne@69: void setArgument(std::string argumentNew); jpayne@69: }; jpayne@69: jpayne@69: Command(); jpayne@69: virtual ~Command() {}; jpayne@69: jpayne@69: void addOption(std::string name, Option option); jpayne@69: const Option & getOption(std::string name) const; jpayne@69: inline bool hasOption(std::string name) const {return options.count(name);} jpayne@69: void print() const; jpayne@69: virtual int run() const = 0; jpayne@69: int run(int argc, const char ** argv); jpayne@69: jpayne@69: std::string name; jpayne@69: std::string summary; jpayne@69: std::string description; jpayne@69: std::string argumentString; jpayne@69: jpayne@69: protected: jpayne@69: jpayne@69: void useOption(std::string name); jpayne@69: void useSketchOptions(); jpayne@69: jpayne@69: std::map options; jpayne@69: std::map optionsAvailable; jpayne@69: std::vector arguments; jpayne@69: jpayne@69: private: jpayne@69: jpayne@69: void addAvailableOption(std::string name, Option option); jpayne@69: void addCategory(std::string name, std::string displayName); jpayne@69: jpayne@69: std::map optionNamesByIdentifier; jpayne@69: std::map > optionNamesByCategory; jpayne@69: std::vector categories; jpayne@69: std::map categoryDisplayNames; jpayne@69: }; jpayne@69: jpayne@69: inline const Command::Option & Command::getOption(std::string name) const {return options.at(name);} jpayne@69: void splitFile(const std::string & file, std::vector & lines); jpayne@69: void printColumns(const std::vector> & columns, int indent = 2, int spacing = 2, const char * missing = "-", int max = 80); jpayne@69: void printColumns(const std::vector> & columns, const std::vector> & dividers, int indent = 2, int spacing = 2, const char * missing = "-", int max = 80); jpayne@69: jpayne@69: } // namespace mash jpayne@69: jpayne@69: #endif