cli #

Usage 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 ([]Flag) get_all_found #

fn (flags []Flag) get_all_found() []Flag

get_all_found returns an array of all Flags 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
	int_array
	float_array
	string_array
}

struct Command #

struct Command {
pub mut:
	name            string
	usage           string
	description     string
	version         string
	pre_execute     FnCommandCallback
	execute         FnCommandCallback
	post_execute    FnCommandCallback
	disable_help    bool
	disable_version bool
	disable_flags   bool
	sort_flags      bool
	sort_commands   bool
	parent          &Command = 0
	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) str #

fn (cmd Command) str() string

str returns the string representation of the Command.

fn (Command) is_root #

fn (cmd Command) is_root() bool

is_root returns true if this Command has no parents.

fn (Command) root #

fn (cmd Command) root() Command

root returns the root Command of the command chain.

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) add_commands #

fn (mut cmd Command) add_commands(commands []Command)

add_commands adds the commands array of Commands as sub-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) setup #

fn (mut cmd Command) setup()

setup ensures that all sub-commands of this Command is linked as a chain.

fn (Command) add_flags #

fn (mut cmd Command) add_flags(flags []Flag)

add_flags adds the array flags to this Command.

fn (Command) add_flag #

fn (mut cmd Command) add_flag(flag Flag)

add_flag adds flag to this Command.

fn (Command) parse #

fn (mut cmd Command) parse(args []string)

parse parses args into this structured Command.

fn (Command) execute_help #

fn (cmd Command) execute_help()

execute_help executes the callback registered for the -h/--help flag option.

struct Flag #

struct Flag {
pub mut:
	flag FlagType

	name string

	abbrev string

	description string
	global      bool

	required bool

	default_value []string = []
mut:
	found bool

	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.