Skip to content

Commit 0d0ef3c

Browse files
authored
Merge pull request #56 from cgay/dev
parse-option-value: call as(param, type) in default method
2 parents 4926f58 + 5ef6e23 commit 0d0ef3c

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

command-line-parser.dylan

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -480,11 +480,16 @@ end method;
480480
define open generic parse-option-value
481481
(parameter :: <string>, type :: <type>) => (value :: <object>);
482482

483-
// Default method just returns the value.
484483
define method parse-option-value
485-
(param :: <string>, type :: <type>) => (value :: <string>)
486-
param
487-
end;
484+
(param :: <string>, type :: <type>) => (value :: <object>)
485+
block ()
486+
// If type happens to have an AS method (for example like <file-locator> does) then
487+
// we will automatically support that type.
488+
as(type, param)
489+
exception (<error>)
490+
param
491+
end
492+
end method;
488493

489494
// This is essentially for "float or int", which could be <real>, but
490495
// <number> is also a natural choice.

tests/command-line-parser-test-suite-library.dylan

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ define module command-line-parser-test-suite
1313
use command-line-parser;
1414
use common-dylan, exclude: { format-to-string };
1515
use format;
16+
use locators;
1617
use option-parser-protocol,
1718
import: {
1819
option-help,

tests/command-line-parser-test-suite.dylan

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ define test test-option-type ()
187187
help: "x",
188188
names: #("repeated-integer"),
189189
type: <integer>));
190+
add-option(parser, make(<parameter-option>,
191+
help: "x",
192+
names: #("file-locator"),
193+
type: <file-locator>));
190194
parser
191195
end method make-parser;
192196
let items = list(list("integer", "123", 123, <integer>),
@@ -198,7 +202,9 @@ define test test-option-type ()
198202
list("symbol", "foo", #"foo", <symbol>),
199203
list("number", "123", 123, <integer>),
200204
list("real", "123", 123, <integer>),
201-
list("string", "bar", "bar", <string>));
205+
list("string", "bar", "bar", <string>),
206+
list("file-locator", "/tmp/x", as(<file-locator>, "/tmp/x"),
207+
<file-locator>));
202208
for (item in items)
203209
let (name, param, expected-value, expected-type) = apply(values, item);
204210
let parser = make-parser();

0 commit comments

Comments
 (0)