Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen Revision Vorhergehende Überarbeitung | |||
arguments [06.01.2006 16:03 (vor 19 Jahren)] – cwacha | arguments [16.11.2016 23:18 (vor 8 Jahren)] (aktuell) – Externe Bearbeitung 127.0.0.1 | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
+ | ===== Commandline Argument Parser ===== | ||
+ | |||
+ | === Overview === | ||
+ | // | ||
+ | |||
+ | This class makes handling argc, argv (as supplied by main() i.e.) very easy. After initializing you provide it with an option you would like to have and an optional default value. If the option is found inside argv it will be returned to you, if not you get the default value. Reading an option from the argument list removes it from the argument list. It works like a stack with a find-and-pop function. | ||
+ | |||
+ | Instead of using argc, argv you can also supply a vector of strings. This comes in handy if you have a line that was splitted into tokens by [[split_line]] for example. | ||
+ | |||
+ | |||
+ | === Features === | ||
+ | * short options | ||
+ | * long options | ||
+ | * flags | ||
+ | * internal storage realised as STL vector | ||
+ | * %%-flag --long-option-flag%% | ||
+ | * %%-option 5 --long-option-text 5%% | ||
+ | |||
+ | == Remarks == | ||
+ | * you cannot unset a flag with something like -flag 0. You will have to test for another flag i.e. -notflag. | ||
+ | * -option=5 will not be recognized. There is no internal splitter. | ||
+ | * every option takes exactly 1 argument. | ||
+ | |||
+ | |||
+ | === Download === | ||
+ | * {{projects: | ||
+ | * {{projects: | ||
+ | * {{projects: | ||
+ | |||
+ | === Code Example === | ||
+ | |||
+ | <code cpp> | ||
+ | #include " | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | int main(int argc, char* argv[]) { | ||
+ | |||
+ | // First, we need to initalize a new Arguments object | ||
+ | Arguments a(argc, argv); | ||
+ | |||
+ | // now you just provide the options you would like to have | ||
+ | string this_binary = a.front(); | ||
+ | |||
+ | // !! remark that you can provide an option MORE THAN ONE time and you | ||
+ | // !! will get subsequent options. A found option will be removed from the | ||
+ | // !! argument list !! | ||
+ | |||
+ | // Format: get(option to search, default value, long option to search); | ||
+ | // long_option is optional | ||
+ | int my_int = a.get(" | ||
+ | double my_int2 = a.get< | ||
+ | string test = a.get< | ||
+ | bool show_help = a.getFlag(" | ||
+ | |||
+ | // if you are interested a supplied filename you can use front() | ||
+ | // or back() to pop the first or last element | ||
+ | string filename = a.back(); | ||
+ | |||
+ | // now we check if we have popped all options of if there are some left (i.e. the user entered an option | ||
+ | // that does not exist) | ||
+ | if( a.size() > 0 ) { | ||
+ | cout << " | ||
+ | } | ||
+ | |||
+ | if( show_help || a.size() > 0 || filename == "" | ||
+ | cout << " | ||
+ | } | ||
+ | |||
+ | //now do your stuff | ||
+ | |||
+ | cout << " | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | === Documentation === | ||
+ | |||
+ | Only relevant members are shown! | ||
+ | |||
+ | <code cpp> | ||
+ | class Arguments { | ||
+ | |||
+ | public: | ||
+ | Arguments(); | ||
+ | Arguments(int argc, char* argv[]); | ||
+ | Arguments(const std:: | ||
+ | ~Arguments(); | ||
+ | |||
+ | // returns true if flag is found, false else | ||
+ | bool getFlag(const std:: | ||
+ | // returns type T if found or default_value (converted to type T) else | ||
+ | // Remark: does NOT work with char*. Use string instead and c_str() to get a char* | ||
+ | template< | ||
+ | |||
+ | // return first element | ||
+ | std:: | ||
+ | // return last element | ||
+ | std:: | ||
+ | // return number of elements currently in the argument list | ||
+ | unsigned int size(); | ||
+ | // return comma separated string containing all elements in the argument list | ||
+ | std:: | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | |||
+ | === License === | ||
+ | |||
+ | < | ||
+ | |||
+ | <!-- Creative Commons License --> | ||
+ | <a href=" | ||
+ | <img alt=" | ||
+ | / | ||
+ | This software is licensed under the <a href=" | ||
+ | <!-- /Creative Commons License --> | ||
+ | |||
+ | <!-- | ||
+ | |||
+ | <rdf:RDF xmlns=" | ||
+ | xmlns: | ||
+ | xmlns: | ||
+ | <Work rdf: | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | |||
+ | <License rdf: | ||
+ | <permits rdf: | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | |||
+ | --> | ||
+ | |||
+ | </ | ||