flag: update vlib/flag/README.md

pull/7371/head
Delyan Angelov 2020-12-17 10:11:09 +02:00
parent c164586fd5
commit 9bd14e8421
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
1 changed files with 30 additions and 38 deletions

View File

@ -1,43 +1,35 @@
module flag for command-line flag parsing The `flag` module helps command-line flag parsing.
Main features are:
- parsing flags like '--flag' or '--stuff=things' or '--things stuff' - parses flags like `-f` or '--flag' or '--stuff=things' or '--things stuff'.
- handles bool, int, float and string args - handles bool, int, float and string args.
- is able to print usage - can print usage information listing all the declrared flags.
- handled unknown arguments as error - handles unknown arguments as error.
Usage example: Usage example:
```v ```v
module main module main
import os import os
import flag import flag
fn main() { fn main() {
mut fp := flag.new_flag_parser(os.args) mut fp := flag.new_flag_parser(os.args)
fp.application('flag_example_tool') fp.application('flag_example_tool')
fp.version('v0.0.0') fp.version('v0.0.0')
fp.description('This tool is only designed to show how the flag lib is working') fp.description('This tool is only designed to show how the flag lib is working')
fp.skip_executable()
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.')
an_int := fp.int('an_int', 0, 0o666, 'some int to define 0o666 is default') a_float := fp.float('a_float', 0, 1.0, 'some floating point value, by default 1.0 .')
a_bool := fp.bool('a_bool', 0, false, 'some \'real\' flag') a_string := fp.string('a_string', `a`, 'no text', 'finally, some text with ' +
a_float := fp.float('a_float', 0, 1.0, 'also floats') ' `-a` as an abbreviation, so you can pass --a_string abc or just -a abc')
a_string := fp.string('a_string', `a`, 'no text', 'finally, some text with "a" an abbreviation') additional_args := fp.finalize() or {
eprintln(err)
additional_args := fp.finalize() or { println(fp.usage())
eprintln(err) return
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())
}
println(' ```
an_int: $an_int
a_bool: $a_bool
a_float: $a_float
a_string: \'$a_string\'
')
println(additional_args.join_lines())
}
```