Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
arguments [04.01.2006 14:21 (vor 19 Jahren)] – cwacha | arguments [16.11.2016 23:18 (vor 8 Jahren)] (aktuell) – Externe Bearbeitung 127.0.0.1 | ||
---|---|---|---|
Zeile 3: | Zeile 3: | ||
=== Overview === | === 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 === | === Features === | ||
Zeile 9: | Zeile 14: | ||
* flags | * flags | ||
* internal storage realised as STL vector | * 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 === | === Download === | ||
+ | * {{projects: | ||
+ | * {{projects: | ||
* {{projects: | * {{projects: | ||
Zeile 35: | Zeile 50: | ||
// !! argument list !! | // !! argument list !! | ||
- | // Format: | + | // Format: |
- | // | + | // |
- | int my_int = a.getInt(" | + | int my_int = a.get(" |
- | int my_int2 = a.getInt(" | + | double |
- | int show_help = a.getFlag(" | + | string test = a.get< |
+ | bool show_help = a.getFlag(" | ||
- | // if you are interested | + | // if you are interested a supplied filename you can use front() |
// or back() to pop the first or last element | // or back() to pop the first or last element | ||
string filename = a.back(); | string filename = a.back(); | ||
Zeile 57: | Zeile 73: | ||
//now do your stuff | //now do your stuff | ||
- | cout << " | + | cout << " |
- | + | ||
return 0; | return 0; | ||
} | } | ||
+ | |||
</ | </ | ||
Zeile 73: | Zeile 90: | ||
Arguments(); | Arguments(); | ||
Arguments(int argc, char* argv[]); | Arguments(int argc, char* argv[]); | ||
- | Arguments(std:: | + | Arguments(const std:: |
~Arguments(); | ~Arguments(); | ||
- | // returns | + | // returns |
- | int getFlag(const std::string option, const std::string long_option = "" | + | bool getFlag(const std::string& option, const std::string& long_option = "" |
- | // returns | + | // returns |
- | int getInt(const std::string option, const std::string default_value = "", | + | // Remark: does NOT work with char*. Use string |
- | // returns long if found, supplied default_value | + | template< |
- | long getLong(const std::string option, const std::string default_value = "", | + | |
- | // returns float if found, supplied default_value else | + | |
- | float getFloat(const std:: | + | |
- | // returns double if found, supplied default_value else | + | |
- | double getDouble(const std::string option, const std::string default_value = "", | + | |
- | // returns string if found, supplied default_value else | + | |
- | std:: | + | |
// return first element | // return first element | ||
Zeile 100: | Zeile 110: | ||
</ | </ | ||
+ | |||
+ | === 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: | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | |||
+ | --> | ||
+ | |||
+ | </ | ||