Works together with flags_data extension.
namespace flags utilities
implicit import ideallibraryelements
implicit import ideallibrarypatterns
implicit import idealruntimeelements
implicit import idealruntimepatterns
implicit import idealmachinechannels
implicit import idealmachinecharacters
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 < argumentssize; index += 1
argument : arguments[index]
if dash patternmatch prefix(argument) is_not null
if separator is null
arg dictionaryput(normalize(argumentskip(1)), "")
else
arg dictionaryput(normalize(argumentslice(1, separatorbegin)), argumentskip(separatorend))
else
break
if index < argumentssize
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 != '_'
resultwrite(unicode handlerinstanceto lower case(c))
return resultelements()
boolean boolean flag(dictionary[string, string] arg dictionary, string name)
not flag : "not" ++ flag name
if arg dictionarycontains key(not flag)
arg dictionaryremove(not flag)
return false
no flag : "no" ++ flag name
if arg dictionarycontains key(no flag)
arg dictionaryremove(no flag)
return false
value : arg dictionaryget(flag name)
if value is null
return false
arg dictionaryremove(flag name)
return flag value != "false" && flag value != "no"
string or null string flag(dictionary[string, string] arg dictionary, string name)
has flag : arg dictionarycontains key(flag name)
if has flag
return arg dictionaryremove(flag name)
else
return missinginstance
void finish(dictionary[string, string] arg dictionary, procedure[void, string] error reporter)
if arg dictionaryis not empty
error reporter("Unknown flag: " ++ arg dictionarykeys() • elementsfirst)