v/vlib/flag
Alexander Medvednikov d4a0d6f73c all: byte => u8 2022-04-15 14:58:56 +03:00
..
testdata flag: add .usage_example/1, .footer/1, .remaining_parameters/0 and tests 2021-07-11 11:06:48 +03:00
README.md docs: adding skeleton README.md files for all vlib modules (#13034) 2022-01-05 18:06:08 +02:00
default_flag_options_test.v os: add os.quoted_path/1, use it consistently for running V itself 2022-01-22 21:13:28 +02:00
flag.v all: byte => u8 2022-04-15 14:58:56 +03:00
flag_test.v builtin: change IError `msg` and `code` to methods + fix vlib, add a deprecation notice for the old usages (#13041) 2022-02-11 15:52:33 +02:00
usage_example_test.v os: add os.quoted_path/1, use it consistently for running V itself 2022-01-22 21:13:28 +02:00

README.md

Description:

The flag module is a command line option parser. Its main features are:

  • simplicity of usage.
  • parses flags like -f or '--flag' or '--stuff=things' or '--things stuff'.
  • handles bool, int, float and string args.
  • can print usage information listing all the declared flags.
  • handles unknown arguments as error.

See also the cli module, for a more complex command line option parser, that supports declaring multiple subcommands each having a separate set of options.

Usage example:

module main

import os
import flag

fn main() {
	mut fp := flag.new_flag_parser(os.args)
	fp.application('flag_example_tool')
	fp.version('v0.0.1')
	fp.limit_free_args(0, 0) ? // comment this, if you expect arbitrary texts after the options
	fp.description('This tool is only designed to show how the flag lib is working')
	fp.skip_executable()
	an_int := fp.int('an_int', 0, 0o123, 'some int to define 0o123 is its default value')
	a_bool := fp.bool('a_bool', 0, false, 'some boolean flag. --a_bool will set it to true.')
	a_float := fp.float('a_float', 0, 1.0, 'some floating point value, by default 1.0 .')
	a_string := fp.string('a_string', `a`, 'no text', 'finally, some text with ' +
		' `-a` as an abbreviation, so you can pass --a_string abc or just -a abc')
	additional_args := fp.finalize() or {
		eprintln(err)
		println(fp.usage())
		return
	}
	println('an_int: $an_int | a_bool: $a_bool | a_float: $a_float | a_string: "$a_string" ')
	println(additional_args.join_lines())
}