Example illustrating assocation of flag specifications with bit flags and (optionally) a receiver variable, thus simplifying the command-line handling code.
// examples/bit_flags.go
package main
import (
"fmt"
clasp "github.com/synesissoftware/CLASP.Go"
libclimate "github.com/synesissoftware/libCLImate.Go"
"os"
)
const (
BF_Sound int = 1 << iota
BF_Vision
)
func main() {
// Specify specifications, parse, and checking standard flags
var flags = 0
flag_Sound := clasp.Flag("--enable-sound").SetAlias("-s").SetHelp("Enables sound").SetBitFlags(BF_Sound, &flags)
flag_Vision := clasp.Flag("--enable-vision").SetAlias("-v").SetHelp("Enables vision").SetBitFlags(BF_Vision, &flags)
climate, _ := libclimate.Init(func(cl *libclimate.Climate) (err error) {
cl.AddFlag(flag_Sound)
cl.AddFlag(flag_Vision)
cl.Version = []int{0, 0, 1}
cl.InfoLines = []string{
"libCLImate.Go Examples",
"",
":version:",
"",
}
cl.UsageHelpSuffix = "" // suppresses the default
return nil
}, libclimate.InitFlag_PanicOnFailure)
_, _ = climate.ParseAndVerify(os.Args, libclimate.ParseFlag_PanicOnFailure)
// Program logic
if 0 != (BF_Sound & flags) {
fmt.Println("running with sound")
}
if 0 != (BF_Vision & flags) {
fmt.Println("running with vision")
}
if 0 == flags {
fmt.Println("running in default mode")
}
}If executed with no arguments
go run examples/bit_flags.goit gives the output:
running in default mode
If executed with the arguments
go run examples/bit_flags.go --helpit gives the output:
libCLImate.Go Examples
bit_flags 0.0.1
USAGE: bit_flags [ ... flags and options ... ]
flags/options:
--help
Shows this help and exits
--version
Shows version information and exits
-s
--enable-sound
Enables sound
-v
--enable-vision
Enables vision
If executed with the arguments
go run examples/bit_flags.go --enable-soundit gives the output:
running with sound
If executed with the arguments
go run examples/bit_flags.go -vit gives the (same) output:
running with vision
If executed with the arguments
go run examples/bit_flags.go -svit gives the (same) output:
running with sound
running with vision