FlagSet
#struct FlagSetA program's set of flags and the result of parsing.
| Name | Type | Description |
|---|---|---|
prog | str = "" | The program's name, for the help text. |
desc | str = "" | The program's description, for the help text. |
names | Vector<str> | The long names of the flags. |
shorts | Vector<str> | The short names of the flags. |
kinds | Vector<int> | The kind of each flag. |
vals | Vector<str> | The current value of each flag, as text. |
defs | Vector<str> | The default value of each flag. |
helps | Vector<str> | The help line of each flag. |
was_set | Vector<bool> | Whether each flag appeared on the command line. |
pos | Vector<str> | The positional arguments. |
err | str = "" | The message of the last parse error. |
help_wanted | bool = false | True when --help was given. |
import flag
func main:
mut f := flag.New("copy", "copies files")
defer flag.Free(f)
flag.AddBool(f, "verbose", "v", false, "report everything")
flag.AddStr(f, "out", "o", "output", "the destination directory")
flag.AddInt(f, "jobs", "j", 1, "how many jobs at once")
flag.AddFloat(f, "ratio", "r", 1.0, "the compression ratio")
mut args := Vector {} as Vector<str>
defer args.Free()
each a in ["-v", "--out=backup", "-j", "4", "a.txt", "b.txt"]:
args.Push(a)
end
println flag.Parse(f, args), "[" + flag.Error(f) + "]"
println flag.GetBool(f, "verbose"),
flag.GetStr(f, "out"),
flag.GetInt(f, "jobs"),
flag.GetFloat(f, "ratio")
println flag.WasSet(f, "jobs"), flag.WasSet(f, "ratio")
println flag.Positional(f), flag.HelpWanted(f)
println flag.FlagKindBool, flag.FlagKindStr, flag.FlagKindInt, flag.FlagKindFloat
endtrue [] true backup 4 1 true false ["a.txt", "b.txt"] false 0 1 2 3