2019-09-28 14:17:16 +03:00
|
|
|
module flag
|
2019-07-12 16:47:27 +03:00
|
|
|
|
2019-09-14 23:54:14 +03:00
|
|
|
// data object storing information about a defined flag
|
2019-12-01 12:50:13 +03:00
|
|
|
pub struct Flag {
|
2020-10-15 16:17:52 +03:00
|
|
|
pub:
|
2019-12-16 19:03:38 +03:00
|
|
|
name string // name as it appears on command line
|
2021-01-03 23:10:25 +03:00
|
|
|
abbr byte // shortcut
|
2019-12-16 19:03:38 +03:00
|
|
|
usage string // help message
|
|
|
|
val_desc string // something like '<arg>' that appears in usage,
|
|
|
|
// and also the default value, when the flag is not given
|
2019-07-12 16:47:27 +03:00
|
|
|
}
|
|
|
|
|
2020-01-08 23:45:47 +03:00
|
|
|
pub fn (f Flag) str() string {
|
2020-10-15 16:17:52 +03:00
|
|
|
return '' + ' flag:\n' + ' name: $f.name\n' + ' abbr: $f.abbr\n' +
|
|
|
|
' usag: $f.usage\n' + ' desc: $f.val_desc'
|
2020-01-08 23:45:47 +03:00
|
|
|
}
|
2020-10-15 16:17:52 +03:00
|
|
|
|
2020-01-08 23:45:47 +03:00
|
|
|
pub fn (af []Flag) str() string {
|
2020-04-26 14:49:31 +03:00
|
|
|
mut res := []string{}
|
2020-01-08 23:45:47 +03:00
|
|
|
res << '\n []Flag = ['
|
|
|
|
for f in af {
|
|
|
|
res << f.str()
|
|
|
|
}
|
|
|
|
res << ' ]'
|
|
|
|
return res.join('\n')
|
|
|
|
}
|
2020-10-15 16:17:52 +03:00
|
|
|
|
2019-12-16 19:03:38 +03:00
|
|
|
//
|
2019-12-01 12:50:13 +03:00
|
|
|
pub struct FlagParser {
|
2020-10-15 16:17:52 +03:00
|
|
|
pub mut:
|
|
|
|
args []string // the arguments to be parsed
|
|
|
|
max_free_args int
|
|
|
|
flags []Flag // registered flags
|
2019-12-16 19:03:38 +03:00
|
|
|
application_name string
|
|
|
|
application_version string
|
|
|
|
application_description string
|
2020-10-15 16:17:52 +03:00
|
|
|
min_free_args int
|
2019-12-16 19:03:38 +03:00
|
|
|
args_description string
|
2019-07-12 16:47:27 +03:00
|
|
|
}
|
|
|
|
|
2019-12-01 12:50:13 +03:00
|
|
|
pub const (
|
2019-12-16 19:03:38 +03:00
|
|
|
// used for formating usage message
|
2020-10-15 16:17:52 +03:00
|
|
|
space = ' '
|
|
|
|
underline = '-----------------------------------------------'
|
2020-05-22 18:36:09 +03:00
|
|
|
max_args_number = 4048
|
2019-09-28 14:17:16 +03:00
|
|
|
)
|
|
|
|
|
2019-07-12 16:47:27 +03:00
|
|
|
// create a new flag set for parsing command line arguments
|
2019-08-05 08:50:24 +03:00
|
|
|
// TODO use INT_MAX some how
|
2019-07-12 16:47:27 +03:00
|
|
|
pub fn new_flag_parser(args []string) &FlagParser {
|
2020-10-15 16:17:52 +03:00
|
|
|
return &FlagParser{
|
|
|
|
args: args.clone()
|
|
|
|
max_free_args: max_args_number
|
|
|
|
}
|
2019-07-12 16:47:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// change the application name to be used in 'usage' output
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut fs FlagParser) application(name string) {
|
2019-12-30 18:38:32 +03:00
|
|
|
fs.application_name = name
|
2019-07-12 16:47:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// change the application version to be used in 'usage' output
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut fs FlagParser) version(vers string) {
|
2019-12-30 18:38:32 +03:00
|
|
|
fs.application_version = vers
|
2019-07-12 16:47:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// change the application version to be used in 'usage' output
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut fs FlagParser) description(desc string) {
|
2019-12-30 18:38:32 +03:00
|
|
|
fs.application_description = desc
|
2019-07-12 16:47:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// in most cases you do not need the first argv for flag parsing
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut fs FlagParser) skip_executable() {
|
2019-12-16 19:03:38 +03:00
|
|
|
fs.args.delete(0)
|
2019-07-12 16:47:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// private helper to register a flag
|
2020-05-17 14:51:18 +03:00
|
|
|
fn (mut fs FlagParser) add_flag(name string, abbr byte, usage string, desc string) {
|
2019-12-16 19:03:38 +03:00
|
|
|
fs.flags << Flag{
|
2020-10-15 16:17:52 +03:00
|
|
|
name: name
|
|
|
|
abbr: abbr
|
|
|
|
usage: usage
|
2019-12-30 18:38:32 +03:00
|
|
|
val_desc: desc
|
2019-12-16 19:03:38 +03:00
|
|
|
}
|
2019-07-12 16:47:27 +03:00
|
|
|
}
|
|
|
|
|
2019-12-16 19:03:38 +03:00
|
|
|
// private: general parsing a single argument
|
2020-10-15 16:17:52 +03:00
|
|
|
// - search args for existence
|
|
|
|
// if true
|
|
|
|
// extract the defined value as string
|
|
|
|
// else
|
|
|
|
// return an (dummy) error -> argument is not defined
|
2019-07-12 16:47:27 +03:00
|
|
|
//
|
2020-10-15 16:17:52 +03:00
|
|
|
// - the name, usage are registered
|
|
|
|
// - found arguments and corresponding values are removed from args list
|
2020-05-17 14:51:18 +03:00
|
|
|
fn (mut fs FlagParser) parse_value(longhand string, shorthand byte) []string {
|
2019-12-16 19:03:38 +03:00
|
|
|
full := '--$longhand'
|
2020-04-26 14:49:31 +03:00
|
|
|
mut found_entries := []string{}
|
|
|
|
mut to_delete := []int{}
|
2019-12-16 19:03:38 +03:00
|
|
|
mut should_skip_one := false
|
|
|
|
for i, arg in fs.args {
|
|
|
|
if should_skip_one {
|
|
|
|
should_skip_one = false
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if arg == '--' {
|
2020-10-15 16:17:52 +03:00
|
|
|
// End of input. We're done here.
|
2019-12-16 19:03:38 +03:00
|
|
|
break
|
|
|
|
}
|
2020-01-08 23:45:47 +03:00
|
|
|
if arg[0] != `-` {
|
|
|
|
continue
|
|
|
|
}
|
2020-10-15 16:17:52 +03:00
|
|
|
if (arg.len == 2 && arg[0] == `-` && arg[1] == shorthand) || arg == full {
|
|
|
|
if i + 1 >= fs.args.len {
|
2020-09-25 16:31:35 +03:00
|
|
|
return []
|
2019-12-16 19:03:38 +03:00
|
|
|
}
|
2020-10-15 16:17:52 +03:00
|
|
|
nextarg := fs.args[i + 1]
|
2019-12-16 19:03:38 +03:00
|
|
|
if nextarg.len > 2 && nextarg[..2] == '--' {
|
2020-10-15 16:17:52 +03:00
|
|
|
// It could be end of input (--) or another argument (--abc).
|
|
|
|
// Both are invalid so die.
|
2020-09-25 16:31:35 +03:00
|
|
|
return []
|
2019-12-16 19:03:38 +03:00
|
|
|
}
|
2020-10-15 16:17:52 +03:00
|
|
|
found_entries << fs.args[i + 1]
|
2019-12-16 19:03:38 +03:00
|
|
|
to_delete << i
|
2020-10-15 16:17:52 +03:00
|
|
|
to_delete << i + 1
|
2019-12-16 19:03:38 +03:00
|
|
|
should_skip_one = true
|
|
|
|
continue
|
|
|
|
}
|
2020-10-15 16:17:52 +03:00
|
|
|
if arg.len > full.len + 1 && arg[..full.len + 1] == '$full=' {
|
|
|
|
found_entries << arg[full.len + 1..]
|
2019-12-16 19:03:38 +03:00
|
|
|
to_delete << i
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i, del in to_delete {
|
2020-10-15 16:17:52 +03:00
|
|
|
// i entrys are deleted so it's shifted left i times.
|
2019-12-16 19:03:38 +03:00
|
|
|
fs.args.delete(del - i)
|
|
|
|
}
|
|
|
|
return found_entries
|
2019-07-12 16:47:27 +03:00
|
|
|
}
|
|
|
|
|
2019-12-16 19:03:38 +03:00
|
|
|
// special parsing for bool values
|
2019-07-12 16:47:27 +03:00
|
|
|
// see also: parse_value
|
2019-12-16 19:03:38 +03:00
|
|
|
//
|
2019-07-12 16:47:27 +03:00
|
|
|
// special: it is allowed to define bool flags without value
|
|
|
|
// -> '--flag' is parsed as true
|
|
|
|
// -> '--flag' is equal to '--flag=true'
|
2020-05-17 14:51:18 +03:00
|
|
|
fn (mut fs FlagParser) parse_bool_value(longhand string, shorthand byte) ?string {
|
2019-12-16 19:03:38 +03:00
|
|
|
full := '--$longhand'
|
|
|
|
for i, arg in fs.args {
|
|
|
|
if arg == '--' {
|
2020-10-15 16:17:52 +03:00
|
|
|
// End of input. We're done.
|
2019-12-16 19:03:38 +03:00
|
|
|
break
|
|
|
|
}
|
2020-01-08 23:45:47 +03:00
|
|
|
if arg.len == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if arg[0] != `-` {
|
|
|
|
continue
|
|
|
|
}
|
2020-10-15 16:17:52 +03:00
|
|
|
if (arg.len == 2 && arg[0] == `-` && arg[1] == shorthand) || arg == full {
|
|
|
|
if fs.args.len > i + 1 && (fs.args[i + 1] in ['true', 'false']) {
|
|
|
|
val := fs.args[i + 1]
|
|
|
|
fs.args.delete(i + 1)
|
2019-12-16 19:03:38 +03:00
|
|
|
fs.args.delete(i)
|
|
|
|
return val
|
|
|
|
} else {
|
|
|
|
fs.args.delete(i)
|
|
|
|
return 'true'
|
|
|
|
}
|
|
|
|
}
|
2020-10-15 16:17:52 +03:00
|
|
|
if arg.len > full.len + 1 && arg[..full.len + 1] == '$full=' {
|
2019-12-16 19:03:38 +03:00
|
|
|
// Flag abc=true
|
2020-10-15 16:17:52 +03:00
|
|
|
val := arg[full.len + 1..]
|
2019-12-16 19:03:38 +03:00
|
|
|
fs.args.delete(i)
|
|
|
|
return val
|
|
|
|
}
|
2020-09-24 15:50:44 +03:00
|
|
|
if arg.len > 1 && arg[0] == `-` && arg[1] != `-` && arg.index_byte(shorthand) != -1 {
|
2019-12-16 19:03:38 +03:00
|
|
|
// -abc is equivalent to -a -b -c
|
|
|
|
return 'true'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return error("parameter '$longhand' not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// bool_opt returns an optional that returns the value associated with the flag.
|
|
|
|
// In the situation that the flag was not provided, it returns null.
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut fs FlagParser) bool_opt(name string, abbr byte, usage string) ?bool {
|
2019-12-30 18:38:32 +03:00
|
|
|
fs.add_flag(name, abbr, usage, '<bool>')
|
2020-12-21 23:24:10 +03:00
|
|
|
parsed := fs.parse_bool_value(name, abbr) or { return error("parameter '$name' not provided") }
|
2019-12-16 19:03:38 +03:00
|
|
|
return parsed == 'true'
|
2019-07-12 16:47:27 +03:00
|
|
|
}
|
|
|
|
|
2019-12-16 19:03:38 +03:00
|
|
|
// defining and parsing a bool flag
|
2020-10-15 16:17:52 +03:00
|
|
|
// if defined
|
|
|
|
// the value is returned (true/false)
|
|
|
|
// else
|
|
|
|
// the default value is returned
|
2019-12-30 18:38:32 +03:00
|
|
|
// version with abbr
|
2020-10-15 16:17:52 +03:00
|
|
|
// TODO error handling for invalid string to bool conversion
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut fs FlagParser) bool(name string, abbr byte, bdefault bool, usage string) bool {
|
2020-12-21 23:24:10 +03:00
|
|
|
value := fs.bool_opt(name, abbr, usage) or { return bdefault }
|
2019-12-16 19:03:38 +03:00
|
|
|
return value
|
2019-07-12 16:47:27 +03:00
|
|
|
}
|
|
|
|
|
2019-12-16 19:03:38 +03:00
|
|
|
// int_multi returns all instances of values associated with the flags provided
|
|
|
|
// In the case that none were found, it returns an empty array.
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut fs FlagParser) int_multi(name string, abbr byte, usage string) []int {
|
2019-12-30 18:38:32 +03:00
|
|
|
fs.add_flag(name, abbr, usage, '<multiple ints>')
|
|
|
|
parsed := fs.parse_value(name, abbr)
|
2020-04-26 14:49:31 +03:00
|
|
|
mut value := []int{}
|
2019-12-16 19:03:38 +03:00
|
|
|
for val in parsed {
|
|
|
|
value << val.int()
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
// int_opt returns an optional that returns the value associated with the flag.
|
|
|
|
// In the situation that the flag was not provided, it returns null.
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut fs FlagParser) int_opt(name string, abbr byte, usage string) ?int {
|
2019-12-30 18:38:32 +03:00
|
|
|
fs.add_flag(name, abbr, usage, '<int>')
|
|
|
|
parsed := fs.parse_value(name, abbr)
|
2019-12-16 19:03:38 +03:00
|
|
|
if parsed.len == 0 {
|
2019-12-30 18:38:32 +03:00
|
|
|
return error("parameter '$name' not provided")
|
2019-12-16 19:03:38 +03:00
|
|
|
}
|
|
|
|
return parsed[0].int()
|
2019-08-07 17:52:10 +03:00
|
|
|
}
|
|
|
|
|
2019-12-16 19:03:38 +03:00
|
|
|
// defining and parsing an int flag
|
2020-10-15 16:17:52 +03:00
|
|
|
// if defined
|
|
|
|
// the value is returned (int)
|
|
|
|
// else
|
|
|
|
// the default value is returned
|
2019-12-30 18:38:32 +03:00
|
|
|
// version with abbr
|
2020-10-15 16:17:52 +03:00
|
|
|
// TODO error handling for invalid string to int conversion
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut fs FlagParser) int(name string, abbr byte, idefault int, usage string) int {
|
2020-12-21 23:24:10 +03:00
|
|
|
value := fs.int_opt(name, abbr, usage) or { return idefault }
|
2019-12-16 19:03:38 +03:00
|
|
|
return value
|
2019-07-12 16:47:27 +03:00
|
|
|
}
|
|
|
|
|
2019-12-16 19:03:38 +03:00
|
|
|
// float_multi returns all instances of values associated with the flags provided
|
|
|
|
// In the case that none were found, it returns an empty array.
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut fs FlagParser) float_multi(name string, abbr byte, usage string) []f64 {
|
2019-12-30 18:38:32 +03:00
|
|
|
fs.add_flag(name, abbr, usage, '<multiple floats>')
|
|
|
|
parsed := fs.parse_value(name, abbr)
|
2020-04-26 14:49:31 +03:00
|
|
|
mut value := []f64{}
|
2019-12-16 19:03:38 +03:00
|
|
|
for val in parsed {
|
2020-04-21 14:44:17 +03:00
|
|
|
value << val.f64()
|
2019-12-16 19:03:38 +03:00
|
|
|
}
|
|
|
|
return value
|
2019-08-07 17:52:10 +03:00
|
|
|
}
|
|
|
|
|
2019-12-16 19:03:38 +03:00
|
|
|
// float_opt returns an optional that returns the value associated with the flag.
|
|
|
|
// In the situation that the flag was not provided, it returns null.
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut fs FlagParser) float_opt(name string, abbr byte, usage string) ?f64 {
|
2019-12-30 18:38:32 +03:00
|
|
|
fs.add_flag(name, abbr, usage, '<float>')
|
|
|
|
parsed := fs.parse_value(name, abbr)
|
2019-12-16 19:03:38 +03:00
|
|
|
if parsed.len == 0 {
|
2019-12-30 18:38:32 +03:00
|
|
|
return error("parameter '$name' not provided")
|
2019-12-16 19:03:38 +03:00
|
|
|
}
|
2020-04-21 14:44:17 +03:00
|
|
|
return parsed[0].f64()
|
2019-12-16 19:03:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// defining and parsing a float flag
|
2020-10-15 16:17:52 +03:00
|
|
|
// if defined
|
|
|
|
// the value is returned (float)
|
|
|
|
// else
|
|
|
|
// the default value is returned
|
2019-12-30 18:38:32 +03:00
|
|
|
// version with abbr
|
2020-10-15 16:17:52 +03:00
|
|
|
// TODO error handling for invalid string to float conversion
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut fs FlagParser) float(name string, abbr byte, fdefault f64, usage string) f64 {
|
2020-12-21 23:24:10 +03:00
|
|
|
value := fs.float_opt(name, abbr, usage) or { return fdefault }
|
2019-12-16 19:03:38 +03:00
|
|
|
return value
|
2019-07-12 16:47:27 +03:00
|
|
|
}
|
|
|
|
|
2019-12-16 19:03:38 +03:00
|
|
|
// string_multi returns all instances of values associated with the flags provided
|
|
|
|
// In the case that none were found, it returns an empty array.
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut fs FlagParser) string_multi(name string, abbr byte, usage string) []string {
|
2020-05-26 15:27:01 +03:00
|
|
|
fs.add_flag(name, abbr, usage, '<multiple strings>')
|
2019-12-30 18:38:32 +03:00
|
|
|
return fs.parse_value(name, abbr)
|
2019-08-07 17:52:10 +03:00
|
|
|
}
|
|
|
|
|
2019-12-16 19:03:38 +03:00
|
|
|
// string_opt returns an optional that returns the value associated with the flag.
|
|
|
|
// In the situation that the flag was not provided, it returns null.
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut fs FlagParser) string_opt(name string, abbr byte, usage string) ?string {
|
2019-12-30 18:38:32 +03:00
|
|
|
fs.add_flag(name, abbr, usage, '<string>')
|
|
|
|
parsed := fs.parse_value(name, abbr)
|
2019-12-16 19:03:38 +03:00
|
|
|
if parsed.len == 0 {
|
2019-12-30 18:38:32 +03:00
|
|
|
return error("parameter '$name' not provided")
|
2019-12-16 19:03:38 +03:00
|
|
|
}
|
|
|
|
return parsed[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// defining and parsing a string flag
|
2020-10-15 16:17:52 +03:00
|
|
|
// if defined
|
|
|
|
// the value is returned (string)
|
|
|
|
// else
|
|
|
|
// the default value is returned
|
2019-12-30 18:38:32 +03:00
|
|
|
// version with abbr
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut fs FlagParser) string(name string, abbr byte, sdefault string, usage string) string {
|
2020-12-21 23:24:10 +03:00
|
|
|
value := fs.string_opt(name, abbr, usage) or { return sdefault }
|
2019-12-16 19:03:38 +03:00
|
|
|
return value
|
2019-07-12 16:47:27 +03:00
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut fs FlagParser) limit_free_args_to_at_least(n int) {
|
2020-05-22 18:36:09 +03:00
|
|
|
if n > max_args_number {
|
|
|
|
panic('flag.limit_free_args_to_at_least expect n to be smaller than $max_args_number')
|
2019-12-16 19:03:38 +03:00
|
|
|
}
|
2019-09-28 14:17:16 +03:00
|
|
|
if n <= 0 {
|
2019-12-16 19:03:38 +03:00
|
|
|
panic('flag.limit_free_args_to_at_least expect n to be a positive number')
|
|
|
|
}
|
|
|
|
fs.min_free_args = n
|
2019-09-28 14:17:16 +03:00
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut fs FlagParser) limit_free_args_to_exactly(n int) {
|
2020-05-22 18:36:09 +03:00
|
|
|
if n > max_args_number {
|
|
|
|
panic('flag.limit_free_args_to_exactly expect n to be smaller than $max_args_number')
|
2019-12-16 19:03:38 +03:00
|
|
|
}
|
2019-09-28 14:17:16 +03:00
|
|
|
if n < 0 {
|
2019-12-16 19:03:38 +03:00
|
|
|
panic('flag.limit_free_args_to_exactly expect n to be a non negative number')
|
|
|
|
}
|
|
|
|
fs.min_free_args = n
|
|
|
|
fs.max_free_args = n
|
2019-09-28 14:17:16 +03:00
|
|
|
}
|
|
|
|
|
2019-08-05 08:50:24 +03:00
|
|
|
// this will cause an error in finalize() if free args are out of range
|
|
|
|
// (min, ..., max)
|
2020-10-15 16:17:52 +03:00
|
|
|
pub fn (mut fs FlagParser) limit_free_args(min int, max int) {
|
2019-12-16 19:03:38 +03:00
|
|
|
if min > max {
|
|
|
|
panic('flag.limit_free_args expect min < max, got $min >= $max')
|
|
|
|
}
|
|
|
|
fs.min_free_args = min
|
|
|
|
fs.max_free_args = max
|
2019-08-05 08:50:24 +03:00
|
|
|
}
|
|
|
|
|
2020-10-15 16:17:52 +03:00
|
|
|
pub fn (mut fs FlagParser) arguments_description(description string) {
|
2019-12-16 19:03:38 +03:00
|
|
|
fs.args_description = description
|
2019-09-28 14:17:16 +03:00
|
|
|
}
|
2019-07-12 16:47:27 +03:00
|
|
|
|
2019-12-16 19:03:38 +03:00
|
|
|
// collect all given information and
|
2019-07-12 16:47:27 +03:00
|
|
|
pub fn (fs FlagParser) usage() string {
|
2020-10-15 16:17:52 +03:00
|
|
|
positive_min_arg := (fs.min_free_args > 0)
|
|
|
|
positive_max_arg := (fs.max_free_args > 0 && fs.max_free_args != max_args_number)
|
|
|
|
no_arguments := (fs.min_free_args == 0 && fs.max_free_args == 0)
|
2019-12-16 19:03:38 +03:00
|
|
|
mut adesc := if fs.args_description.len > 0 { fs.args_description } else { '[ARGS]' }
|
2020-10-15 16:17:52 +03:00
|
|
|
if no_arguments {
|
|
|
|
adesc = ''
|
|
|
|
}
|
2019-12-16 19:03:38 +03:00
|
|
|
mut use := ''
|
2019-12-30 07:21:22 +03:00
|
|
|
if fs.application_version != '' {
|
|
|
|
use += '$fs.application_name $fs.application_version\n'
|
2020-05-22 18:36:09 +03:00
|
|
|
use += '$underline\n'
|
2019-12-30 07:21:22 +03:00
|
|
|
}
|
2020-10-15 16:17:52 +03:00
|
|
|
use += 'Usage: $fs.application_name [options] $adesc\n'
|
2019-12-16 19:03:38 +03:00
|
|
|
use += '\n'
|
|
|
|
if fs.application_description != '' {
|
|
|
|
use += 'Description:\n'
|
|
|
|
use += '$fs.application_description'
|
|
|
|
use += '\n\n'
|
|
|
|
}
|
|
|
|
// show a message about the [ARGS]:
|
|
|
|
if positive_min_arg || positive_max_arg || no_arguments {
|
|
|
|
if no_arguments {
|
|
|
|
use += 'This application does not expect any arguments\n\n'
|
|
|
|
goto end_of_arguments_handling
|
|
|
|
}
|
2020-10-15 16:17:52 +03:00
|
|
|
mut s := []string{}
|
|
|
|
if positive_min_arg {
|
|
|
|
s << 'at least $fs.min_free_args'
|
|
|
|
}
|
|
|
|
if positive_max_arg {
|
|
|
|
s << 'at most $fs.max_free_args'
|
|
|
|
}
|
2019-12-16 19:03:38 +03:00
|
|
|
if positive_min_arg && positive_max_arg && fs.min_free_args == fs.max_free_args {
|
|
|
|
s = ['exactly $fs.min_free_args']
|
|
|
|
}
|
|
|
|
sargs := s.join(' and ')
|
|
|
|
use += 'The arguments should be $sargs in number.\n\n'
|
|
|
|
}
|
|
|
|
end_of_arguments_handling:
|
|
|
|
if fs.flags.len > 0 {
|
|
|
|
use += 'Options:\n'
|
|
|
|
for f in fs.flags {
|
2020-04-27 23:53:26 +03:00
|
|
|
mut onames := []string{}
|
2019-12-30 18:38:32 +03:00
|
|
|
if f.abbr != 0 {
|
2021-01-05 20:59:51 +03:00
|
|
|
onames << '-$f.abbr.ascii_str()'
|
2019-12-30 07:21:22 +03:00
|
|
|
}
|
2019-12-30 18:38:32 +03:00
|
|
|
if f.name != '' {
|
|
|
|
if !f.val_desc.contains('<bool>') {
|
2020-10-15 16:17:52 +03:00
|
|
|
onames << '--$f.name $f.val_desc'
|
|
|
|
} else {
|
|
|
|
onames << '--$f.name'
|
2019-12-30 18:38:32 +03:00
|
|
|
}
|
2019-12-30 07:21:22 +03:00
|
|
|
}
|
2019-12-30 18:38:32 +03:00
|
|
|
option_names := ' ' + onames.join(', ')
|
2020-04-11 17:24:21 +03:00
|
|
|
mut xspace := ''
|
2020-10-15 16:17:52 +03:00
|
|
|
if option_names.len > space.len - 2 {
|
|
|
|
xspace = '\n$space'
|
2019-12-16 19:03:38 +03:00
|
|
|
} else {
|
2020-04-08 19:49:32 +03:00
|
|
|
xspace = space[option_names.len..]
|
2019-12-16 19:03:38 +03:00
|
|
|
}
|
2020-10-15 16:17:52 +03:00
|
|
|
use += '$option_names$xspace$f.usage\n'
|
2019-12-16 19:03:38 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-16 12:57:34 +03:00
|
|
|
return use.replace('- ,', ' ')
|
2019-07-12 16:47:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// finalize argument parsing -> call after all arguments are defined
|
|
|
|
//
|
|
|
|
// all remaining arguments are returned in the same order they are defined on
|
2019-09-14 23:54:14 +03:00
|
|
|
// command line
|
2019-07-12 16:47:27 +03:00
|
|
|
//
|
2019-12-16 19:03:38 +03:00
|
|
|
// if additional flag are found (things starting with '--') an error is returned
|
2019-07-12 16:47:27 +03:00
|
|
|
// error handling is up to the application developer
|
|
|
|
pub fn (fs FlagParser) finalize() ?[]string {
|
2019-12-16 19:03:38 +03:00
|
|
|
for a in fs.args {
|
|
|
|
if a.len >= 2 && a[..2] == '--' {
|
2020-10-15 16:17:52 +03:00
|
|
|
return error("Unknown argument \'${a[2..]}\'")
|
2019-12-16 19:03:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if fs.args.len < fs.min_free_args && fs.min_free_args > 0 {
|
2020-10-15 16:17:52 +03:00
|
|
|
return error('Expected at least $fs.min_free_args arguments, but given $fs.args.len')
|
2019-12-16 19:03:38 +03:00
|
|
|
}
|
|
|
|
if fs.args.len > fs.max_free_args && fs.max_free_args > 0 {
|
2020-10-15 16:17:52 +03:00
|
|
|
return error('Expected at most $fs.max_free_args arguments, but given $fs.args.len')
|
2019-12-16 19:03:38 +03:00
|
|
|
}
|
|
|
|
if fs.args.len > 0 && fs.max_free_args == 0 && fs.min_free_args == 0 {
|
|
|
|
return error('Expected no arguments, but given $fs.args.len')
|
|
|
|
}
|
|
|
|
return fs.args
|
2019-07-12 16:47:27 +03:00
|
|
|
}
|