-
Notifications
You must be signed in to change notification settings - Fork 4
/
library.dylan
118 lines (105 loc) · 3.17 KB
/
library.dylan
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
module: dylan-user
author: Eric Kidd
copyright: See LICENSE file in this distribution.
define library command-line-parser
use common-dylan;
use dylan,
import: { dylan-extensions };
use io;
use strings;
use system;
export
command-line-parser,
option-parser-protocol;
end library;
// Only used when defining new option-parser subclasses.
define module option-parser-protocol
create
// <command-line-parser>
tokens-remaining?,
pop-token,
peek-token,
find-option,
add-pattern-substitution,
selected-subcommand,
positional-options,
// <option>
option-present?,
option-names, long-names, short-names,
option-help,
option-default,
option-might-have-parameters?, option-might-have-parameters?-setter,
option-repeated?, option-repeated?-setter,
option-value, option-value-setter,
option-variable,
parse-option,
negative-option?,
format-option-usage,
<token>,
token-value,
<argument-token>,
<option-token>,
<short-option-token>,
tightly-bound-to-next-token?, // XXX - not implemented fully
<long-option-token>,
<equals-token>;
end module option-parser-protocol;
// Used by most programs.
define module command-line-parser
use common-dylan,
exclude: { format-to-string };
use dylan-extensions,
import: { debug-name };
use format;
use locators;
use option-parser-protocol;
use standard-io;
use strings;
use streams;
export
<command-line-parser>,
execute-command,
add-option,
parse-command-line,
get-option-value,
print-help,
unconsumed-arguments,
parse-option-value;
// Subcommands
export
<subcommand>, // Subclass this for each subcommand...
<help-subcommand>, // ...except use this for the help subcommand.
add-subcommand,
execute-subcommand; // Override this for each subcommand.
// Options
export
<option>,
<flag-option>, // --opt or --opt=yes/no
<help-option>, // --help (handled specially)
<parameter-option>, // --opt=value
<choice-option>, // --opt=<one of a, b, or c>
<repeated-parameter-option>, // --opt=a --opt=b
<optional-parameter-option>, // --opt (gives #t) or --opt=x
<keyed-option>, // --opt k1=v1 --opt k2=v2
<positional-option>; // Args with no - or -- preceding.
// Must follow all - or -- options.
// Error handling
export
<command-line-parser-error>,
<abort-command-error>, // Always catch this in main function.
<usage-error>, // Optionally handle this also.
abort-command, // Terminate the command with an exit status.
exit-status, // Status code from <abort-command-error>.
usage-error; // Terminate the command with a message.
// define command-line
export
command-line-definer,
defcmdline-rec,
defcmdline-aux,
defcmdline-class,
defcmdline-init,
defcmdline-accessors;
// For the test suite. DO NOT DEPEND ON THESE!
export
program-name;
end module command-line-parser;