From 5ef6e23b516150bf515f571518090e433867a468 Mon Sep 17 00:00:00 2001 From: Carl Gay Date: Sun, 12 Oct 2025 14:26:15 -0400 Subject: [PATCH] parse-option-value: call as(param, type) in default method Fixes #55 --- command-line-parser.dylan | 13 +++++++++---- tests/command-line-parser-test-suite-library.dylan | 1 + tests/command-line-parser-test-suite.dylan | 8 +++++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/command-line-parser.dylan b/command-line-parser.dylan index e432804..bb72b2a 100644 --- a/command-line-parser.dylan +++ b/command-line-parser.dylan @@ -480,11 +480,16 @@ end method; define open generic parse-option-value (parameter :: , type :: ) => (value :: ); -// Default method just returns the value. define method parse-option-value - (param :: , type :: ) => (value :: ) - param -end; + (param :: , type :: ) => (value :: ) + block () + // If type happens to have an AS method (for example like does) then + // we will automatically support that type. + as(type, param) + exception () + param + end +end method; // This is essentially for "float or int", which could be , but // is also a natural choice. diff --git a/tests/command-line-parser-test-suite-library.dylan b/tests/command-line-parser-test-suite-library.dylan index c113d2e..7b3f5c9 100644 --- a/tests/command-line-parser-test-suite-library.dylan +++ b/tests/command-line-parser-test-suite-library.dylan @@ -13,6 +13,7 @@ define module command-line-parser-test-suite use command-line-parser; use common-dylan, exclude: { format-to-string }; use format; + use locators; use option-parser-protocol, import: { option-help, diff --git a/tests/command-line-parser-test-suite.dylan b/tests/command-line-parser-test-suite.dylan index abc8ea3..8474596 100644 --- a/tests/command-line-parser-test-suite.dylan +++ b/tests/command-line-parser-test-suite.dylan @@ -187,6 +187,10 @@ define test test-option-type () help: "x", names: #("repeated-integer"), type: )); + add-option(parser, make(, + help: "x", + names: #("file-locator"), + type: )); parser end method make-parser; let items = list(list("integer", "123", 123, ), @@ -198,7 +202,9 @@ define test test-option-type () list("symbol", "foo", #"foo", ), list("number", "123", 123, ), list("real", "123", 123, ), - list("string", "bar", "bar", )); + list("string", "bar", "bar", ), + list("file-locator", "/tmp/x", as(, "/tmp/x"), + )); for (item in items) let (name, param, expected-value, expected-type) = apply(values, item); let parser = make-parser();