Skip to content

Commit

Permalink
Merge pull request #2433 from NilsBossung/13315
Browse files Browse the repository at this point in the history
fix issues 13315, 13316, 13317 - std.getopt woes
  • Loading branch information
H. S. Teoh committed Aug 18, 2014
2 parents 68833f0 + b676492 commit a2b4b0e
Showing 1 changed file with 52 additions and 12 deletions.
64 changes: 52 additions & 12 deletions std/getopt.d
Expand Up @@ -579,30 +579,33 @@ private void getoptImpl(T...)(ref string[] args, ref configuration cfg,
else
{
// no more options to look for, potentially some arguments left
foreach (i, a ; args[1 .. $]) {
for (size_t i = 1; i < args.length;)
{
auto a = args[i];
if (endOfOptions.length && a == endOfOptions)
{
// Consume the "--"
args = args.remove(i);
break;
}
if (!a.length || a[0] != optionChar)
{
// not an option
if (cfg.stopOnFirstNonOption) break;
++i;
continue;
}
if (endOfOptions.length && a == endOfOptions)
if (a == "--help" || a == "-h")
{
// Consume the "--"
args = args.remove(i + 1);
break;
rslt.helpWanted = true;
args = args.remove(i);
continue;
}
if (!cfg.passThrough)
{
throw new GetOptException("Unrecognized option "~a);
}

if (a == "--help" || a == "-h")
{
rslt.helpWanted = true;

args = args.remove(i + 1);
}
++i;
}

Option helpOpt;
Expand Down Expand Up @@ -1278,6 +1281,43 @@ unittest
assert(!r.helpWanted);
}

unittest // implicit help option without config.passThrough
{
string[] args = ["program", "--help"];
auto r = getopt(args);
assert(r.helpWanted);
}

// Issue 13316 - std.getopt: implicit help option breaks the next argument
unittest
{
string[] args = ["program", "--help", "--", "something"];
getopt(args);
assert(args == ["program", "something"]);

args = ["program", "--help", "--"];
getopt(args);
assert(args == ["program"]);

bool b;
args = ["program", "--help", "nonoption", "--option"];
getopt(args, config.stopOnFirstNonOption, "option", &b);
assert(args == ["program", "nonoption", "--option"]);
}

// Issue 13317 - std.getopt: endOfOptions broken when it doesn't look like an option
unittest
{
auto endOfOptionsBackup = endOfOptions;
scope(exit) endOfOptions = endOfOptionsBackup;
endOfOptions = "endofoptions";
string[] args = ["program", "endofoptions", "--option"];
bool b = false;
getopt(args, "option", &b);
assert(!b);
assert(args == ["program", "--option"]);
}

/** This function prints the passed $(D Option) and text in an aligned manner.
The passed text will be printed first, followed by a newline. Than the short
Expand Down

0 comments on commit a2b4b0e

Please sign in to comment.