2019-11-20 14:52:44 -06:00
// compflag shows how to use the github.com/posener/complete/v2/compflag package to have auto bash
2019-11-13 22:51:44 -06:00
// completion for a defined set of flags.
package main
import (
"fmt"
"os"
2019-11-17 17:25:16 -06:00
"github.com/posener/complete/v2/compflag"
2019-11-22 03:40:09 -06:00
"github.com/posener/complete/v2/predict"
2019-11-13 22:51:44 -06:00
)
var (
// Add variables to the program. Since we are using the compflag library, we can pass options to
// enable bash completion to the flag values.
2019-11-22 03:40:09 -06:00
name = compflag . String ( "name" , "" , "Give your name" , predict . OptValues ( "foo" , "bar" , "foo bar" ) )
something = compflag . String ( "something" , "" , "Expect somthing, but we don't know what, so no other completion options will be provided." , predict . OptValues ( "" ) )
2019-11-13 22:51:44 -06:00
nothing = compflag . String ( "nothing" , "" , "Expect nothing after flag, so other completion can be provided." )
)
func main ( ) {
// Parse flags and perform bash completion if needed.
2019-11-27 13:50:17 -06:00
compflag . Parse ( )
2019-11-13 22:51:44 -06:00
// Program logic.
if * name == "" {
fmt . Println ( "Your name is missing" )
os . Exit ( 1 )
}
fmt . Println ( "Hi," , name )
}