| [c6efc1] | 1 | /* | 
|---|
|  | 2 | * CommandLineParser.hpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: May 8, 2010 | 
|---|
|  | 5 | *      Author: heber | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | #ifndef COMMANDLINEPARSER_HPP_ | 
|---|
|  | 9 | #define COMMANDLINEPARSER_HPP_ | 
|---|
|  | 10 |  | 
|---|
|  | 11 | #include <boost/program_options.hpp> | 
|---|
|  | 12 |  | 
|---|
|  | 13 | namespace po = boost::program_options; | 
|---|
|  | 14 |  | 
|---|
|  | 15 | #include "Patterns/Singleton.hpp" | 
|---|
| [e4afb4] | 16 | #include "UIElements/CommandLineUI/TypeEnumContainer.hpp" | 
|---|
| [c6efc1] | 17 |  | 
|---|
| [e4afb4] | 18 | #include <map> | 
|---|
|  | 19 |  | 
|---|
|  | 20 | class Action; | 
|---|
|  | 21 | class OptionTrait; | 
|---|
| [c6efc1] | 22 |  | 
|---|
| [de8e45] | 23 | /** This class is a wrapper for boost::program_options. | 
|---|
|  | 24 | * | 
|---|
|  | 25 | * <h1> CommandLine Howto </h1> | 
|---|
|  | 26 | * <h2> Introduction </h2> | 
|---|
|  | 27 | * | 
|---|
|  | 28 | * The UIFactory is a base class for the User Interaction. There are three UI specializations: | 
|---|
|  | 29 | * Text, GUI and CommandLine. Accessing functionality via the CommandLine UI is explained here. | 
|---|
|  | 30 | * | 
|---|
|  | 31 | * First, an Action has to be written for the specific functionality. This Action should | 
|---|
|  | 32 | * be added in Actions/...Action in the respective subdirectory of the following types: | 
|---|
|  | 33 | *  -# Analysis: Analysis actions like evaluating pair correlation, bonds, ... | 
|---|
|  | 34 | *  -# Atom: adding, removing, manipulating atoms | 
|---|
|  | 35 | *  -# Cmd: specifying data bases, verbosity, ... | 
|---|
|  | 36 | *  -# Fragmentation: fragmenting a system, performing graph analysis, ... | 
|---|
|  | 37 | *  -# Molecule: adding, removing, manipulating molecules | 
|---|
|  | 38 | *  -# Parser: Parsing files (loading, saving) | 
|---|
|  | 39 | *  -# Tesselation: obtaining (non)convex surface of a molecule, embedding, ... | 
|---|
|  | 40 | *  -# World: Setting Box dimensions, default name of new molecules, ... | 
|---|
|  | 41 | * | 
|---|
|  | 42 | *  The CommandLineUIFactory is a specialization of the UIFactory for parsing command | 
|---|
|  | 43 | *  line parameters, generating and executing actions there from. | 
|---|
|  | 44 | * | 
|---|
|  | 45 | *  The idea of the CommandLineFactory is explained elsewhere, here we would like to give a | 
|---|
|  | 46 | *  receipe for creating new actions. | 
|---|
|  | 47 | * | 
|---|
|  | 48 | * <h3>Introducing new actions</h3> | 
|---|
|  | 49 | * | 
|---|
|  | 50 | * Let us now introduce what to do if a new action is to be implemented. Here, we use the | 
|---|
|  | 51 | * CommandLineVersionAction as an example. | 
|---|
|  | 52 | * This consists if basically three parts: | 
|---|
|  | 53 | * 1. Create the files, write the classes and make them compilable | 
|---|
|  | 54 | *   - Create new source and header files in one of the above subfolders in the Actions folder, | 
|---|
|  | 55 | *     e.g. create VersionAction.cpp and VersionAction.hpp in Actions/Cmd/ | 
|---|
|  | 56 | *   - Give it a sensible class name, the convention is <type><what it does>Action, | 
|---|
|  | 57 | *     where <type> is basically the naming (written out) of the subdirectory, | 
|---|
|  | 58 | *     e.g. class CommandLineVersionAction. | 
|---|
|  | 59 | *   - Add the source and header file to the respective variables in molecuilder/src/Makefile.am, | 
|---|
|  | 60 | *     e.g. if you add a Cmd action the variables are CMDACTIONSOURCE and CMDACTIONHEADER, | 
|---|
|  | 61 | *     such that they get compiled. | 
|---|
|  | 62 | * 2. Add an instance to the CommandLineUIFactory, such that they are known to the UI. | 
|---|
|  | 63 | *   - Add the header file as an include to UIElements/CommandLineWindow.cpp, e.g. | 
|---|
|  | 64 | *     #include "Actions/Cmd/VersionAction.hpp" | 
|---|
|  | 65 | *   - Add an instance of your class to the specific populater-function in | 
|---|
| [53d01c] | 66 | *     UIElements/CommandLineWindow.cpp, e.g. for the above Cmd action, add to populateCommandActions() | 
|---|
| [de8e45] | 67 | *     add new CommandLineVersionAction(). | 
|---|
|  | 68 | *     This will automatically register in the ActionRegistry. | 
|---|
|  | 69 | * 3. Give them an option name, short hand an description, such that they can be referenced from | 
|---|
|  | 70 | *    the command line. | 
|---|
|  | 71 | *   - think of a new key name, e.g. "version", which is the long form of the command parameter, | 
|---|
|  | 72 | *     i.e. --version). | 
|---|
|  | 73 | *   - add this key to every map of MapofActions, i.e. to | 
|---|
|  | 74 | *     - MapofActions::DescriptionMap: the description which appears as help and tooltip | 
|---|
|  | 75 | *     - MapofActions::ShortFormMap: the short form of the command parameter (e.g. -v) | 
|---|
|  | 76 | *     - MapofActions::ValueMap: the value the command parameter has (do not create if it does not need one) | 
|---|
|  | 77 | *   - If your action requires additional parameters, these need to be added in the same manner as in | 
|---|
|  | 78 | *     the list item above. | 
|---|
|  | 79 | * | 
|---|
|  | 80 | *  Don't forget to write the actual code. :) | 
|---|
|  | 81 | * | 
|---|
|  | 82 | * <h3>Writing an action</h3> | 
|---|
|  | 83 | * | 
|---|
|  | 84 | *  As you write a new action you may think in terms of the command line, i.e. you want to use this | 
|---|
|  | 85 | *  new functionality you add by calling molecuilder as: ./molecuilder --super-action foobar.txt, where | 
|---|
|  | 86 | *  the key of your new action would be "super-action". While this is fine, keep in mind, that your action | 
|---|
|  | 87 | *  should be useable for the other UI specializations as well, i.e. from the menu and the GUI. Therefore, | 
|---|
|  | 88 | *  -# Don't use cin to ask the user for input: Use Query...()! | 
|---|
|  | 89 | *  -# Rather don't use cout/cerrs, but either give Log() or eLog() or use QueryEmpty() if you want to give | 
|---|
|  | 90 | *     the user specific information what you ask of him. | 
|---|
|  | 91 | * | 
|---|
|  | 92 | */ | 
|---|
| [bcd072] | 93 | class CommandLineParser : public Singleton<CommandLineParser> { | 
|---|
|  | 94 | friend class Singleton<CommandLineParser>; | 
|---|
| [c6efc1] | 95 | public: | 
|---|
|  | 96 |  | 
|---|
|  | 97 | // Parses the command line arguments in CommandLineParser::**argv with currently known options. | 
|---|
| [e4afb4] | 98 | void Run(int _argc, char **_argv); | 
|---|
|  | 99 |  | 
|---|
|  | 100 | // Initialises all options from ActionRegistry. | 
|---|
|  | 101 | void InitializeCommandArguments(); | 
|---|
| [c6efc1] | 102 |  | 
|---|
|  | 103 | // Checks whether there have been any commands on the command line. | 
|---|
|  | 104 | bool isEmpty(); | 
|---|
|  | 105 |  | 
|---|
|  | 106 | /* boost's program_options are sorted into three categories: | 
|---|
|  | 107 | * -# generic options: option available to both command line and config | 
|---|
|  | 108 | * -# config options: only available in the config file | 
|---|
|  | 109 | * -# hidden options: options which the user is not shown on "help" | 
|---|
|  | 110 | */ | 
|---|
| [e4afb4] | 111 | po::options_description analysis; | 
|---|
|  | 112 | po::options_description atom; | 
|---|
|  | 113 | po::options_description command; | 
|---|
|  | 114 | po::options_description edit; | 
|---|
|  | 115 | po::options_description fragmentation; | 
|---|
|  | 116 | po::options_description molecule; | 
|---|
|  | 117 | po::options_description parser; | 
|---|
|  | 118 | po::options_description selection; | 
|---|
|  | 119 | po::options_description tesselation; | 
|---|
|  | 120 | po::options_description world; | 
|---|
|  | 121 | po::options_description options; | 
|---|
| [c6efc1] | 122 |  | 
|---|
| [e3ecc1a] | 123 | po::options_description visible; | 
|---|
|  | 124 |  | 
|---|
| [bcd072] | 125 | po::variables_map vm; | 
|---|
| [c6efc1] | 126 |  | 
|---|
| [7e6b00] | 127 | // private sequence of actions as they appeared on the command line | 
|---|
|  | 128 | std::list<std::string> SequenceOfActions; | 
|---|
|  | 129 |  | 
|---|
| [c6efc1] | 130 | private: | 
|---|
| [bcd072] | 131 | // private constructor and destructor | 
|---|
|  | 132 | CommandLineParser(); | 
|---|
|  | 133 | virtual ~CommandLineParser(); | 
|---|
|  | 134 |  | 
|---|
| [c6efc1] | 135 | /* The following program_options options_decriptions are used to | 
|---|
|  | 136 | * generate the various cases and call differently in Parse(). | 
|---|
|  | 137 | */ | 
|---|
|  | 138 | po::options_description cmdline_options; | 
|---|
|  | 139 | po::options_description config_file_options; | 
|---|
|  | 140 |  | 
|---|
| [e4afb4] | 141 | // adds options to the parser | 
|---|
|  | 142 | void AddOptionToParser(const OptionTrait * const currentOption, po::options_description* OptionList); | 
|---|
|  | 143 |  | 
|---|
|  | 144 | // creates a map from short forms to action tokens needed to parse command line | 
|---|
|  | 145 | std::map <std::string, std::string> getShortFormToActionMap(); | 
|---|
|  | 146 |  | 
|---|
|  | 147 | // lookup list from "configmenus" to the ones of CommandLineParser | 
|---|
|  | 148 | std::map< std::string , po::options_description *> CmdParserLookup; | 
|---|
|  | 149 |  | 
|---|
| [c6efc1] | 150 | // Sets the options from the three cases. | 
|---|
|  | 151 | void setOptions(int _argc, char **_argv); | 
|---|
|  | 152 |  | 
|---|
|  | 153 | // Parses all options from command line and config file | 
|---|
|  | 154 | void Parse(); | 
|---|
|  | 155 |  | 
|---|
| [7e6b00] | 156 | // as boost's program_options does not care about of order of appearance but we do for actions, | 
|---|
|  | 157 | // we have to have a list and a function to obtain it. | 
|---|
| [e4afb4] | 158 | void scanforSequenceOfArguments(); | 
|---|
|  | 159 |  | 
|---|
|  | 160 | TypeEnumContainer TypeToEnums; | 
|---|
| [7e6b00] | 161 |  | 
|---|
| [c6efc1] | 162 | // argument counter and array passed on from main() | 
|---|
|  | 163 | int argc; | 
|---|
|  | 164 | char **argv; | 
|---|
|  | 165 | }; | 
|---|
|  | 166 |  | 
|---|
|  | 167 | #endif /* COMMANDLINEPARSER_HPP_ */ | 
|---|