2019-11-21 13:03:12 +01:00
|
|
|
module cli
|
|
|
|
|
|
|
|
pub enum FlagType {
|
|
|
|
bool
|
|
|
|
int
|
|
|
|
float
|
|
|
|
string
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Flag {
|
|
|
|
pub mut:
|
2020-07-18 13:24:10 +02:00
|
|
|
flag FlagType
|
|
|
|
name string
|
|
|
|
abbrev string
|
2019-11-21 13:03:12 +01:00
|
|
|
description string
|
2020-07-18 13:24:10 +02:00
|
|
|
global bool
|
|
|
|
required bool
|
|
|
|
value string
|
2020-07-02 11:10:03 +02:00
|
|
|
mut:
|
2020-07-18 13:24:10 +02:00
|
|
|
found bool
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
|
2020-07-02 11:10:03 +02:00
|
|
|
pub fn (flags []Flag) get_all_found() []Flag {
|
|
|
|
return flags.filter(it.found)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (flag Flag) get_bool() ?bool {
|
2020-07-18 13:24:10 +02:00
|
|
|
if flag.flag != .bool {
|
2020-09-16 22:01:44 +02:00
|
|
|
return error('$flag.name: Invalid flag type `$flag.flag`, expected `bool`')
|
2020-07-18 13:24:10 +02:00
|
|
|
}
|
2019-11-21 13:03:12 +01:00
|
|
|
return flag.value == 'true'
|
|
|
|
}
|
|
|
|
|
2020-07-02 11:10:03 +02:00
|
|
|
pub fn (flags []Flag) get_bool(name string) ?bool {
|
2020-09-16 22:01:44 +02:00
|
|
|
flag := flags.get(name) ?
|
2020-07-02 11:10:03 +02:00
|
|
|
return flag.get_bool()
|
|
|
|
}
|
|
|
|
|
2020-04-30 12:18:46 +02:00
|
|
|
pub fn (flags []Flag) get_bool_or(name string, or_value bool) bool {
|
2020-07-18 13:24:10 +02:00
|
|
|
value := flags.get_bool(name) or {
|
|
|
|
return or_value
|
|
|
|
}
|
2020-04-30 12:18:46 +02:00
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2020-07-02 11:10:03 +02:00
|
|
|
pub fn (flag Flag) get_int() ?int {
|
2020-07-18 13:24:10 +02:00
|
|
|
if flag.flag != .int {
|
2020-09-16 22:01:44 +02:00
|
|
|
return error('$flag.name: Invalid flag type `$flag.flag`, expected `int`')
|
2020-07-18 13:24:10 +02:00
|
|
|
}
|
2019-11-21 13:03:12 +01:00
|
|
|
return flag.value.int()
|
|
|
|
}
|
|
|
|
|
2020-07-02 11:10:03 +02:00
|
|
|
pub fn (flags []Flag) get_int(name string) ?int {
|
2020-09-16 22:01:44 +02:00
|
|
|
flag := flags.get(name) ?
|
2020-07-02 11:10:03 +02:00
|
|
|
return flag.get_int()
|
|
|
|
}
|
|
|
|
|
2020-04-30 12:18:46 +02:00
|
|
|
pub fn (flags []Flag) get_int_or(name string, or_value int) int {
|
2020-07-18 13:24:10 +02:00
|
|
|
value := flags.get_int(name) or {
|
|
|
|
return or_value
|
|
|
|
}
|
2020-04-30 12:18:46 +02:00
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2020-07-02 11:10:03 +02:00
|
|
|
pub fn (flag Flag) get_float() ?f64 {
|
2020-07-18 13:24:10 +02:00
|
|
|
if flag.flag != .float {
|
2020-09-16 22:01:44 +02:00
|
|
|
return error('$flag.name: Invalid flag type `$flag.flag`, expected `float`')
|
2020-07-18 13:24:10 +02:00
|
|
|
}
|
2020-07-02 11:10:03 +02:00
|
|
|
return flag.value.f64()
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
|
2020-07-02 11:10:03 +02:00
|
|
|
pub fn (flags []Flag) get_float(name string) ?f64 {
|
2020-09-16 22:01:44 +02:00
|
|
|
flag := flags.get(name) ?
|
2020-07-02 11:10:03 +02:00
|
|
|
return flag.get_float()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (flags []Flag) get_float_or(name string, or_value f64) f64 {
|
2020-07-18 13:24:10 +02:00
|
|
|
value := flags.get_float(name) or {
|
|
|
|
return or_value
|
|
|
|
}
|
2020-04-30 12:18:46 +02:00
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2020-07-02 11:10:03 +02:00
|
|
|
pub fn (flag Flag) get_string() ?string {
|
2020-07-18 13:24:10 +02:00
|
|
|
if flag.flag != .string {
|
2020-09-16 22:01:44 +02:00
|
|
|
return error('$flag.name: Invalid flag type `$flag.flag`, expected `string`')
|
2020-07-18 13:24:10 +02:00
|
|
|
}
|
2019-11-21 13:03:12 +01:00
|
|
|
return flag.value
|
|
|
|
}
|
|
|
|
|
2020-07-02 11:10:03 +02:00
|
|
|
pub fn (flags []Flag) get_string(name string) ?string {
|
2020-09-16 22:01:44 +02:00
|
|
|
flag := flags.get(name) ?
|
2020-07-02 11:10:03 +02:00
|
|
|
return flag.get_string()
|
|
|
|
}
|
|
|
|
|
2020-10-21 11:23:03 +02:00
|
|
|
pub fn (flags []Flag) get_string_or(name string, or_value string) string {
|
2020-07-18 13:24:10 +02:00
|
|
|
value := flags.get_string(name) or {
|
|
|
|
return or_value
|
|
|
|
}
|
2020-04-30 12:18:46 +02:00
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2019-11-21 13:03:12 +01:00
|
|
|
// parse flag value from arguments and return arguments with all consumed element removed
|
2020-07-18 13:24:10 +02:00
|
|
|
fn (mut flag Flag) parse(args []string, with_abbrev bool) ?[]string {
|
|
|
|
if flag.matches(args, with_abbrev) {
|
2019-11-21 13:03:12 +01:00
|
|
|
if flag.flag == .bool {
|
2020-09-16 22:01:44 +02:00
|
|
|
new_args := flag.parse_bool(args) ?
|
2019-11-21 13:03:12 +01:00
|
|
|
return new_args
|
|
|
|
} else {
|
2020-09-16 22:01:44 +02:00
|
|
|
new_args := flag.parse_raw(args) ?
|
2019-11-21 13:03:12 +01:00
|
|
|
return new_args
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if first arg matches flag
|
2020-07-18 13:24:10 +02:00
|
|
|
fn (mut flag Flag) matches(args []string, with_abbrev bool) bool {
|
|
|
|
if with_abbrev {
|
|
|
|
return (flag.name != '' && args[0] == '--$flag.name') ||
|
|
|
|
(flag.name != '' && args[0].starts_with('--$flag.name=')) ||
|
|
|
|
(flag.abbrev != '' && args[0] == '-$flag.abbrev') ||
|
|
|
|
(flag.abbrev != '' && args[0].starts_with('-$flag.abbrev='))
|
|
|
|
} else {
|
|
|
|
return (flag.name != '' && args[0] == '-$flag.name') ||
|
|
|
|
(flag.name != '' && args[0].starts_with('-$flag.name='))
|
|
|
|
}
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut flag Flag) parse_raw(args []string) ?[]string {
|
2019-11-21 13:03:12 +01:00
|
|
|
if args[0].len > flag.name.len && args[0].contains('=') {
|
|
|
|
flag.value = args[0].split('=')[1]
|
2019-11-30 10:44:40 +01:00
|
|
|
return args[1..]
|
2019-11-21 13:03:12 +01:00
|
|
|
} else if args.len >= 2 {
|
|
|
|
flag.value = args[1]
|
2019-11-30 10:44:40 +01:00
|
|
|
return args[2..]
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
2020-08-20 23:14:53 +02:00
|
|
|
return error('Missing argument for `$flag.name`')
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut flag Flag) parse_bool(args []string) ?[]string {
|
2019-11-21 13:03:12 +01:00
|
|
|
if args[0].len > flag.name.len && args[0].contains('=') {
|
|
|
|
flag.value = args[0].split('=')[1]
|
2019-11-30 10:44:40 +01:00
|
|
|
return args[1..]
|
|
|
|
} else if args.len >= 2 {
|
2019-11-21 13:03:12 +01:00
|
|
|
if args[1] in ['true', 'false'] {
|
|
|
|
flag.value = args[1]
|
2019-11-30 10:44:40 +01:00
|
|
|
return args[2..]
|
|
|
|
}
|
|
|
|
}
|
2019-11-21 13:03:12 +01:00
|
|
|
flag.value = 'true'
|
2019-11-30 10:44:40 +01:00
|
|
|
return args[1..]
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (flags []Flag) get(name string) ?Flag {
|
|
|
|
for flag in flags {
|
|
|
|
if flag.name == name {
|
|
|
|
return flag
|
|
|
|
}
|
|
|
|
}
|
2020-09-16 22:01:44 +02:00
|
|
|
return error('Flag `$name` not found in $flags')
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (flags []Flag) contains(name string) bool {
|
|
|
|
for flag in flags {
|
|
|
|
if flag.name == name || flag.abbrev == name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2020-07-02 11:10:03 +02:00
|
|
|
|
2020-08-20 23:14:53 +02:00
|
|
|
fn (flags []Flag) have_abbrev() bool {
|
|
|
|
mut have_abbrev := false
|
|
|
|
for flag in flags {
|
|
|
|
if flag.abbrev != '' {
|
|
|
|
have_abbrev = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return have_abbrev
|
2020-08-12 18:43:41 +02:00
|
|
|
}
|