2019-09-28 14:17:16 +03:00
|
|
|
module flag
|
2019-07-12 16:47:27 +03:00
|
|
|
|
|
|
|
// module flag for command-line flag parsing
|
|
|
|
//
|
|
|
|
// - parsing flags like '--flag' or '--stuff=things' or '--things stuff'
|
|
|
|
// - handles bool, int, float and string args
|
2019-09-14 23:54:14 +03:00
|
|
|
// - is able to print usage
|
2019-12-16 19:03:38 +03:00
|
|
|
// - handled unknown arguments as error
|
|
|
|
//
|
2019-07-12 16:47:27 +03:00
|
|
|
// Usage example:
|
|
|
|
//
|
|
|
|
// ```v
|
|
|
|
// module main
|
2019-12-16 19:03:38 +03:00
|
|
|
//
|
2019-07-12 16:47:27 +03:00
|
|
|
// import os
|
|
|
|
// import flag
|
2019-12-16 19:03:38 +03:00
|
|
|
//
|
2019-07-12 16:47:27 +03:00
|
|
|
// fn main() {
|
|
|
|
// mut fp := flag.new_flag_parser(os.args)
|
|
|
|
// fp.application('flag_example_tool')
|
|
|
|
// fp.version('v0.0.0')
|
|
|
|
// fp.description('This tool is only designed to show how the flag lib is working')
|
2019-12-16 19:03:38 +03:00
|
|
|
//
|
2019-07-12 16:47:27 +03:00
|
|
|
// fp.skip_executable()
|
2019-12-16 19:03:38 +03:00
|
|
|
//
|
2019-07-12 16:47:27 +03:00
|
|
|
// an_int := fp.int('an_int', 666, 'some int to define 666 is default')
|
|
|
|
// a_bool := fp.bool('a_bool', false, 'some \'real\' flag')
|
|
|
|
// a_float := fp.float('a_float', 1.0, 'also floats')
|
|
|
|
// a_string := fp.string('a_string', 'no text', 'finally, some text')
|
2019-12-16 19:03:38 +03:00
|
|
|
//
|
2019-07-12 16:47:27 +03:00
|
|
|
// additional_args := fp.finalize() or {
|
|
|
|
// eprintln(err)
|
|
|
|
// println(fp.usage())
|
|
|
|
// return
|
|
|
|
// }
|
2019-12-16 19:03:38 +03:00
|
|
|
//
|
2019-07-12 16:47:27 +03:00
|
|
|
// println('
|
|
|
|
// an_int: $an_int
|
|
|
|
// a_bool: $a_bool
|
|
|
|
// a_float: $a_float
|
|
|
|
// a_string: \'$a_string\'
|
|
|
|
// ')
|
|
|
|
// println(additional_args.join_lines())
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
|
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 {
|
2019-12-16 19:03:38 +03:00
|
|
|
pub:
|
|
|
|
name string // name as it appears on command line
|
|
|
|
abbr byte // shortcut
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-12-16 19:03:38 +03:00
|
|
|
//
|
2019-12-01 12:50:13 +03:00
|
|
|
pub struct FlagParser {
|
2019-12-16 19:03:38 +03:00
|
|
|
pub mut:
|
|
|
|
args []string // the arguments to be parsed
|
|
|
|
flags []Flag // registered flags
|
2019-07-12 16:47:27 +03:00
|
|
|
|
2019-12-16 19:03:38 +03:00
|
|
|
application_name string
|
|
|
|
application_version string
|
|
|
|
application_description string
|
2019-08-05 08:50:24 +03:00
|
|
|
|
2019-12-16 19:03:38 +03:00
|
|
|
min_free_args int
|
|
|
|
max_free_args int
|
|
|
|
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
|
|
|
|
SPACE = ' '
|
|
|
|
UNDERLINE = '-----------------------------------------------'
|
|
|
|
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 {
|
2019-12-16 19:03:38 +03:00
|
|
|
return &FlagParser{args:args, max_free_args: MAX_ARGS_NUMBER}
|
2019-07-12 16:47:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// change the application name to be used in 'usage' output
|
|
|
|
pub fn (fs mut FlagParser) application(n string) {
|
2019-12-16 19:03:38 +03:00
|
|
|
fs.application_name = n
|
2019-07-12 16:47:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// change the application version to be used in 'usage' output
|
|
|
|
pub fn (fs mut FlagParser) version(n string) {
|
2019-12-16 19:03:38 +03:00
|
|
|
fs.application_version = n
|
2019-07-12 16:47:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// change the application version to be used in 'usage' output
|
|
|
|
pub fn (fs mut FlagParser) description(n string) {
|
2019-12-16 19:03:38 +03:00
|
|
|
fs.application_description = n
|
2019-07-12 16:47:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// in most cases you do not need the first argv for flag parsing
|
|
|
|
pub fn (fs mut 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
|
2019-08-07 17:52:10 +03:00
|
|
|
fn (fs mut FlagParser) add_flag(n string, a byte, u, vd string) {
|
2019-12-16 19:03:38 +03:00
|
|
|
fs.flags << Flag{
|
|
|
|
name: n,
|
|
|
|
abbr: a,
|
|
|
|
usage: u,
|
|
|
|
val_desc: vd
|
|
|
|
}
|
2019-07-12 16:47:27 +03:00
|
|
|
}
|
|
|
|
|
2019-12-16 19:03:38 +03:00
|
|
|
// private: general parsing a single argument
|
2019-09-14 23:54:14 +03:00
|
|
|
// - search args for existence
|
2019-07-12 16:47:27 +03:00
|
|
|
// if true
|
|
|
|
// extract the defined value as string
|
2019-12-16 19:03:38 +03:00
|
|
|
// else
|
2019-07-12 16:47:27 +03:00
|
|
|
// return an (dummy) error -> argument is not defined
|
|
|
|
//
|
|
|
|
// - the name, usage are registered
|
|
|
|
// - found arguments and corresponding values are removed from args list
|
2019-12-16 19:03:38 +03:00
|
|
|
fn (fs mut FlagParser) parse_value(longhand string, shorthand byte) []string {
|
|
|
|
full := '--$longhand'
|
|
|
|
mut found_entries := []string
|
|
|
|
mut to_delete := []int
|
|
|
|
mut should_skip_one := false
|
|
|
|
for i, arg in fs.args {
|
|
|
|
if should_skip_one {
|
|
|
|
should_skip_one = false
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if arg == '--' {
|
|
|
|
//End of input. We're done here.
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if arg == full || (arg[0] == `-` && arg[1] == shorthand && arg.len == 2) {
|
|
|
|
if i+1 > fs.args.len {
|
|
|
|
panic("Missing argument for '$longhand'")
|
|
|
|
}
|
|
|
|
nextarg := fs.args[i+1]
|
|
|
|
if nextarg.len > 2 && nextarg[..2] == '--' {
|
|
|
|
//It could be end of input (--) or another argument (--abc).
|
|
|
|
//Both are invalid so die.
|
|
|
|
panic("Missing argument for '$longhand'")
|
|
|
|
}
|
|
|
|
found_entries << fs.args[i+1]
|
|
|
|
to_delete << i
|
|
|
|
to_delete << i+1
|
|
|
|
should_skip_one = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if arg.len > full.len+1 && arg[..full.len+1] == '$full=' {
|
|
|
|
found_entries << arg[full.len+1..]
|
|
|
|
to_delete << i
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i, del in to_delete {
|
|
|
|
//i entrys are deleted so it's shifted left i times.
|
|
|
|
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'
|
2019-12-16 19:03:38 +03:00
|
|
|
fn (fs mut FlagParser) parse_bool_value(longhand string, shorthand byte) ?string {
|
|
|
|
full := '--$longhand'
|
|
|
|
for i, arg in fs.args {
|
|
|
|
if arg == '--' {
|
|
|
|
//End of input. We're done.
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if arg == full || (arg[0] == `-` && arg[1] == shorthand && arg.len == 2) {
|
|
|
|
if fs.args.len > i+1 && (fs.args[i+1] in ['true', 'false']) {
|
|
|
|
val := fs.args[i+1]
|
|
|
|
fs.args.delete(i+1)
|
|
|
|
fs.args.delete(i)
|
|
|
|
return val
|
|
|
|
} else {
|
|
|
|
fs.args.delete(i)
|
|
|
|
return 'true'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if arg.len > full.len+1 && arg[..full.len+1] == '$full=' {
|
|
|
|
// Flag abc=true
|
|
|
|
val := arg[full.len+1..]
|
|
|
|
fs.args.delete(i)
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
if arg[0] == `-` && arg.index_byte(shorthand) != -1 {
|
|
|
|
// -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.
|
|
|
|
pub fn (fs mut FlagParser) bool_opt(n string, a byte, u string) ?bool {
|
|
|
|
fs.add_flag(n, a, u, '<bool>')
|
|
|
|
parsed := fs.parse_bool_value(n, a) or {
|
|
|
|
return error("parameter '$n' not provided")
|
|
|
|
}
|
|
|
|
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
|
|
|
|
// if defined
|
2019-07-12 16:47:27 +03:00
|
|
|
// the value is returned (true/false)
|
2019-12-16 19:03:38 +03:00
|
|
|
// else
|
2019-07-12 16:47:27 +03:00
|
|
|
// the default value is returned
|
2019-08-07 17:52:10 +03:00
|
|
|
// version with abbreviation
|
2019-07-12 16:47:27 +03:00
|
|
|
//TODO error handling for invalid string to bool conversion
|
2019-08-07 17:52:10 +03:00
|
|
|
pub fn (fs mut FlagParser) bool_(n string, a byte, v bool, u string) bool {
|
2019-12-16 19:03:38 +03:00
|
|
|
value := fs.bool_opt(n, a, u) or {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
return value
|
2019-07-12 16:47:27 +03:00
|
|
|
}
|
|
|
|
|
2019-12-16 19:03:38 +03:00
|
|
|
// defining and parsing a bool flag
|
|
|
|
// if defined
|
2019-08-07 17:52:10 +03:00
|
|
|
// the value is returned (true/false)
|
2019-12-16 19:03:38 +03:00
|
|
|
// else
|
2019-08-07 17:52:10 +03:00
|
|
|
// the default value is returned
|
|
|
|
//TODO error handling for invalid string to bool conversion
|
|
|
|
pub fn (fs mut FlagParser) bool(n string, v bool, u string) bool {
|
2019-12-16 19:03:38 +03:00
|
|
|
return fs.bool_(n, `\0`, v, u)
|
|
|
|
}
|
|
|
|
|
|
|
|
// int_multi returns all instances of values associated with the flags provided
|
|
|
|
// In the case that none were found, it returns an empty array.
|
|
|
|
pub fn (fs mut FlagParser) int_multi(n string, a byte, u string) []int {
|
|
|
|
fs.add_flag(n, a, u, '<multiple ints>')
|
|
|
|
parsed := fs.parse_value(n, a)
|
|
|
|
mut value := []int
|
|
|
|
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.
|
|
|
|
pub fn (fs mut FlagParser) int_opt(n string, a byte, u string) ?int {
|
|
|
|
fs.add_flag(n, a, u, '<int>')
|
|
|
|
parsed := fs.parse_value(n, a)
|
|
|
|
if parsed.len == 0 {
|
|
|
|
return error("parameter '$n' not provided")
|
|
|
|
}
|
|
|
|
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
|
|
|
|
// if defined
|
2019-07-12 16:47:27 +03:00
|
|
|
// the value is returned (int)
|
2019-12-16 19:03:38 +03:00
|
|
|
// else
|
2019-07-12 16:47:27 +03:00
|
|
|
// the default value is returned
|
2019-08-07 17:52:10 +03:00
|
|
|
// version with abbreviation
|
2019-07-12 16:47:27 +03:00
|
|
|
//TODO error handling for invalid string to int conversion
|
2019-08-07 17:52:10 +03:00
|
|
|
pub fn (fs mut FlagParser) int_(n string, a byte, i int, u string) int {
|
2019-12-16 19:03:38 +03:00
|
|
|
value := fs.int_opt(n, a, u) or {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
return value
|
2019-07-12 16:47:27 +03:00
|
|
|
}
|
|
|
|
|
2019-12-16 19:03:38 +03:00
|
|
|
// defining and parsing an int flag
|
|
|
|
// if defined
|
2019-08-07 17:52:10 +03:00
|
|
|
// the value is returned (int)
|
2019-12-16 19:03:38 +03:00
|
|
|
// else
|
2019-08-07 17:52:10 +03:00
|
|
|
// the default value is returned
|
|
|
|
//TODO error handling for invalid string to int conversion
|
|
|
|
pub fn (fs mut FlagParser) int(n string, i int, u string) int {
|
2019-12-16 19:03:38 +03:00
|
|
|
return fs.int_(n, `\0`, i, u)
|
|
|
|
}
|
|
|
|
|
|
|
|
// float_multi returns all instances of values associated with the flags provided
|
|
|
|
// In the case that none were found, it returns an empty array.
|
|
|
|
pub fn (fs mut FlagParser) float_multi(n string, a byte, u string) []f32 {
|
|
|
|
fs.add_flag(n, a, u, '<multiple floats>')
|
|
|
|
parsed := fs.parse_value(n, a)
|
|
|
|
mut value := []f32
|
|
|
|
for val in parsed {
|
|
|
|
value << val.f32()
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
pub fn (fs mut FlagParser) float_opt(n string, a byte, u string) ?f32 {
|
|
|
|
fs.add_flag(n, a, u, '<float>')
|
|
|
|
parsed := fs.parse_value(n, a)
|
|
|
|
if parsed.len == 0 {
|
|
|
|
return error("parameter '$n' not provided")
|
|
|
|
}
|
|
|
|
return parsed[0].f32()
|
|
|
|
}
|
|
|
|
|
|
|
|
// defining and parsing a float flag
|
|
|
|
// if defined
|
2019-07-12 16:47:27 +03:00
|
|
|
// the value is returned (float)
|
2019-12-16 19:03:38 +03:00
|
|
|
// else
|
2019-07-12 16:47:27 +03:00
|
|
|
// the default value is returned
|
2019-08-07 17:52:10 +03:00
|
|
|
// version with abbreviation
|
2019-07-12 16:47:27 +03:00
|
|
|
//TODO error handling for invalid string to float conversion
|
2019-08-07 17:52:10 +03:00
|
|
|
pub fn (fs mut FlagParser) float_(n string, a byte, f f32, u string) f32 {
|
2019-12-16 19:03:38 +03:00
|
|
|
value := fs.float_opt(n, a, u) or {
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
return value
|
2019-07-12 16:47:27 +03:00
|
|
|
}
|
|
|
|
|
2019-12-16 19:03:38 +03:00
|
|
|
// defining and parsing a float flag
|
|
|
|
// if defined
|
2019-08-07 17:52:10 +03:00
|
|
|
// the value is returned (float)
|
2019-12-16 19:03:38 +03:00
|
|
|
// else
|
2019-08-07 17:52:10 +03:00
|
|
|
// the default value is returned
|
|
|
|
//TODO error handling for invalid string to float conversion
|
|
|
|
pub fn (fs mut FlagParser) float(n string, f f32, u string) f32 {
|
2019-12-16 19:03:38 +03:00
|
|
|
return fs.float_(n, `\0`, f, u)
|
|
|
|
}
|
|
|
|
|
|
|
|
// string_multi returns all instances of values associated with the flags provided
|
|
|
|
// In the case that none were found, it returns an empty array.
|
|
|
|
pub fn (fs mut FlagParser) string_multi(n string, a byte, u string) []string {
|
|
|
|
fs.add_flag(n, a, u, '<multiple floats>')
|
|
|
|
return fs.parse_value(n, a)
|
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.
|
|
|
|
pub fn (fs mut FlagParser) string_opt(n string, a byte, u string) ?string {
|
|
|
|
fs.add_flag(n, a, u, '<string>')
|
|
|
|
parsed := fs.parse_value(n, a)
|
|
|
|
if parsed.len == 0 {
|
|
|
|
return error("parameter '$n' not provided")
|
|
|
|
}
|
|
|
|
return parsed[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// defining and parsing a string flag
|
|
|
|
// if defined
|
2019-07-12 16:47:27 +03:00
|
|
|
// the value is returned (string)
|
2019-12-16 19:03:38 +03:00
|
|
|
// else
|
2019-07-12 16:47:27 +03:00
|
|
|
// the default value is returned
|
2019-08-07 17:52:10 +03:00
|
|
|
// version with abbreviation
|
|
|
|
pub fn (fs mut FlagParser) string_(n string, a byte, v, u string) string {
|
2019-12-16 19:03:38 +03:00
|
|
|
value := fs.string_opt(n, a, u) or {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
return value
|
2019-07-12 16:47:27 +03:00
|
|
|
}
|
|
|
|
|
2019-12-16 19:03:38 +03:00
|
|
|
// defining and parsing a string flag
|
|
|
|
// if defined
|
2019-08-07 17:52:10 +03:00
|
|
|
// the value is returned (string)
|
2019-12-16 19:03:38 +03:00
|
|
|
// else
|
2019-08-07 17:52:10 +03:00
|
|
|
// the default value is returned
|
|
|
|
pub fn (fs mut FlagParser) string(n, v, u string) string {
|
2019-12-16 19:03:38 +03:00
|
|
|
return fs.string_(n, `\0`, v, u)
|
2019-08-07 17:52:10 +03:00
|
|
|
}
|
|
|
|
|
2019-09-28 14:17:16 +03:00
|
|
|
pub fn (fs mut FlagParser) limit_free_args_to_at_least(n int) {
|
|
|
|
if n > MAX_ARGS_NUMBER {
|
2019-12-16 19:03:38 +03:00
|
|
|
panic('flag.limit_free_args_to_at_least expect n to be smaller than $MAX_ARGS_NUMBER')
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (fs mut FlagParser) limit_free_args_to_exactly(n int) {
|
|
|
|
if n > MAX_ARGS_NUMBER {
|
2019-12-16 19:03:38 +03:00
|
|
|
panic('flag.limit_free_args_to_exactly expect n to be smaller than $MAX_ARGS_NUMBER')
|
|
|
|
}
|
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)
|
|
|
|
pub fn (fs mut FlagParser) limit_free_args(min, 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
|
|
|
}
|
|
|
|
|
2019-09-28 14:17:16 +03:00
|
|
|
pub fn (fs mut 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 {
|
2019-09-28 14:17:16 +03:00
|
|
|
|
2019-12-16 19:03:38 +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 )
|
|
|
|
|
|
|
|
mut adesc := if fs.args_description.len > 0 { fs.args_description } else { '[ARGS]' }
|
|
|
|
if no_arguments { adesc = '' }
|
|
|
|
|
|
|
|
mut use := ''
|
2019-12-30 07:21:22 +03:00
|
|
|
if fs.application_version != '' {
|
|
|
|
use += '$fs.application_name $fs.application_version\n'
|
|
|
|
use += '$UNDERLINE\n'
|
|
|
|
}
|
2019-12-16 19:03:38 +03:00
|
|
|
use += 'Usage: ${fs.application_name} [options] $adesc\n'
|
|
|
|
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
|
|
|
|
}
|
|
|
|
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' }
|
|
|
|
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 {
|
2019-12-30 07:21:22 +03:00
|
|
|
longstr := if f.val_desc.contains('bool') {
|
|
|
|
', --$f.name'
|
|
|
|
} else {
|
|
|
|
', --$f.name $f.val_desc'
|
|
|
|
}
|
|
|
|
flag_desc := if f.name.len == 0 {
|
|
|
|
', $f.val_desc'
|
|
|
|
} else {
|
|
|
|
longstr
|
|
|
|
}
|
2019-12-16 19:03:38 +03:00
|
|
|
space := if flag_desc.len > SPACE.len-2 {
|
|
|
|
'\n$SPACE'
|
|
|
|
} else {
|
|
|
|
SPACE[flag_desc.len..]
|
|
|
|
}
|
2019-12-30 07:21:22 +03:00
|
|
|
abbr_desc := if f.abbr == `\0` { '' } else { ' -${tos(f.abbr, 1)}' }
|
2019-12-16 19:03:38 +03:00
|
|
|
use += '${abbr_desc}${flag_desc}${space}${f.usage}\n'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return use
|
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] == '--' {
|
|
|
|
return error('Unknown argument \'${a[2..]}\'')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if fs.args.len < fs.min_free_args && fs.min_free_args > 0 {
|
|
|
|
return error('Expected at least ${fs.min_free_args} arguments, but given $fs.args.len')
|
|
|
|
}
|
|
|
|
if fs.args.len > fs.max_free_args && fs.max_free_args > 0 {
|
|
|
|
return error('Expected at most ${fs.max_free_args} arguments, but given $fs.args.len')
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2019-08-05 08:50:24 +03:00
|
|
|
|