-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
220 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include "options.h" | ||
|
||
std::vector<std::string> OptionHandler::parse(int argc, const char *argv[]) | ||
{ | ||
std::vector<std::string> remaining; | ||
|
||
ArgQueue args; | ||
for(int i = 1; i < argc; ++i) // skip argv[0] | ||
args.push(argv[i]); | ||
|
||
while(!args.empty()) { | ||
auto word = args.front(); | ||
args.pop(); | ||
|
||
if(word.substr(0, 1) != "-") { | ||
remaining.push_back(word); | ||
continue; | ||
} | ||
|
||
AbstractOption *opt = nullptr; | ||
|
||
for(auto &o : m_Options) { | ||
if(word == o->shortName || word == o->longName) { | ||
opt = o.get(); | ||
break; | ||
} | ||
} | ||
|
||
if(!opt) { | ||
fprintf(stderr, "Unknown option: \"%s\"\n", word.c_str()); | ||
exit(1); | ||
} | ||
|
||
opt->parse(args); | ||
} | ||
|
||
return remaining; | ||
} | ||
|
||
void OptionHandler::printHelp(FILE *f) | ||
{ | ||
fprintf(f, "\nUsage:\n"); | ||
for(auto &o : m_Options) { | ||
auto s = o->shortName + ", " + o->longName; | ||
while(s.size() < 40) | ||
s += " "; | ||
fprintf(f, " %s%s\n", s.c_str(), o->desc.c_str()); | ||
} | ||
fprintf(f, "\n"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#pragma once | ||
|
||
#include <memory> | ||
#include <queue> | ||
#include <string> | ||
#include <vector> | ||
|
||
typedef std::queue<std::string> ArgQueue; | ||
|
||
static inline std::string safePop(ArgQueue &args) | ||
{ | ||
if(args.empty()) { | ||
fprintf(stderr, "unexpected end of command line\n"); | ||
exit(1); | ||
} | ||
|
||
auto val = args.front(); | ||
args.pop(); | ||
return val; | ||
} | ||
|
||
static inline void parseValue(bool &var, ArgQueue &) | ||
{ | ||
var = true; | ||
} | ||
|
||
static inline void parseValue(std::string &var, ArgQueue &args) | ||
{ | ||
var = safePop(args); | ||
} | ||
|
||
template<typename Element> | ||
static inline void parseValue(std::vector<Element> &var, ArgQueue &args) | ||
{ | ||
Element e{}; | ||
parseValue(e, args); | ||
var.push_back(e); | ||
} | ||
|
||
struct OptionHandler { | ||
void addFlag(std::string shortName, std::string longName, bool *pVar, std::string desc = "") | ||
{ | ||
add(shortName, longName, pVar, desc); | ||
} | ||
|
||
template<typename T> | ||
void add(std::string shortName, std::string longName, T *pVar, std::string desc = "") | ||
{ | ||
auto opt = std::make_unique<TypedOption<T>>(); | ||
opt->pVar = pVar; | ||
opt->shortName = "-" + shortName; | ||
opt->longName = "--" + longName; | ||
opt->desc = desc; | ||
m_Options.push_back(std::move(opt)); | ||
} | ||
|
||
std::vector<std::string> parse(int argc, const char *argv[]); | ||
void printHelp(FILE *); | ||
|
||
private: | ||
struct AbstractOption { | ||
virtual ~AbstractOption() = default; | ||
std::string shortName, longName; | ||
std::string desc; | ||
virtual void parse(ArgQueue &args) = 0; | ||
}; | ||
|
||
std::vector<std::unique_ptr<AbstractOption>> m_Options; | ||
|
||
template<typename T> | ||
struct TypedOption : AbstractOption { | ||
T *pVar; | ||
void parse(ArgQueue &args) { parseValue(*pVar, args); } | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters