Works together with flags_data extension.
namespace flags utilities
implicit import ideal • library • elements
implicit import ideal • library • patterns
implicit import ideal • runtime • elements
implicit import ideal • runtime • patterns
implicit import ideal • machine • channels
implicit import ideal • machine • characters
dash pattern : singleton pattern[character] • new('-')
separator pattern : option pattern[character] • new([singleton pattern[character] • new('=') .> pattern[character], singleton pattern[character] • new(':')])
dictionary[string, string] parse flags(readonly list[string] arguments, procedure[void, string] error reporter)
for index = 0; index < arguments • size; index += 1
argument : arguments[index]
if dash pattern • match prefix(argument) is_not null
if separator is null
arg dictionary • put(normalize(argument • skip(1)), "")
else
arg dictionary • put(normalize(argument • slice(1, separator • begin)), argument • skip(separator • end))
else
break
if index < arguments • size
error reporter("Non-flag parameters found--don't know what to do!")
return arg dictionary
private string normalize(string the string)
result : string writer • new()
for (c : the string .> readonly list[character])
if c != '-' && c != '_'
result • write(unicode handler • instance • to lower case(c))
return result • elements()
boolean boolean flag(dictionary[string, string] arg dictionary, string name)
not flag : "not" ++ flag name
if arg dictionary • contains key(not flag)
arg dictionary • remove(not flag)
return false
no flag : "no" ++ flag name
if arg dictionary • contains key(no flag)
arg dictionary • remove(no flag)
return false
value : arg dictionary • get(flag name)
if value is null
return false
arg dictionary • remove(flag name)
return flag value != "false" && flag value != "no"
string or null string flag(dictionary[string, string] arg dictionary, string name)
has flag : arg dictionary • contains key(flag name)
if has flag
return arg dictionary • remove(flag name)
else
return missing • instance
void finish(dictionary[string, string] arg dictionary, procedure[void, string] error reporter)
if arg dictionary • is not empty
error reporter("Unknown flag: " ++ arg dictionary • keys() • elements • first)