jpayne@69
|
1 // Copyright © 2015, Battelle National Biodefense Institute (BNBI);
|
jpayne@69
|
2 // all rights reserved. Authored by: Brian Ondov, Todd Treangen,
|
jpayne@69
|
3 // Sergey Koren, and Adam Phillippy
|
jpayne@69
|
4 //
|
jpayne@69
|
5 // See the LICENSE.txt file included with this software for license information.
|
jpayne@69
|
6
|
jpayne@69
|
7 #ifndef INCLUDED_Command
|
jpayne@69
|
8 #define INCLUDED_Command
|
jpayne@69
|
9
|
jpayne@69
|
10 #include <map>
|
jpayne@69
|
11 #include <string>
|
jpayne@69
|
12 #include <vector>
|
jpayne@69
|
13 #include <set>
|
jpayne@69
|
14
|
jpayne@69
|
15 namespace mash {
|
jpayne@69
|
16
|
jpayne@69
|
17 class Command
|
jpayne@69
|
18 {
|
jpayne@69
|
19
|
jpayne@69
|
20 // A base class for handling command line parsing and usage messages. Specific
|
jpayne@69
|
21 // commands can be implemented by subclassing and overriding the run() method.
|
jpayne@69
|
22 // The run() method will be called by this base class from run(argc, argv) once
|
jpayne@69
|
23 // it has parsed options, which can be accessed from the 'options' map using the
|
jpayne@69
|
24 // name strings given when adding them (not 'identifier', which is its parsing
|
jpayne@69
|
25 // name to appear after the '-'). Arguments (i.e. operands given after the
|
jpayne@69
|
26 // options on the command line) will be available in the 'arguments' vector.
|
jpayne@69
|
27
|
jpayne@69
|
28 public:
|
jpayne@69
|
29
|
jpayne@69
|
30 class Option
|
jpayne@69
|
31 {
|
jpayne@69
|
32 public:
|
jpayne@69
|
33
|
jpayne@69
|
34 enum Type
|
jpayne@69
|
35 {
|
jpayne@69
|
36 Boolean,
|
jpayne@69
|
37 Number,
|
jpayne@69
|
38 Integer,
|
jpayne@69
|
39 Size,
|
jpayne@69
|
40 File,
|
jpayne@69
|
41 String
|
jpayne@69
|
42 }
|
jpayne@69
|
43 type;
|
jpayne@69
|
44
|
jpayne@69
|
45 std::string category;
|
jpayne@69
|
46 std::string identifier;
|
jpayne@69
|
47 std::string description;
|
jpayne@69
|
48 std::string argument;
|
jpayne@69
|
49 std::string argumentDefault;
|
jpayne@69
|
50
|
jpayne@69
|
51 float argumentAsNumber;
|
jpayne@69
|
52 float argumentMin;
|
jpayne@69
|
53 float argumentMax;
|
jpayne@69
|
54
|
jpayne@69
|
55 bool active;
|
jpayne@69
|
56 bool changed;
|
jpayne@69
|
57
|
jpayne@69
|
58 Option() {}
|
jpayne@69
|
59 Option(Type typeNew, std::string identifierNew, std::string categoryNew, std::string descriptionNew, std::string argumentDefaultNew = "", float argumentMinNew = 0, float argumentMaxNew = 0);
|
jpayne@69
|
60
|
jpayne@69
|
61 float getArgumentAsNumber() const {return argumentAsNumber;}
|
jpayne@69
|
62 void setArgument(std::string argumentNew);
|
jpayne@69
|
63 };
|
jpayne@69
|
64
|
jpayne@69
|
65 Command();
|
jpayne@69
|
66 virtual ~Command() {};
|
jpayne@69
|
67
|
jpayne@69
|
68 void addOption(std::string name, Option option);
|
jpayne@69
|
69 const Option & getOption(std::string name) const;
|
jpayne@69
|
70 inline bool hasOption(std::string name) const {return options.count(name);}
|
jpayne@69
|
71 void print() const;
|
jpayne@69
|
72 virtual int run() const = 0;
|
jpayne@69
|
73 int run(int argc, const char ** argv);
|
jpayne@69
|
74
|
jpayne@69
|
75 std::string name;
|
jpayne@69
|
76 std::string summary;
|
jpayne@69
|
77 std::string description;
|
jpayne@69
|
78 std::string argumentString;
|
jpayne@69
|
79
|
jpayne@69
|
80 protected:
|
jpayne@69
|
81
|
jpayne@69
|
82 void useOption(std::string name);
|
jpayne@69
|
83 void useSketchOptions();
|
jpayne@69
|
84
|
jpayne@69
|
85 std::map<std::string, Option> options;
|
jpayne@69
|
86 std::map<std::string, Option> optionsAvailable;
|
jpayne@69
|
87 std::vector<std::string> arguments;
|
jpayne@69
|
88
|
jpayne@69
|
89 private:
|
jpayne@69
|
90
|
jpayne@69
|
91 void addAvailableOption(std::string name, Option option);
|
jpayne@69
|
92 void addCategory(std::string name, std::string displayName);
|
jpayne@69
|
93
|
jpayne@69
|
94 std::map<std::string, std::string> optionNamesByIdentifier;
|
jpayne@69
|
95 std::map<std::string, std::vector<std::string> > optionNamesByCategory;
|
jpayne@69
|
96 std::vector<std::string> categories;
|
jpayne@69
|
97 std::map<std::string, std::string> categoryDisplayNames;
|
jpayne@69
|
98 };
|
jpayne@69
|
99
|
jpayne@69
|
100 inline const Command::Option & Command::getOption(std::string name) const {return options.at(name);}
|
jpayne@69
|
101 void splitFile(const std::string & file, std::vector<std::string> & lines);
|
jpayne@69
|
102 void printColumns(const std::vector<std::vector<std::string>> & columns, int indent = 2, int spacing = 2, const char * missing = "-", int max = 80);
|
jpayne@69
|
103 void printColumns(const std::vector<std::vector<std::string>> & columns, const std::vector<std::pair<int, std::string>> & dividers, int indent = 2, int spacing = 2, const char * missing = "-", int max = 80);
|
jpayne@69
|
104
|
jpayne@69
|
105 } // namespace mash
|
jpayne@69
|
106
|
jpayne@69
|
107 #endif
|