Module cli stdlib

cli
Version:
0.3.3
License:
MIT
Dependencies from vmod:
0
Imports:
3
Imported by:
1
Repository:
OS-specific
Show selected OS-specific symbols.
Backend-specific
Show selected Backend-specific symbols.

Dependencies defined in v.mod

This section is empty.

Imports

Imported by

Overview

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)
}

Aliases

This section is empty.

Constants

This section is empty.

Sum types

This section is empty.

Functions

#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.

Structs

#struct Command

pub 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) 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) add_default_flags

fn (mut cmd &Command) add_default_flags()

add_default_flags adds the commonly used -h/--help and -v/--version flags to the Command.

#fn (&Command) add_default_commands

fn (mut cmd &Command) add_default_commands()

add_default_commands adds the command functions of the commonly used help and version flags to the Command.

#fn (&Command) parse_flags

fn (mut cmd &Command) parse_flags()

#fn (&Command) parse_commands

fn (mut cmd &Command) parse_commands()

#fn (&Command) handle_cb

fn (mut cmd &Command) handle_cb(cb fn (Command) !, label string)

#fn (&Command) check_help_flag

fn (cmd &Command) check_help_flag()

#fn (&Command) check_man_flag

fn (cmd &Command) check_man_flag()

#fn (&Command) check_version_flag

fn (cmd &Command) check_version_flag()

#fn (&Command) check_required_flags

fn (cmd &Command) check_required_flags()

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

fn (cmd &Command) help_message() string

help_message returns a generated help message as a string for the Command.

#fn (&Command) manpage

fn (cmd &Command) manpage() string

manpage returns a string containing the mdoc(7) manpage for this Command

#struct Flag

pub 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.

#fn (&Flag) matches

fn (mut flag &Flag) matches(args []string, posix_mode bool) bool

matches returns true if first arg in args matches this flag.

#fn (&Flag) parse_raw

fn (mut flag &Flag) parse_raw(args []string) ![]string

#fn (&Flag) parse_bool

fn (mut flag &Flag) parse_bool(args []string) ![]string

#fn (&Flag) get_value_or_default_value

fn (flag &Flag) get_value_or_default_value() []string

Check if value is set by command line option. If not, return default value.

Interfaces

This section is empty.

Enums

#enum FlagType

pub enum FlagType {
	bool
	int
	float
	string
	// If flag can set multiple time, use array type
	int_array
	float_array
	string_array
}