arguments

Dies ist eine alte Version des Dokuments!


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.

Features

  • short options
  • long options
  • flags
  • internal storage realised as STL vector

Download

Code example

#include "Arguments.h"
 
#include <string>
#include <iostream>
 
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: getXXX(option to search, default value, long option to search);
	//  the latter two are optional
	int my_int = a.getInt("-a");
	int my_int2 = a.getInt("-a", "340", "--with-long-option");
	int show_help = a.getFlag("-h", "--help");
 
	// if you are interested in 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 << "Unsupported option(s) given: " << a.list() << endl;
	}
 
	if( show_help || a.size() > 0 || filename == "") {
		cout << "usage: " << this_binary << " -a -h filename" << endl;
	}
 
	//now do your stuff
 
	cout << "a=" << my_int << " other a=" << my_int2 << endl;
 
	return 0;
}

Documentation

Only relevant members are shown!

class Arguments {
 
public:
	Arguments();
	Arguments(int argc, char* argv[]);
	Arguments(std::vector<std::string>);
	~Arguments();
 
	// returns 1 if flag is found, 0 else
	int getFlag(const std::string option, const std::string long_option = "");
	// returns integer if found, supplied default_value else
	int getInt(const std::string option, const std::string default_value = "", const std::string long_option = "");
	// returns long if found, supplied default_value else
	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
	std::string front();
	// return last element
	std::string back();
	// return number of elements currently in the argument list
	unsigned int size();
	// return comma separated string containing all elements in the argument list
	std::string list();
};
  • arguments.1136380645.txt.gz
  • Zuletzt geändert: 16.11.2016 23:16 (vor 8 Jahren)
  • (Externe Bearbeitung)