CLI Applications

37.1 flag Module

Newer struct-based parsing: flag.to_struct[Config](os.args) with field attributes (@[short: n; xdoc: 'help']).

import flag
import os

fn main() {
	mut fp := flag.new_flag_parser(os.args)
	fp.application('greeter')
	fp.version('1.0.0')
	fp.description('Greets people')
	fp.skip_executable()
	name := fp.string('name', `n`, 'World', 'who to greet')
	times := fp.int('times', `t`, 1, 'repetitions')
	loud := fp.bool('loud', `l`, false, 'shout')
	rest := fp.finalize() or {
		eprintln(err)
		println(fp.usage())
		return
	}
	for _ in 0 .. times {
		println(if loud { name.to_upper() } else { 'Hello, ${name}' })
	}
	_ = rest
}

37.2 cli Module (Subcommands)

import cli
import os

fn main() {
	mut app := cli.Command{
		name: 'tool'
		description: 'Multi-command tool'
		commands: [
			cli.Command{
				name: 'greet'
				flags: [cli.Flag{flag: .string, name: 'name', abbrev: 'n', default_value: ['World']}]
				execute: fn (cmd cli.Command) ! {
					name := cmd.flags.get_string('name')!
					println('Hello, ${name}')
				}
			},
		]
	}
	app.setup()
	app.parse(os.args)
}

37.3 Terminal Utilities

term (colors: term.green('ok'), term.bold, term.clear(), term.get_terminal_size()), term.ui (full TUI with an event loop), readline (line editing and history), progress bars (community), log (log.info, levels, file output).