From e63b7a9037200ec7a2435f996ad9dd07d2a83128 Mon Sep 17 00:00:00 2001 From: Eyal Posener Date: Wed, 27 Nov 2019 21:50:17 +0200 Subject: [PATCH] Use command line name instead of external name --- compflag/compflag.go | 20 ++++++++++++++------ example/compflag/main.go | 2 +- example/stdlib/main.go | 2 +- flags.go | 4 ++-- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/compflag/compflag.go b/compflag/compflag.go index fa219c8..badbc57 100644 --- a/compflag/compflag.go +++ b/compflag/compflag.go @@ -10,7 +10,7 @@ // ) // // func main() { -// compflag.Parse("my-program") +// compflag.Parse() // // Main function. // } // @@ -28,7 +28,7 @@ // ) // // func main() { -// complete.CommandLine("my-program") +// complete.CommandLine() // flag.Parse() // // Main function. // } @@ -53,9 +53,17 @@ func (fs *FlagSet) Parse(args []string) error { 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. -func (fs *FlagSet) Complete(name string) { - complete.Complete(name, complete.FlagSet((*flag.FlagSet)(CommandLine))) +func (fs *FlagSet) Complete() { + complete.Complete(fs.Name(), complete.FlagSet((*flag.FlagSet)(CommandLine))) } 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) // Parse parses command line arguments. It also performs bash completion when needed. -func Parse(name string) { - CommandLine.Complete(name) +func Parse() { + CommandLine.Complete() CommandLine.Parse(os.Args[1:]) } diff --git a/example/compflag/main.go b/example/compflag/main.go index 98328fe..1d51307 100644 --- a/example/compflag/main.go +++ b/example/compflag/main.go @@ -20,7 +20,7 @@ var ( func main() { // Parse flags and perform bash completion if needed. - compflag.Parse("stdlib") + compflag.Parse() // Program logic. if *name == "" { diff --git a/example/stdlib/main.go b/example/stdlib/main.go index 5beee51..c54130c 100644 --- a/example/stdlib/main.go +++ b/example/stdlib/main.go @@ -20,7 +20,7 @@ var ( func main() { // Run the completion. Notice that since we are using standard library flags, only the flag // names will be completed and not their values. - complete.CommandLine("stdlib") + complete.CommandLine() // Parse the flags. flag.Parse() diff --git a/flags.go b/flags.go index 7658061..3a1cdfd 100644 --- a/flags.go +++ b/flags.go @@ -5,8 +5,8 @@ import ( ) // Complete default command line flag set defined by the standard library. -func CommandLine(name string) { - Complete(name, FlagSet(flag.CommandLine)) +func CommandLine() { + Complete(flag.CommandLine.Name(), FlagSet(flag.CommandLine)) } // FlagSet returns a completer for a given standard library `flag.FlagSet`. It completes flag names,