Skip to content

Commit

Permalink
Change long-option format
Browse files Browse the repository at this point in the history
Now, long-option look more like gzip/bzip2/xz (getopt_long()).
related to issue #19
  • Loading branch information
t-mat committed Sep 11, 2013
1 parent 05797e4 commit 8653127
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ const char usage_advanced[] =
" output : can be 'stdout'(pipe) or a filename or 'null'\n"

"\nlz4mt exclusive options :\n"
" --lz4mt-thread 0 : Multi thread mode (default)\n"
" --lz4mt-thread 1 : Single thread mode\n"
" --lz4mt-thread=0 : Multi thread mode (default)\n"
" --lz4mt-thread=1 : Single thread mode\n"
;

typedef std::function<bool(void)> AttyFunc;
Expand Down Expand Up @@ -96,28 +96,39 @@ struct Option {
args.push_back(argv[iarg]);
}

auto hasArg = [&]() {
return !args.empty();
auto isDigits = [](const std::string& s) {
return std::all_of(std::begin(s), std::end(s)
, static_cast<int(*)(int)>(std::isdigit));
};

auto getArg = [&]() -> std::string {
if(hasArg()) {
auto a = args.front();
args.pop_front();
return a;
std::map<std::string, std::function<bool (const std::string&)>> opts;

const char optdelim = '=';

auto getOptionName = [&](const std::string& s) -> std::string {
const auto pos = s.find(optdelim);
if(std::string::npos != pos) {
return s.substr(0, pos);
} else {
return std::string("");
return s;
}
};

auto isDigits = [](const std::string& s) {
return std::all_of(std::begin(s), std::end(s)
, static_cast<int(*)(int)>(std::isdigit));
auto getOptionArg = [&](const std::string& s) -> std::string {
const auto pos = s.find(optdelim);
if(std::string::npos != pos) {
return s.substr(pos+1);
} else {
return "";
}
};

auto findOption = [&](const std::string& s) {
return opts.find(getOptionName(s));
};

std::map<std::string, std::function<bool ()>> opts;
opts["--lz4mt-thread"] = [&]() -> bool {
auto a = getArg();
opts["--lz4mt-thread"] = [&](const std::string& arg) -> bool {
auto a = getOptionArg(arg);
if(isDigits(a)) {
const auto v = atoi(a.c_str());
switch(v) {
Expand All @@ -137,8 +148,9 @@ struct Option {
}
};

while(hasArg() && !error && !exitFlag) {
const auto a = getArg();
while(!args.empty() && !error && !exitFlag) {
const auto a = args.front();
args.pop_front();
const auto a0 = a[0];
const auto a1 = a[1];

Expand All @@ -164,13 +176,13 @@ struct Option {
}
} else if('-' == a0 && '-' == a1) {
// long option
const auto it = opts.find(a);
const auto it = findOption(a);
if(opts.end() == it) {
errorString += "lz4mt: Bad argument ["
+ std::string(a) + "]\n";
error = true;
} else {
const auto b = it->second();
const auto b = it->second(a);
if(!b) {
error = true;
}
Expand Down

0 comments on commit 8653127

Please sign in to comment.