-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_line.d
More file actions
100 lines (86 loc) · 2.03 KB
/
command_line.d
File metadata and controls
100 lines (86 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/dmd -run
/*******************************************************************************
command_line.d
To compile: dmd command_line.d
To optimize: dmd -O -inline -release command_line.d
To get generated documentation: dmd command_line.d -D
*******************************************************************************/
import std.stdio;
void main(char[][] args) {
writefln("Command line arguments:");
foreach(argc, argv; args) {
auto cl = new CmdLin(argc, argv);
writefln(cl.argnum, cl.suffix, " arg: %s", cl.argv);
delete cl;
}
struct specs {
int count, allocated;
}
specs argspecs(char[][] args)
in {
assert(args.length > 0);
}
out(result) {
assert(result.count == CmdLin.total);
assert(result.allocated > 0);
}
body {
specs *s = new specs;
s.count = args.length;
s.allocated = typeof(args).sizeof;
foreach(argv; args) {
s.allocated += argv.length * typeof(argv[0]).sizeof;
}
return *s;
}
char[] argcmsg = "argc = %d";
char[] allocmsg = "allocated = %d";
writefln(argcmsg ~ ", " ~ allocmsg, argspecs(args).count,
argspecs(args).allocated);
}
/*******************************************************************************
CmdLin class: Stores a single command line argument.
*******************************************************************************/
class CmdLin {
private:
int argc_;
char[] argv_;
static uint totalc_;
public:
this(int argc, char[] argv) {
argc_ = argc + 1;
argv_ = argv;
totalc_++;
}
~this() {}
int argnum() {
return argc_;
}
char[] argv() {
return argv_;
}
wchar[] suffix() {
wchar[] suffix;
switch(argc_) {
case 1:
suffix = "st";
break;
case 2:
suffix = "nd";
break;
case 3:
suffix = "rd";
break;
default:
suffix = "th";
}
return suffix;
}
static typeof(totalc_) total() {
return totalc_;
}
invariant {
assert(argc_ > 0);
assert(totalc_ >= argc_);
}
}