2019-11-21 15:03:12 +03:00
|
|
|
module cli
|
|
|
|
|
|
|
|
pub enum FlagType {
|
|
|
|
bool
|
|
|
|
int
|
|
|
|
float
|
|
|
|
string
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Flag {
|
|
|
|
pub mut:
|
|
|
|
flag FlagType
|
|
|
|
name string
|
|
|
|
abbrev string
|
|
|
|
description string
|
|
|
|
global bool
|
|
|
|
required bool
|
|
|
|
|
|
|
|
value string
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (flags []Flag) get_bool(name string) ?bool {
|
|
|
|
flag := flags.get(name) or { return error(err) }
|
|
|
|
if flag.flag != .bool { return error('invalid flag type') }
|
|
|
|
return flag.value == 'true'
|
|
|
|
}
|
|
|
|
|
2020-04-30 13:18:46 +03:00
|
|
|
pub fn (flags []Flag) get_bool_or(name string, or_value bool) bool {
|
|
|
|
value := flags.get_bool(name) or { return or_value }
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:03:12 +03:00
|
|
|
pub fn (flags []Flag) get_int(name string) ?int {
|
|
|
|
flag := flags.get(name) or { return error(err) }
|
|
|
|
if flag.flag != .int { return error('invalid flag type') }
|
|
|
|
return flag.value.int()
|
|
|
|
}
|
|
|
|
|
2020-04-30 13:18:46 +03:00
|
|
|
pub fn (flags []Flag) get_int_or(name string, or_value int) int {
|
|
|
|
value := flags.get_int(name) or { return or_value }
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:03:12 +03:00
|
|
|
pub fn (flags []Flag) get_float(name string) ?f32 {
|
|
|
|
flag := flags.get(name) or { return error(err) }
|
|
|
|
if flag.flag != .float { return error('invalid flag type') }
|
|
|
|
return flag.value.f32()
|
|
|
|
}
|
|
|
|
|
2020-04-30 13:18:46 +03:00
|
|
|
pub fn (flags []Flag) get_float_or(name string, or_value f32) f32 {
|
|
|
|
value := flags.get_float(name) or { return or_value }
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:03:12 +03:00
|
|
|
pub fn (flags []Flag) get_string(name string) ?string {
|
|
|
|
flag := flags.get(name) or { return error(err) }
|
|
|
|
if flag.flag != .string { return error('invalid flag type') }
|
|
|
|
return flag.value
|
|
|
|
}
|
|
|
|
|
2020-04-30 13:18:46 +03:00
|
|
|
pub fn (flags []Flag) get_string_or(name string, or_value string) string {
|
|
|
|
value := flags.get_string(name) or { return or_value }
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:03:12 +03:00
|
|
|
// parse flag value from arguments and return arguments with all consumed element removed
|
2020-05-17 14:51:18 +03:00
|
|
|
fn (mut flag Flag) parse(args []string) ?[]string {
|
2019-11-21 15:03:12 +03:00
|
|
|
if flag.matches(args) {
|
|
|
|
if flag.flag == .bool {
|
|
|
|
new_args := flag.parse_bool(args) or { return error(err) }
|
|
|
|
return new_args
|
|
|
|
} else {
|
|
|
|
new_args := flag.parse_raw(args) or { return error(err) }
|
|
|
|
return new_args
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if first arg matches flag
|
2020-07-01 11:54:34 +03:00
|
|
|
fn (mut flag Flag) matches(args []string) bool {
|
2019-11-30 12:44:40 +03:00
|
|
|
return
|
2019-11-21 15:03:12 +03:00
|
|
|
(flag.name != '' && args[0].starts_with('--${flag.name}')) ||
|
|
|
|
(flag.abbrev != '' && args[0].starts_with('-${flag.abbrev}'))
|
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
fn (mut flag Flag) parse_raw(args []string) ?[]string {
|
2019-11-21 15:03:12 +03:00
|
|
|
if args[0].len > flag.name.len && args[0].contains('=') {
|
|
|
|
flag.value = args[0].split('=')[1]
|
2019-11-30 12:44:40 +03:00
|
|
|
return args[1..]
|
2019-11-21 15:03:12 +03:00
|
|
|
} else if args.len >= 2 {
|
|
|
|
flag.value = args[1]
|
2019-11-30 12:44:40 +03:00
|
|
|
return args[2..]
|
2019-11-21 15:03:12 +03:00
|
|
|
}
|
|
|
|
return error('missing argument for ${flag.name}')
|
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
fn (mut flag Flag) parse_bool(args []string) ?[]string {
|
2019-11-21 15:03:12 +03:00
|
|
|
if args[0].len > flag.name.len && args[0].contains('=') {
|
|
|
|
flag.value = args[0].split('=')[1]
|
2019-11-30 12:44:40 +03:00
|
|
|
return args[1..]
|
|
|
|
} else if args.len >= 2 {
|
2019-11-21 15:03:12 +03:00
|
|
|
if args[1] in ['true', 'false'] {
|
|
|
|
flag.value = args[1]
|
2019-11-30 12:44:40 +03:00
|
|
|
return args[2..]
|
|
|
|
}
|
|
|
|
}
|
2019-11-21 15:03:12 +03:00
|
|
|
flag.value = 'true'
|
2019-11-30 12:44:40 +03:00
|
|
|
return args[1..]
|
2019-11-21 15:03:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (flags []Flag) get(name string) ?Flag {
|
|
|
|
for flag in flags {
|
|
|
|
if flag.name == name {
|
|
|
|
return flag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return error('flag ${name} not found.')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (flags []Flag) contains(name string) bool {
|
|
|
|
for flag in flags {
|
|
|
|
if flag.name == name || flag.abbrev == name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|