Merge branch 'master' of github.com:andralex/phobos into current-pull-request

This commit is contained in:
Andrei Alexandrescu 2011-04-17 09:28:17 -05:00
commit 047bf768ec
3 changed files with 38 additions and 4 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
generated
GNUmakefile

View file

@ -49,6 +49,8 @@ import std.getopt;
string data = "file.dat"; string data = "file.dat";
int length = 24; int length = 24;
bool verbose; bool verbose;
enum Color { no, yes };
Color color;
void main(string[] args) void main(string[] args)
{ {
@ -56,7 +58,8 @@ void main(string[] args)
args, args,
"length", &length, // numeric "length", &length, // numeric
"file", &data, // string "file", &data, // string
"verbose", &verbose); // flag "verbose", &verbose, // flag
"color", &color); // enum
... ...
} }
--------- ---------
@ -117,6 +120,19 @@ To set $(D timeout) to $(D 5), invoke the program with either $(D
instead, $(D paranoid) is set to 2 and "42" is not considered instead, $(D paranoid) is set to 2 and "42" is not considered
as part of the normal program arguments.)) as part of the normal program arguments.))
$(LI $(I Enum options.) If an option is bound to an enum, an enum symbol as a
string is expected as the next option, or right within the option separated
with an "=" sign:
---------
enum Color { no, yes };
Color color; // default initialized to Color.no
getopt(args, "color", &color);
---------
To set $(D color) to $(D Color.yes), invoke the program with either $(D
--color=yes) or $(D --color yes).
$(LI $(I String options.) If an option is bound to a string, a string $(LI $(I String options.) If an option is bound to a string, a string
is expected as the next option, or right within the option separated is expected as the next option, or right within the option separated
with an "=" sign: with an "=" sign:
@ -459,6 +475,11 @@ void handleOption(R)(string option, R receiver, ref string[] args,
if (incremental) ++*receiver; if (incremental) ++*receiver;
else *receiver = to!(typeof(*receiver))(val); else *receiver = to!(typeof(*receiver))(val);
} }
else static if (is(typeof(*receiver) == enum))
{
// enum receiver
*receiver = parse!(typeof(*receiver))(val);
}
else static if (is(typeof(*receiver) == string)) else static if (is(typeof(*receiver) == string))
{ {
// string receiver // string receiver
@ -614,6 +635,17 @@ unittest
getopt(args, "paranoid+", &paranoid); getopt(args, "paranoid+", &paranoid);
assert(paranoid == 5, to!(string)(paranoid)); assert(paranoid == 5, to!(string)(paranoid));
enum Color { no, yes };
Color color;
args = (["program.name", "--color=yes",]).dup;
getopt(args, "color", &color);
assert(color, to!(string)(color));
color = Color.no;
args = (["program.name", "--color", "yes",]).dup;
getopt(args, "color", &color);
assert(color, to!(string)(color));
string data = "file.dat"; string data = "file.dat";
int length = 24; int length = 24;
bool verbose = false; bool verbose = false;

View file

@ -247,7 +247,7 @@ DOCS= $(DOC)\object.html \
$(DOC)\std_net_isemail.html \ $(DOC)\std_net_isemail.html \
$(DOC)\phobos.html $(DOC)\phobos.html
SRC= unittest.d crc32.d phobos.d SRC= unittest.d crc32.d index.d
SRC_STD= std\zlib.d std\zip.d std\stdint.d std\container.d std\conv.d std\utf.d std\uri.d \ SRC_STD= std\zlib.d std\zip.d std\stdint.d std\container.d std\conv.d std\utf.d std\uri.d \
std\math.d std\string.d std\path.d std\date.d std\datetime.d \ std\math.d std\string.d std\path.d std\date.d std\datetime.d \
@ -614,8 +614,8 @@ windows.obj : std\c\windows\windows.d
$(DOC)\object.html : $(STDDOC) $(DRUNTIME)\src\object_.d $(DOC)\object.html : $(STDDOC) $(DRUNTIME)\src\object_.d
$(DMD) -c -o- $(DFLAGS) -Df$(DOC)\object.html $(STDDOC) $(DRUNTIME)\src\object_.d -I$(DRUNTIME)\src\ $(DMD) -c -o- $(DFLAGS) -Df$(DOC)\object.html $(STDDOC) $(DRUNTIME)\src\object_.d -I$(DRUNTIME)\src\
$(DOC)\phobos.html : $(STDDOC) phobos.d $(DOC)\phobos.html : $(STDDOC) index.d
$(DMD) -c -o- $(DFLAGS) -Df$(DOC)\phobos.html $(STDDOC) phobos.d $(DMD) -c -o- $(DFLAGS) -Df$(DOC)\phobos.html $(STDDOC) index.d
$(DOC)\core_atomic.html : $(STDDOC) $(DRUNTIME)\src\core\atomic.d $(DOC)\core_atomic.html : $(STDDOC) $(DRUNTIME)\src\core\atomic.d
$(DMD) -c -o- $(DFLAGS) -Df$(DOC)\core_atomic.html $(STDDOC) $(DRUNTIME)\src\core\atomic.d -I$(DRUNTIME)\src\ $(DMD) -c -o- $(DFLAGS) -Df$(DOC)\core_atomic.html $(STDDOC) $(DRUNTIME)\src\core\atomic.d -I$(DRUNTIME)\src\