complete/compflag/compflag.go

103 lines
3.0 KiB
Go
Raw Permalink Normal View History

2019-11-13 22:51:44 -06:00
// Package compflag provides a handful of standard library-compatible flags with bash complition capabilities.
//
// Usage
//
2019-11-17 17:25:16 -06:00
// import "github.com/posener/complete/v2/compflag"
2019-11-13 22:51:44 -06:00
//
// var (
// // Define flags...
// foo = compflag.String("foo", "", "")
// )
//
// func main() {
// compflag.Parse()
2019-11-13 22:51:44 -06:00
// // Main function.
// }
//
// Alternatively, the library can just be used with the standard library flag package:
//
// import (
// "flag"
2019-11-17 17:25:16 -06:00
// "github.com/posener/complete/v2/compflag"
2019-11-13 22:51:44 -06:00
// )
//
// var (
// // Define flags...
// foo = compflag.String("foo", "", "")
// bar = flag.String("bar", "", "")
// )
//
// func main() {
// complete.CommandLine()
2019-11-27 13:21:30 -06:00
// flag.Parse()
2019-11-13 22:51:44 -06:00
// // Main function.
// }
package compflag
import (
"flag"
"os"
"strconv"
2019-11-27 13:33:55 -06:00
"time"
2019-11-13 22:51:44 -06:00
2019-11-17 17:25:16 -06:00
"github.com/posener/complete/v2"
2019-11-13 22:51:44 -06:00
)
// FlagSet is bash completion enabled flag.FlagSet.
type FlagSet flag.FlagSet
// Parse parses command line arguments.
func (fs *FlagSet) Parse(args []string) error {
return (*flag.FlagSet)(fs).Parse(args)
}
2019-11-27 14:24:33 -06:00
func (fs *FlagSet) Visit(fn func(*flag.Flag)) { (*flag.FlagSet)(fs).Visit(fn) }
func (fs *FlagSet) VisitAll(fn func(*flag.Flag)) { (*flag.FlagSet)(fs).VisitAll(fn) }
func (fs *FlagSet) Arg(i int) string { return (*flag.FlagSet)(fs).Arg(i) }
func (fs *FlagSet) Args() []string { return (*flag.FlagSet)(fs).Args() }
func (fs *FlagSet) NArg() int { return (*flag.FlagSet)(fs).NArg() }
func (fs *FlagSet) NFlag() int { return (*flag.FlagSet)(fs).NFlag() }
func (fs *FlagSet) Name() string { return (*flag.FlagSet)(fs).Name() }
func (fs *FlagSet) PrintDefaults() { (*flag.FlagSet)(fs).PrintDefaults() }
func (fs *FlagSet) Lookup(name string) *flag.Flag { return (*flag.FlagSet)(fs).Lookup(name) }
func (fs *FlagSet) Parsed() bool { return (*flag.FlagSet)(fs).Parsed() }
2019-11-13 22:51:44 -06:00
// Complete performs bash completion if needed.
func (fs *FlagSet) Complete() {
complete.Complete(fs.Name(), complete.FlagSet((*flag.FlagSet)(CommandLine)))
2019-11-13 22:51:44 -06:00
}
var CommandLine = (*FlagSet)(flag.CommandLine)
// Parse parses command line arguments. It also performs bash completion when needed.
func Parse() {
CommandLine.Complete()
2019-11-13 22:51:44 -06:00
CommandLine.Parse(os.Args[1:])
}
2020-05-09 05:36:38 -05:00
func predictBool(value bool, prefix string) []string {
2019-11-13 22:51:44 -06:00
// If false, typing the bool flag is expected to turn it on, so there is nothing to complete
// after the flag.
2020-05-09 05:36:38 -05:00
if !value {
2019-11-13 22:51:44 -06:00
return nil
}
// Otherwise, suggest only to turn it off.
return []string{"false"}
}
2020-05-09 05:36:38 -05:00
func parseString(s string) (string, error) { return s, nil }
2019-11-13 22:51:44 -06:00
2020-05-09 05:36:38 -05:00
func formatString(v string) string { return v }
2019-11-13 22:51:44 -06:00
2020-05-09 05:36:38 -05:00
func parseInt(s string) (int, error) { return strconv.Atoi(s) }
2019-11-13 22:51:44 -06:00
2020-05-09 05:36:38 -05:00
func formatInt(v int) string { return strconv.Itoa(v) }
2019-11-13 22:51:44 -06:00
2020-05-09 05:36:38 -05:00
func parseBool(s string) (bool, error) { return strconv.ParseBool(s) }
2019-11-13 22:51:44 -06:00
2020-05-09 05:36:38 -05:00
func formatBool(v bool) string { return strconv.FormatBool(v) }
2019-11-13 22:51:44 -06:00
2020-05-09 05:36:38 -05:00
func parseDuration(s string) (time.Duration, error) { return time.ParseDuration(s) }
2019-11-13 22:51:44 -06:00
2020-05-09 05:36:38 -05:00
func formatDuration(v time.Duration) string { return v.String() }