From e61096d6eb426747376cf8d8c6f46c62b583eb8e Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 9 Sep 2024 15:02:47 -0400 Subject: Fix up ini example --- examples/ini.tm | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'examples') diff --git a/examples/ini.tm b/examples/ini.tm index 5e6f438e..b2e59986 100644 --- a/examples/ini.tm +++ b/examples/ini.tm @@ -6,12 +6,8 @@ _HELP := " $_USAGE " -file := use ./file.tm - -func parse_ini(filename:Text)->{Text:{Text:Text}}: - text := when file.read(filename) is Failure(err): fail(err) - is Success(text): text - +func parse_ini(path:Path)->{Text:{Text:Text}}: + text := path:read() sections := {:Text:@{Text:Text}} current_section := @{:Text:Text} sections:set("", current_section) @@ -21,8 +17,9 @@ func parse_ini(filename:Text)->{Text:{Text:Text}}: for line in text:lines(): line = line:trim() + skip if line:starts_with(";") or line:starts_with("#") if line:matches($/{0+space}[{..}]/): - section_name := line:replace($/{0+space}[{..}]{..}/, "\2"):trim():lower() + section_name := line:replace($/[?]/, "\1"):trim():lower() current_section = @{:Text:Text} sections:set(section_name, current_section) else if line:matches($/{..}={..}/): @@ -32,7 +29,7 @@ func parse_ini(filename:Text)->{Text:{Text:Text}}: return {k:v[] for k,v in sections} -func main(filename:Text, key:Text): +func main(path:Path, key:Text): keys := key:split($Pattern"/") if keys.length > 2: exit(1, message=" @@ -40,7 +37,10 @@ func main(filename:Text, key:Text): $_USAGE ") - data := parse_ini(filename) + if not path:is_file() or path:is_pipe(): + exit(code=1, "Could not read file: $(path.text_content)") + + data := parse_ini(path) if keys.length < 1 or keys[1] == '*': !! $data return -- cgit v1.2.3