arguments

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Beide Seiten der vorigen Revision Vorhergehende Überarbeitung
Nächste Überarbeitung
Vorhergehende Überarbeitung
arguments [04.01.2006 14:21 (vor 19 Jahren)] cwachaarguments [16.11.2016 23:18 (vor 8 Jahren)] (aktuell) – Externe Bearbeitung 127.0.0.1
Zeile 3: Zeile 3:
 === Overview === === Overview ===
 //Arguments// is a very simple and intuitive command line arguments parser written in C++ that takes (argc, argv) as arguments and then lets you easily test for given command line options. Have a look at the example code to see how easy it is. //Arguments// is a very simple and intuitive command line arguments parser written in C++ that takes (argc, argv) as arguments and then lets you easily test for given command line options. Have a look at the example code to see how easy it is.
 +
 +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:arguments-1.2.zip}}
 +  * {{projects:arguments-1.1.zip}}
   * {{projects:arguments-1.0.tar.gz}}   * {{projects:arguments-1.0.tar.gz}}
  
Zeile 35: Zeile 50:
  // !! argument list !!  // !! argument list !!
   
- // Format: getXXX(option to search, default value, long option to search); + // Format: get(option to search, default value, long option to search); 
- //  the latter two are optional + //  long_option is optional 
- int my_int = a.getInt("-a"); + int my_int = a.get("-a", 0); 
- int my_int2 = a.getInt("-a", "340", "--with-long-option"); + double my_int2 = a.get<double>("-a", 340, "--with-long-option"); 
- int show_help = a.getFlag("-h", "--help");+ string test = a.get<string>("-test", "not here"); 
 + bool show_help = a.getFlag("-h", "--help");
   
- // if you are interested in a supplied filename you can use front()+ // 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 << "a=" << my_int << " other a=" << my_int2 << endl; + cout << "a=" << my_int << " other a=" << my_int2 << " test=" << test << endl; 
- +
  return 0;  return 0;
 } }
 +
 </code> </code>
  
Zeile 73: Zeile 90:
  Arguments();  Arguments();
  Arguments(int argc, char* argv[]);  Arguments(int argc, char* argv[]);
- Arguments(std::vector<std::string>);+ Arguments(const std::vector<std::string>&);
  ~Arguments();  ~Arguments();
   
- // returns if flag is found, else + // returns true if flag is found, false else 
- int getFlag(const std::string option, const std::string long_option = ""); + bool getFlag(const std::stringoption, const std::stringlong_option = ""); 
- // returns integer if found, supplied default_value else + // returns type T if found or default_value (converted to type T) else 
- int getInt(const std::string option, const std::string default_value = "", const std::string long_option = "")+ // Remarkdoes NOT work with char*. Use string instead and c_str() to get a char* 
- // returns long if found, supplied default_value else + template<typename T> T get(const std::stringoption, const T& default_value, const std::stringlong_option = "");
- long getLong(const std::string option, const std::string default_value = "", const std::string long_option = ""); +
- // returns float if found, supplied default_value else +
- float getFloat(const std::string option, const std::string default_value = "", const std::string long_option = ""); +
- // returns double if found, supplied default_value else +
- double getDouble(const std::string option, const std::string default_value = "", const std::string long_option = ""); +
- // returns string if found, supplied default_value else +
- std::string getString(const std::string option, const std::string default_value = "", const std::string long_option = "");+
  
  // return first element  // return first element
Zeile 100: Zeile 110:
 </code> </code>
  
 +
 +=== License ===
 +
 +<html>
 +
 +<!-- Creative Commons License -->
 +<a href="http://creativecommons.org/licenses/GPL/2.0/">
 +<img alt="CC-GNU GPL" border="0" src="http://creativecommons.org/images
 +/public/cc-GPL-a.png" /></a><br />
 +This software is licensed under the <a href="http://creativecommons.org/licenses/GPL/2.0/">CC-GNU GPL</a>.
 +<!-- /Creative Commons License -->
 +
 +<!--
 +
 +<rdf:RDF xmlns="http://web.resource.org/cc/"
 +    xmlns:dc="http://purl.org/dc/elements/1.1/"
 +    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
 +<Work rdf:about="">
 +   <license rdf:resource="http://creativecommons.org/licenses/GPL/2.0/" />
 +   <dc:type rdf:resource="http://purl.org/dc/dcmitype/Software" />
 +</Work>
 +
 +<License rdf:about="http://creativecommons.org/licenses/GPL/2.0/">
 +<permits rdf:resource="http://web.resource.org/cc/Reproduction" />
 +   <permits rdf:resource="http://web.resource.org/cc/Distribution" />
 +   <requires rdf:resource="http://web.resource.org/cc/Notice" />
 +   <permits rdf:resource="http://web.resource.org/cc/DerivativeWorks" />
 +   <requires rdf:resource="http://web.resource.org/cc/ShareAlike" />
 +   <requires rdf:resource="http://web.resource.org/cc/SourceCode" />
 +</License>
 +
 +</rdf:RDF>
 +
 +-->
 +
 +</html>
  
  • arguments.1136380906.txt.gz
  • Zuletzt geändert: 16.11.2016 23:16 (vor 8 Jahren)
  • (Externe Bearbeitung)