<commit_before>#ifndef flags_hpp_
#define flags_hpp_

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <stdexcept>
#include <string>

class Flags
{
public:
	Flags(std::string name, std::string version)
	: name(name), version(version)
	{
	}

	void add_option(std::string, std::string, std::string, std::function<void(std::string, std::string, void*)>);
	void add_option(std::string, std::string, std::string, std::function<void(std::string, std::string, void*)>, void*);
	void parse(int argc, char* argv[]);
	void print_usage();

private:
	std::string name;
	std::string version;

	std::map<std::string, std::function<void(std::string, std::string, void*)>> funcs;
	std::map<std::string, void*> ptrs;
	std::map<std::pair<std::string, std::string>, std::string> descs;
};

inline void Flags :: add_option(std::string long_opt,
	std::string opt, std::string desc,
	std::function<void(std::string, std::string, void*)> cb) {
	add_option(long_opt, opt, desc, cb, nullptr);
}

inline void Flags :: add_option(std::string long_opt,
	std::string opt, std::string desc,
	std::function<void(std::string, std::string, void*)> cb,
	void* p) {

	if (funcs.find(long_opt) != funcs.end()) {
		throw std::invalid_argument("redefined flag: " + long_opt);
	}

	if (funcs.find(opt) != funcs.end()) {
		throw std::invalid_argument("redefined flag: " + opt);
	}

	funcs[long_opt] = cb;
	ptrs[long_opt] = p;

	if (opt != "") {
		funcs[opt] = cb;
		ptrs[opt] = p;
	}

	descs[std::pair<std::string, std::string>(opt, long_opt)] = desc;
}

inline void Flags :: print_usage() {
	std::cout << name << " version " << version << " usage:" << std::endl << std::endl;
	for (const auto &i: descs) {
		std::string opts = "";

		if (i.first.first != "") {
			opts = i.first.first + ", " + i.first.second;
		} else {
			opts = "    " + i.first.second;
		}

		printf("  %-25s %s\n", opts.c_str(), i.second.c_str());
	}
}

inline void Flags :: parse(int argc, char* argv[]) {
	auto is_first = true;
	std::string arg;

	for (int i = 0; i < argc; i++) {
		if (!is_first) {
			auto val = funcs.find(arg);
			if (argv[i][0] == '-') {
				// Start of a new flag.
				val->second(arg, "", ptrs.find(arg)->second);
				arg = "";
				break;
			}

			if (val != funcs.end()) {
				// Call callback
				val->second(arg, argv[i], ptrs.find(arg)->second);
			}

			is_first = true;
			arg = "";

			continue;
		}

		is_first = true;
		arg = "";

		auto val = funcs.find(argv[i]);
		if (val != funcs.end()) {
			if (is_first) {
				arg = val->first;
				is_first = false;
				continue;
			}
		} else {
			// Check for an equals.
			auto argv_str = std::string(argv[i]);
			std::size_t pos = argv_str.find("=");
			if (pos != std::string::npos) {
				// Found an '='.
				auto arg = argv_str.substr(0, pos);
				auto val = argv_str.substr(pos+1);

				auto cb = funcs.find(arg.c_str());
				if (cb != funcs.end()) {
					cb->second(arg.c_str(), val, ptrs.find(arg.c_str())->second);
				}
			}
		}
	}

	if (!is_first) {
		auto val = funcs.find(arg);
		if (val != funcs.end()) {
			val->second(arg, "", ptrs.find(arg)->second);
		}
	}
}

#endif
<commit_msg>clean up<commit_after>// This header file provides a command line flag handling
// utility class.
#ifndef flags_hpp_
#define flags_hpp_

#include <functional> // std::function
#include <iostream> // std::cout
#include <map> // std::map
#include <stdexcept> // exceptions
#include <string> // std::string

class Flags
{
public:
	Flags(std::string name, std::string version)
	: name(name), version(version)
	{
	}

	void add_option(std::string, std::string, std::string,
		std::function<void(std::string, std::string, void*)>);

	void add_option(std::string, std::string, std::string,
		std::function<void(std::string, std::string, void*)>, void*);

	// parses arguments
	void parse(int argc, char* argv[]) throw();

	// Prints usage string message to standard out.
	void print_usage() throw();

private:
	// program name
	std::string name;

	// program version
	std::string version;

	// map from flag to callback
	std::map<std::string, std::function<void(std::string, std::string, void*)>> funcs;

	// map from flag to data pointer
	std::map<std::string, void*> ptrs;

	// map from flag to description string
	std::map<std::pair<std::string, std::string>, std::string> descs;
};

inline void Flags :: add_option(std::string long_opt,
	std::string opt, std::string desc,
	std::function<void(std::string, std::string, void*)> cb) {

	// no data pointer
	add_option(long_opt, opt, desc, cb, nullptr);
}

inline void Flags :: add_option(std::string long_opt,
	std::string opt, std::string desc,
	std::function<void(std::string, std::string, void*)> cb,
	void* p) {

	if (opt.length() > 2) {
		// length includes the hyphen
		throw std::invalid_argument("short option longer than one character: " + opt);
	}

	if (funcs.find(long_opt) != funcs.end()) {
		throw std::invalid_argument("redefined flag: " + long_opt);
	}

	if (funcs.find(opt) != funcs.end()) {
		throw std::invalid_argument("redefined flag: " + opt);
	}

	funcs[long_opt] = cb;
	ptrs[long_opt] = p;

	if (opt != "") {
		funcs[opt] = cb;
		ptrs[opt] = p;
	}

	descs[std::pair<std::string, std::string>(opt, long_opt)] = desc;
}

inline void Flags :: print_usage() throw() {
	std::cout << name << " version " << version << " usage:" << std::endl << std::endl;
	for (const auto &i: descs) {
		// short and long options combined
		std::string opts = "";

		if (i.first.first != "") {
			opts = i.first.first + ", " + i.first.second;
		} else {
			opts = "    " + i.first.second;
		}

		printf("  %-25s %s\n", opts.c_str(), i.second.c_str());
	}
}

inline void Flags :: parse(int argc, char* argv[]) throw() {
	auto is_first = true;
	std::string arg;

	for (int i = 0; i < argc; i++) {
		if (!is_first) {
			auto cb = funcs.find(arg);
			if (argv[i][0] == '-') {
				// Start of a new flag.
				cb->second(arg, "", ptrs.find(arg)->second);
				arg = "";
				break;
			}

			if (cb != funcs.end()) {
				// Call callback
				cb->second(arg, argv[i], ptrs.find(arg)->second);
			}

			is_first = true;
			arg = "";

			continue;
		}

		is_first = true;
		arg = "";

		auto cb = funcs.find(argv[i]);
		if (cb != funcs.end()) {
			if (is_first) {
				arg = cb->first;
				is_first = false;
				continue;
			}
		} else {
			// Check for an equals.
			auto argv_str = std::string(argv[i]);
			std::size_t pos = argv_str.find("=");
			if (pos != std::string::npos) {
				// Found an '='.
				auto arg = argv_str.substr(0, pos);
				auto val = argv_str.substr(pos+1);

				auto cb = funcs.find(arg.c_str());
				if (cb != funcs.end()) {
					cb->second(arg.c_str(), val, ptrs.find(arg.c_str())->second);
				}
			}
		}
	}

	if (!is_first) {
		auto cb = funcs.find(arg);
		if (cb != funcs.end()) {
			cb->second(arg, "", ptrs.find(arg)->second);
		}
	}
}

#endif

{'fraction_non_alphanumeric': 0.19933022714036108, 'fraction_numerical': 0.0018928363424577752, 'mean_word_length': 3.6100671140939595, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7333449', 'n_tokens_mistral': 2844, 'n_tokens_neox': 2576, 'n_words': 599}