2025-04-01 12:15:28 -07:00
|
|
|
|
|
|
|
use patterns
|
|
|
|
|
2024-09-06 21:26:30 -07:00
|
|
|
_USAGE := "
|
2024-09-06 09:19:52 -07:00
|
|
|
Usage: ini <filename> "[section[/key]]"
|
|
|
|
"
|
2024-09-06 21:26:30 -07:00
|
|
|
_HELP := "
|
2024-09-06 09:19:52 -07:00
|
|
|
ini: A .ini config file reader tool.
|
2024-09-06 21:26:30 -07:00
|
|
|
$_USAGE
|
2024-09-06 09:19:52 -07:00
|
|
|
"
|
|
|
|
|
2025-04-02 13:14:20 -07:00
|
|
|
func parse_ini(path:Path -> {Text={Text=Text}}):
|
2025-04-06 11:20:18 -07:00
|
|
|
text := path.read() or exit("Could not read INI file: $\[31;1]$(path)$\[]")
|
2025-04-04 14:06:09 -07:00
|
|
|
sections : @{Text=@{Text=Text}} = @{}
|
|
|
|
current_section : @{Text=Text} = @{}
|
2024-09-06 11:16:45 -07:00
|
|
|
|
|
|
|
# Line wraps:
|
2025-04-06 11:20:18 -07:00
|
|
|
text = text.replace_pattern($Pat/\{1 nl}{0+space}/, " ")
|
2024-09-06 11:16:45 -07:00
|
|
|
|
2025-04-06 11:20:18 -07:00
|
|
|
for line in text.lines():
|
|
|
|
line = line.trim()
|
|
|
|
skip if line.starts_with(";") or line.starts_with("#")
|
|
|
|
if line.matches_pattern($Pat/[?]/):
|
|
|
|
section_name := line.replace($Pat/[?]/, "\1").trim().lower()
|
2025-04-04 14:06:09 -07:00
|
|
|
current_section = @{}
|
2024-11-30 12:50:54 -08:00
|
|
|
sections[section_name] = current_section
|
2025-04-06 11:20:18 -07:00
|
|
|
else if line.matches_pattern($Pat/{..}={..}/):
|
|
|
|
key := line.replace_pattern($Pat/{..}={..}/, "\1").trim().lower()
|
|
|
|
value := line.replace_pattern($Pat/{..}={..}/, "\2").trim()
|
2024-11-30 12:50:54 -08:00
|
|
|
current_section[key] = value
|
2024-09-06 11:16:45 -07:00
|
|
|
|
2025-01-12 13:54:37 -08:00
|
|
|
return {k=v[] for k,v in sections[]}
|
2024-09-06 09:19:52 -07:00
|
|
|
|
2024-09-15 14:53:33 -07:00
|
|
|
func main(path:Path, key:Text?):
|
2025-04-06 11:20:18 -07:00
|
|
|
keys := (key or "").split($|/|)
|
2024-09-06 09:19:52 -07:00
|
|
|
if keys.length > 2:
|
2024-09-15 13:42:42 -07:00
|
|
|
exit("
|
2024-09-06 09:19:52 -07:00
|
|
|
Too many arguments!
|
2024-09-06 21:26:30 -07:00
|
|
|
$_USAGE
|
2024-09-06 09:19:52 -07:00
|
|
|
")
|
|
|
|
|
2024-09-09 12:02:47 -07:00
|
|
|
data := parse_ini(path)
|
2024-09-06 09:19:52 -07:00
|
|
|
if keys.length < 1 or keys[1] == '*':
|
2025-04-06 10:40:17 -07:00
|
|
|
say("$data")
|
2024-09-06 09:19:52 -07:00
|
|
|
return
|
|
|
|
|
2025-04-06 11:20:18 -07:00
|
|
|
section := keys[1].lower()
|
2024-11-30 12:50:54 -08:00
|
|
|
section_data := data[section] or exit("
|
2024-09-15 13:53:42 -07:00
|
|
|
Invalid section name: $\[31;1]$section$\[]
|
2025-04-06 11:20:18 -07:00
|
|
|
Valid names: $\[1]$(", ".join([k.quoted() for k in data.keys]))$\[]
|
2024-09-15 13:53:42 -07:00
|
|
|
")
|
2024-09-06 09:19:52 -07:00
|
|
|
if keys.length < 2 or keys[2] == '*':
|
2025-04-06 10:40:17 -07:00
|
|
|
say("$section_data")
|
2024-09-06 09:19:52 -07:00
|
|
|
return
|
|
|
|
|
2025-04-06 11:20:18 -07:00
|
|
|
section_key := keys[2].lower()
|
2024-11-30 12:50:54 -08:00
|
|
|
value := section_data[section_key] or exit("
|
2024-09-15 13:53:42 -07:00
|
|
|
Invalid key: $\[31;1]$section_key$\[]
|
2025-04-06 11:20:18 -07:00
|
|
|
Valid keys: $\[1]$(", ".join([s.quoted() for s in section_data.keys]))$\[]
|
2024-09-15 13:53:42 -07:00
|
|
|
")
|
|
|
|
say(value)
|