aboutsummaryrefslogtreecommitdiff
path: root/examples/ini.tm
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-09-15 16:53:42 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-09-15 16:53:42 -0400
commit0060686646561b099e3c8b9908d82cc22eba433a (patch)
treeaf28f64b57252fdbf8262adfcc625c58dedc43fa /examples/ini.tm
parent835eb7e89627eea923bfd57bdacba7065c6b1d4c (diff)
Update examples
Diffstat (limited to 'examples/ini.tm')
-rw-r--r--examples/ini.tm22
1 files changed, 10 insertions, 12 deletions
diff --git a/examples/ini.tm b/examples/ini.tm
index afd71a30..c0d490cb 100644
--- a/examples/ini.tm
+++ b/examples/ini.tm
@@ -7,7 +7,7 @@ _HELP := "
"
func parse_ini(path:Path)->{Text:{Text:Text}}:
- text := path:read()
+ text := path:read():or_exit("Could not read INI file: $\[31;1]$(path.text_content)$\[]")
sections := {:Text:@{Text:Text}}
current_section := @{:Text:Text}
sections:set("", current_section)
@@ -37,25 +37,23 @@ func main(path:Path, key:Text):
$_USAGE
")
- if not path:is_file() or path:is_pipe():
- exit("Could not read file: $(path.text_content)")
-
data := parse_ini(path)
if keys.length < 1 or keys[1] == '*':
!! $data
return
section := keys[1]:lower()
- if not data:has(section):
- exit("Invalid section name: $section; valid names: $(", ":join([k:quoted() for k in data.keys]))")
-
- section_data := data:get(section)
+ section_data := data:get(section):or_exit("
+ Invalid section name: $\[31;1]$section$\[]
+ Valid names: $\[1]$(", ":join([k:quoted() for k in data.keys]))$\[]
+ ")
if keys.length < 2 or keys[2] == '*':
!! $section_data
return
section_key := keys[2]:lower()
- if not section_data:has(section_key):
- exit("Invalid key: $section_key; valid keys: $(", ":join(section_data.keys))")
-
- say(section_data:get(section_key))
+ value := section_data:get(section_key):or_exit("
+ Invalid key: $\[31;1]$section_key$\[]
+ Valid keys: $\[1]$(", ":join([s:quoted() for s in section_data.keys]))$\[]
+ ")
+ say(value)