2019-11-21 13:03:12 +01:00
|
|
|
module cli
|
|
|
|
|
|
|
|
pub enum FlagType {
|
|
|
|
bool
|
|
|
|
int
|
|
|
|
float
|
|
|
|
string
|
2021-03-19 12:09:56 +01:00
|
|
|
// If flag can set multiple time, use array type
|
|
|
|
int_array
|
|
|
|
float_array
|
|
|
|
string_array
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
|
2021-01-20 22:15:48 +01:00
|
|
|
// Flag holds information for a command line flag.
|
|
|
|
// (flags are also commonly referred to as "options" or "switches")
|
|
|
|
// These are typically denoted in the shell by a short form `-f` and/or a long form `--flag`
|
2019-11-21 13:03:12 +01:00
|
|
|
pub struct Flag {
|
|
|
|
pub mut:
|
2021-03-19 12:09:56 +01:00
|
|
|
flag FlagType
|
|
|
|
// Name of flag
|
|
|
|
name string
|
|
|
|
// Like short option
|
|
|
|
abbrev string
|
|
|
|
// Desciption of flag
|
2019-11-21 13:03:12 +01:00
|
|
|
description string
|
2020-07-18 13:24:10 +02:00
|
|
|
global bool
|
2021-03-19 12:09:56 +01:00
|
|
|
// If flag is requierd
|
|
|
|
required bool
|
|
|
|
// Default value if no value provide by command line
|
|
|
|
default_value []string = []
|
2020-07-02 11:10:03 +02:00
|
|
|
mut:
|
2021-01-22 18:03:02 +01:00
|
|
|
// Set true if flag found.
|
2021-01-12 04:38:43 +01:00
|
|
|
found bool
|
2021-03-19 12:09:56 +01:00
|
|
|
// Value of flag
|
|
|
|
value []string = []
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
|
2021-01-20 22:15:48 +01:00
|
|
|
// get_all_found returns an array of all `Flag`s found in the command parameters
|
2020-07-02 11:10:03 +02:00
|
|
|
pub fn (flags []Flag) get_all_found() []Flag {
|
|
|
|
return flags.filter(it.found)
|
|
|
|
}
|
|
|
|
|
2021-01-20 22:15:48 +01:00
|
|
|
// get_bool returns `true` if the flag is set.
|
|
|
|
// get_bool returns an error if the `FlagType` is not boolean.
|
2020-07-02 11:10:03 +02:00
|
|
|
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
|
|
|
}
|
2021-03-19 12:09:56 +01:00
|
|
|
|
|
|
|
val := flag.get_value_or_default_value()
|
|
|
|
|
|
|
|
return val.len > 0 && val[0] == 'true'
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
|
2021-01-20 22:15:48 +01:00
|
|
|
// get_bool returns `true` if the flag specified in `name` is set.
|
|
|
|
// get_bool returns an error if the `FlagType` is not boolean.
|
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()
|
|
|
|
}
|
|
|
|
|
2021-01-20 22:15:48 +01:00
|
|
|
// get_int returns the `int` value argument of the flag.
|
|
|
|
// get_int returns an error if the `FlagType` is not integer.
|
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
|
|
|
}
|
2021-01-22 18:03:02 +01:00
|
|
|
|
2021-03-19 12:09:56 +01:00
|
|
|
val := flag.get_value_or_default_value()
|
|
|
|
|
|
|
|
if val.len == 0 {
|
2021-01-22 18:03:02 +01:00
|
|
|
return 0
|
|
|
|
} else {
|
2021-03-19 12:09:56 +01:00
|
|
|
return val[0].int()
|
2021-01-22 18:03:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get_ints returns the array of `int` value argument of the flag specified in `name`.
|
|
|
|
// get_ints returns an error if the `FlagType` is not integer.
|
|
|
|
pub fn (flag Flag) get_ints() ?[]int {
|
2021-03-19 12:09:56 +01:00
|
|
|
if flag.flag != .int_array {
|
|
|
|
return error('$flag.name: Invalid flag type `$flag.flag`, expected `int_array`')
|
2021-01-22 18:03:02 +01:00
|
|
|
}
|
|
|
|
|
2021-03-19 12:09:56 +01:00
|
|
|
val := flag.get_value_or_default_value()
|
|
|
|
|
|
|
|
if val.len == 0 {
|
2021-01-22 18:03:02 +01:00
|
|
|
return []int{}
|
|
|
|
} else {
|
2021-03-19 12:09:56 +01:00
|
|
|
mut values := []int{}
|
2021-01-22 18:03:02 +01:00
|
|
|
|
2021-03-19 12:09:56 +01:00
|
|
|
for f in val {
|
|
|
|
values << f.int()
|
2021-01-22 18:03:02 +01:00
|
|
|
}
|
|
|
|
|
2021-03-19 12:09:56 +01:00
|
|
|
return values
|
2021-01-22 18:03:02 +01:00
|
|
|
}
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
|
2021-01-20 22:15:48 +01:00
|
|
|
// get_int returns the `int` value argument of the flag specified in `name`.
|
|
|
|
// get_int returns an error if the `FlagType` is not integer.
|
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()
|
|
|
|
}
|
|
|
|
|
2021-01-22 18:03:02 +01:00
|
|
|
// get_ints returns the array of `int` value argument of the flag specified in `name`.
|
|
|
|
// get_ints returns an error if the `FlagType` is not integer.
|
|
|
|
pub fn (flags []Flag) get_ints(name string) ?[]int {
|
|
|
|
flag := flags.get(name) ?
|
|
|
|
return flag.get_ints()
|
|
|
|
}
|
|
|
|
|
2021-01-20 22:15:48 +01:00
|
|
|
// get_float returns the `f64` value argument of the flag.
|
|
|
|
// get_float returns an error if the `FlagType` is not floating point.
|
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
|
|
|
}
|
2021-01-22 18:03:02 +01:00
|
|
|
|
2021-03-19 12:09:56 +01:00
|
|
|
val := flag.get_value_or_default_value()
|
|
|
|
|
|
|
|
if val.len == 0 {
|
2021-01-22 18:03:02 +01:00
|
|
|
return 0.0
|
|
|
|
} else {
|
2021-03-19 12:09:56 +01:00
|
|
|
return val[0].f64()
|
2021-01-22 18:03:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get_floats returns the `f64` value argument of the flag.
|
|
|
|
// get_floats returns an error if the `FlagType` is not floating point.
|
|
|
|
pub fn (flag Flag) get_floats() ?[]f64 {
|
2021-03-19 12:09:56 +01:00
|
|
|
if flag.flag != .float_array {
|
|
|
|
return error('$flag.name: Invalid flag type `$flag.flag`, expected `float_array`')
|
2021-01-22 18:03:02 +01:00
|
|
|
}
|
|
|
|
|
2021-03-19 12:09:56 +01:00
|
|
|
val := flag.get_value_or_default_value()
|
|
|
|
|
|
|
|
if val.len == 0 {
|
2021-01-22 18:03:02 +01:00
|
|
|
return []f64{}
|
|
|
|
} else {
|
2021-03-19 12:09:56 +01:00
|
|
|
mut values := []f64{}
|
2021-01-22 18:03:02 +01:00
|
|
|
|
2021-03-19 12:09:56 +01:00
|
|
|
for f in val {
|
|
|
|
values << f.f64()
|
2021-01-22 18:03:02 +01:00
|
|
|
}
|
|
|
|
|
2021-03-19 12:09:56 +01:00
|
|
|
return values
|
2021-01-22 18:03:02 +01:00
|
|
|
}
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
|
2021-01-20 22:15:48 +01:00
|
|
|
// get_float returns the `f64` value argument of the flag specified in `name`.
|
|
|
|
// get_float returns an error if the `FlagType` is not floating point.
|
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()
|
|
|
|
}
|
|
|
|
|
2021-01-22 18:03:02 +01:00
|
|
|
// get_floats returns the array of `f64` value argument of the flag specified in `name`.
|
|
|
|
// get_floats returns an error if the `FlagType` is not floating point.
|
|
|
|
pub fn (flags []Flag) get_floats(name string) ?[]f64 {
|
|
|
|
flag := flags.get(name) ?
|
|
|
|
return flag.get_floats()
|
|
|
|
}
|
|
|
|
|
2021-01-20 22:15:48 +01:00
|
|
|
// get_string returns the `string` value argument of the flag.
|
|
|
|
// get_string returns an error if the `FlagType` is not string.
|
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
|
|
|
}
|
2021-01-22 18:03:02 +01:00
|
|
|
|
2021-03-19 12:09:56 +01:00
|
|
|
val := flag.get_value_or_default_value()
|
|
|
|
|
|
|
|
if val.len == 0 {
|
2021-01-22 18:03:02 +01:00
|
|
|
return ''
|
|
|
|
} else {
|
2021-03-19 12:09:56 +01:00
|
|
|
return val[0]
|
2021-01-22 18:03:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get_strings returns the array of `string` value argument of the flag.
|
|
|
|
// get_strings returns an error if the `FlagType` is not string.
|
|
|
|
pub fn (flag Flag) get_strings() ?[]string {
|
2021-03-19 12:09:56 +01:00
|
|
|
if flag.flag != .string_array {
|
|
|
|
return error('$flag.name: Invalid flag type `$flag.flag`, expected `string_array`')
|
2021-01-22 18:03:02 +01:00
|
|
|
}
|
|
|
|
|
2021-03-19 12:09:56 +01:00
|
|
|
val := flag.get_value_or_default_value()
|
|
|
|
|
|
|
|
if val.len == 0 {
|
2021-01-22 18:03:02 +01:00
|
|
|
return []string{}
|
|
|
|
} else {
|
2021-03-19 12:09:56 +01:00
|
|
|
return val
|
2021-01-22 18:03:02 +01:00
|
|
|
}
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
|
2021-01-20 22:15:48 +01:00
|
|
|
// get_string returns the `string` value argument of the flag specified in `name`.
|
|
|
|
// get_string returns an error if the `FlagType` is not string.
|
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()
|
|
|
|
}
|
|
|
|
|
2021-01-22 18:03:02 +01:00
|
|
|
// get_strings returns the `string` value argument of the flag specified in `name`.
|
|
|
|
// get_strings returns an error if the `FlagType` is not string.
|
|
|
|
pub fn (flags []Flag) get_strings(name string) ?[]string {
|
|
|
|
flag := flags.get(name) ?
|
|
|
|
return flag.get_strings()
|
|
|
|
}
|
|
|
|
|
2021-01-20 22:15:48 +01:00
|
|
|
// parse parses flag values from arguments and return
|
|
|
|
// an array of arguments with all consumed elements 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 {
|
2021-03-19 12:09:56 +01:00
|
|
|
if flag.value.len > 0 && flag.flag != .int_array && flag.flag != .float_array
|
|
|
|
&& flag.flag != .string_array {
|
2021-01-22 18:03:02 +01:00
|
|
|
return error('The argument `$flag.name` accept only one value!')
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 22:15:48 +01:00
|
|
|
// matches returns `true` if first arg in `args` matches this flag.
|
2020-07-18 13:24:10 +02:00
|
|
|
fn (mut flag Flag) matches(args []string, with_abbrev bool) bool {
|
|
|
|
if with_abbrev {
|
2021-01-23 09:33:22 +01:00
|
|
|
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='))
|
2020-07-18 13:24:10 +02:00
|
|
|
} else {
|
2021-01-23 09:33:22 +01:00
|
|
|
return (flag.name != '' && args[0] == '-$flag.name')
|
|
|
|
|| (flag.name != '' && args[0].starts_with('-$flag.name='))
|
2020-07-18 13:24:10 +02:00
|
|
|
}
|
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('=') {
|
2021-01-22 18:03:02 +01:00
|
|
|
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 {
|
2021-01-22 18:03:02 +01:00
|
|
|
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('=') {
|
2021-01-22 18:03:02 +01:00
|
|
|
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'] {
|
2021-01-22 18:03:02 +01:00
|
|
|
flag.value = [args[1]]
|
2019-11-30 10:44:40 +01:00
|
|
|
return args[2..]
|
|
|
|
}
|
|
|
|
}
|
2021-01-22 18:03:02 +01:00
|
|
|
// In fact bool cannot be multiple
|
|
|
|
flag.value = ['true']
|
2019-11-30 10:44:40 +01:00
|
|
|
return args[1..]
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
|
2021-01-20 22:15:48 +01:00
|
|
|
// get returns the `Flag` matching `name` or an error
|
|
|
|
// if it can't be found.
|
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
|
|
|
}
|
2021-03-19 12:09:56 +01:00
|
|
|
|
|
|
|
// Check if value is set by command line option. If not, return default value.
|
|
|
|
fn (flag Flag) get_value_or_default_value() []string {
|
|
|
|
if flag.value.len == 0 && flag.default_value.len > 0 {
|
|
|
|
// If default value is set and no value provide, use default value.
|
|
|
|
return flag.default_value
|
|
|
|
} else {
|
|
|
|
return flag.value
|
|
|
|
}
|
|
|
|
}
|