cli #
Description:
cli
is a command line option parser, that supports
declarative subcommands, each having separate set of options.
See also the flag
module, for a simpler command line option parser,
that supports only options.
Example:
module main
import os
import cli
fn main() {
mut app := cli.Command{
name: 'example-app'
description: 'example-app'
execute: fn (cmd cli.Command) ! {
println('hello app')
return
}
commands: [
cli.Command{
name: 'sub'
execute: fn (cmd cli.Command) ! {
println('hello subcommand')
return
}
},
]
}
app.setup()
app.parse(os.args)
}
fn print_help_for_command #
fn print_help_for_command(help_cmd Command) !
print_help_for_command outputs the help message of help_cmd
.
fn print_manpage_for_command #
fn print_manpage_for_command(man_cmd Command) !
print_manpage_for_command prints the manpage for the command or subcommand in man_cmd
to stdout
fn ([]Flag) get_all_found #
fn (flags []Flag) get_all_found() []Flag
get_all_found returns an array of all Flag
s found in the command parameters
fn ([]Flag) get_bool #
fn (flags []Flag) get_bool(name string) !bool
get_bool returns true
if the flag specified in name
is set.
get_bool returns an error if the FlagType
is not boolean.
fn ([]Flag) get_int #
fn (flags []Flag) get_int(name string) !int
get_int returns the int
value argument of the flag specified in name
.
get_int returns an error if the FlagType
is not integer.
fn ([]Flag) get_ints #
fn (flags []Flag) get_ints(name string) ![]int
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.
fn ([]Flag) get_float #
fn (flags []Flag) get_float(name string) !f64
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.
fn ([]Flag) get_floats #
fn (flags []Flag) get_floats(name string) ![]f64
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.
fn ([]Flag) get_string #
fn (flags []Flag) get_string(name string) !string
get_string returns the string
value argument of the flag specified in name
.
get_string returns an error if the FlagType
is not string.
fn ([]Flag) get_strings #
fn (flags []Flag) get_strings(name string) ![]string
get_strings returns the string
value argument of the flag specified in name
.
get_strings returns an error if the FlagType
is not string.
fn (FnCommandCallback) str #
fn (f FnCommandCallback) str() string
str returns the string
representation of the callback.
enum FlagType #
enum FlagType {
bool
int
float
string
// If flag can set multiple time, use array type
int_array
float_array
string_array
}
struct Command #
struct Command {
pub mut:
name string
usage string
description string
man_description string
version string
pre_execute FnCommandCallback
execute FnCommandCallback
post_execute FnCommandCallback
disable_help bool
disable_man bool
disable_version bool
disable_flags bool
sort_flags bool
sort_commands bool
parent &Command = unsafe { nil }
commands []Command
flags []Flag
required_args int
args []string
posix_mode bool
}
Command is a structured representation of a single command or chain of commands.
fn (Command) add_command #
fn (mut cmd Command) add_command(command Command)
add_command adds command
as a sub-command of this Command
.
fn (Command) add_commands #
fn (mut cmd Command) add_commands(commands []Command)
add_commands adds the commands
array of Command
s as sub-commands.
fn (Command) add_flag #
fn (mut cmd Command) add_flag(flag Flag)
add_flag adds flag
to this Command
.
fn (Command) add_flags #
fn (mut cmd Command) add_flags(flags []Flag)
add_flags adds the array flags
to this Command
.
fn (Command) execute_help #
fn (cmd Command) execute_help()
execute_help executes the callback registered
for the -h
/--help
flag option.
fn (Command) execute_man #
fn (cmd Command) execute_man()
execute_help executes the callback registered
for the -man
flag option.
fn (Command) full_name #
fn (cmd Command) full_name() string
full_name returns the full string
representation of all commands int the chain.
fn (Command) help_message #
fn (cmd Command) help_message() string
help_message returns a generated help message as a string
for the Command
.
fn (Command) is_root #
fn (cmd Command) is_root() bool
is_root returns true
if this Command
has no parents.
fn (Command) manpage #
fn (cmd Command) manpage() string
manpage returns a string
containing the mdoc(7) manpage for this Command
fn (Command) parse #
fn (mut cmd Command) parse(args []string)
parse parses args
into this structured Command
.
fn (Command) root #
fn (cmd Command) root() Command
root returns the root Command
of the command chain.
fn (Command) setup #
fn (mut cmd Command) setup()
setup ensures that all sub-commands of this Command
is linked as a chain.
fn (Command) str #
fn (cmd Command) str() string
str returns the string
representation of the Command
.
struct Flag #
struct Flag {
pub mut:
flag FlagType
// Name of flag
name string
// Like short option
abbrev string
// Desciption of flag
description string
global bool
// If flag is requierd
required bool
// Default value if no value provide by command line
default_value []string = []
mut:
// Set true if flag found.
found bool
// Value of flag
value []string = []
}
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
fn (Flag) get_bool #
fn (flag Flag) get_bool() !bool
get_bool returns true
if the flag is set.
get_bool returns an error if the FlagType
is not boolean.
fn (Flag) get_int #
fn (flag Flag) get_int() !int
get_int returns the int
value argument of the flag.
get_int returns an error if the FlagType
is not integer.
fn (Flag) get_ints #
fn (flag Flag) get_ints() ![]int
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.
fn (Flag) get_float #
fn (flag Flag) get_float() !f64
get_float returns the f64
value argument of the flag.
get_float returns an error if the FlagType
is not floating point.
fn (Flag) get_floats #
fn (flag Flag) get_floats() ![]f64
get_floats returns the f64
value argument of the flag.
get_floats returns an error if the FlagType
is not floating point.
fn (Flag) get_string #
fn (flag Flag) get_string() !string
get_string returns the string
value argument of the flag.
get_string returns an error if the FlagType
is not string.
fn (Flag) get_strings #
fn (flag Flag) get_strings() ![]string
get_strings returns the array of string
value argument of the flag.
get_strings returns an error if the FlagType
is not string.
fn (Flag) parse #
fn (mut flag Flag) parse(args []string, posix_mode bool) ![]string
parse parses flag values from arguments and return an array of arguments with all consumed elements removed.