Use command line name instead of external name

This commit is contained in:
Eyal Posener 2019-11-27 21:50:17 +02:00
parent 85f1fe4e1d
commit e63b7a9037
4 changed files with 18 additions and 10 deletions

View File

@ -10,7 +10,7 @@
// ) // )
// //
// func main() { // func main() {
// compflag.Parse("my-program") // compflag.Parse()
// // Main function. // // Main function.
// } // }
// //
@ -28,7 +28,7 @@
// ) // )
// //
// func main() { // func main() {
// complete.CommandLine("my-program") // complete.CommandLine()
// flag.Parse() // flag.Parse()
// // Main function. // // Main function.
// } // }
@ -53,9 +53,17 @@ func (fs *FlagSet) Parse(args []string) error {
return (*flag.FlagSet)(fs).Parse(args) return (*flag.FlagSet)(fs).Parse(args)
} }
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() }
// Complete performs bash completion if needed. // Complete performs bash completion if needed.
func (fs *FlagSet) Complete(name string) { func (fs *FlagSet) Complete() {
complete.Complete(name, complete.FlagSet((*flag.FlagSet)(CommandLine))) complete.Complete(fs.Name(), complete.FlagSet((*flag.FlagSet)(CommandLine)))
} }
func (fs *FlagSet) String(name string, value string, usage string, options ...predict.Option) *string { func (fs *FlagSet) String(name string, value string, usage string, options ...predict.Option) *string {
@ -85,8 +93,8 @@ func (fs *FlagSet) Duration(name string, value time.Duration, usage string, opti
var CommandLine = (*FlagSet)(flag.CommandLine) var CommandLine = (*FlagSet)(flag.CommandLine)
// Parse parses command line arguments. It also performs bash completion when needed. // Parse parses command line arguments. It also performs bash completion when needed.
func Parse(name string) { func Parse() {
CommandLine.Complete(name) CommandLine.Complete()
CommandLine.Parse(os.Args[1:]) CommandLine.Parse(os.Args[1:])
} }

View File

@ -20,7 +20,7 @@ var (
func main() { func main() {
// Parse flags and perform bash completion if needed. // Parse flags and perform bash completion if needed.
compflag.Parse("stdlib") compflag.Parse()
// Program logic. // Program logic.
if *name == "" { if *name == "" {

View File

@ -20,7 +20,7 @@ var (
func main() { func main() {
// Run the completion. Notice that since we are using standard library flags, only the flag // Run the completion. Notice that since we are using standard library flags, only the flag
// names will be completed and not their values. // names will be completed and not their values.
complete.CommandLine("stdlib") complete.CommandLine()
// Parse the flags. // Parse the flags.
flag.Parse() flag.Parse()

View File

@ -5,8 +5,8 @@ import (
) )
// Complete default command line flag set defined by the standard library. // Complete default command line flag set defined by the standard library.
func CommandLine(name string) { func CommandLine() {
Complete(name, FlagSet(flag.CommandLine)) Complete(flag.CommandLine.Name(), FlagSet(flag.CommandLine))
} }
// FlagSet returns a completer for a given standard library `flag.FlagSet`. It completes flag names, // FlagSet returns a completer for a given standard library `flag.FlagSet`. It completes flag names,