Skip to content

Instantly share code, notes, and snippets.

@DataKinds
Created April 8, 2018 02:16
Show Gist options
  • Save DataKinds/0f426b6e3a4294c9fd0a39c2e062dda9 to your computer and use it in GitHub Desktop.
Save DataKinds/0f426b6e3a4294c9fd0a39c2e062dda9 to your computer and use it in GitHub Desktop.
template<std::vector<std::string>* arg, std::string_view* flag, std::function<void()>* f>
struct CommandLineLambda {
//returns whether or not the argument was called
static inline bool runArg() {
auto iter = std::find(arg->begin(), arg->end(), *flag);
if (iter != arg->end()) {
(*f)();
return true;
}
return false;
}
};
template<std::vector<std::string>* arg, std::string_view* flag, std::optional<std::string>* var>
struct CommandLineOptional {
//returns the success of parsing the argument
static inline bool runArg() {
auto iter = std::find(arg->begin(), arg->end(), *flag);
if (iter != arg->end()) {
if (
iter == std::prev(arg->end()) ||
std::next(iter)->front() == '-'
) {
printf("No argument supplied to %s\n", "-a");
return false;
} else {
(*var) = *(std::next(iter));
}
}
return true;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment