1
1
mirror of https://github.com/NixOS/mobile-nixos.git synced 2024-12-18 05:21:47 +03:00
mobile-nixos/boot/lib/lvgui/args.rb
2021-01-01 21:37:29 -05:00

51 lines
1.0 KiB
Ruby

# Extremely minimal and naïve arguments parsing.
module Args
@@parsed = nil
def self.get(key, default = nil)
parse[:values][key] or default
end
def self.unused()
parsed[:unused]
end
def self.parse()
return @@parsed if @@parsed
argv = ARGV.dup
unused = []
# Default values.
# `nil` means it expects a parameter.
# `false` means it's a boolean toggle without parameter.
values = {}
while argv.length > 0
arg = argv.shift
if arg.match(/^--help$/)
# Assumes the programmer will add it
begin
print_help
rescue
$stderr.puts "Sorry, no help defined."
end
exit 0
elsif arg.match(/^--/)
key = arg.sub(/^--/, "").gsub("-", "_").to_sym
if argv.length > 0 && !argv[0].match(/^--/)
values[key] = argv.shift
else
values[key] = true
end
else
unused << arg
end
end
@@parsed = {
unused: unused,
values: values,
}
end
end