Skip to content

Commit

Permalink
Remove implicit magic with flag registration in generators
Browse files Browse the repository at this point in the history
  • Loading branch information
sttts committed Sep 28, 2017
1 parent 7535618 commit b96b967
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
8 changes: 1 addition & 7 deletions args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package args

import (
"bytes"
goflag "flag"
"fmt"
"io/ioutil"
"os"
Expand All @@ -39,13 +38,11 @@ import (
// Default returns a defaulted GeneratorArgs. You may change the defaults
// before calling AddFlags.
func Default() *GeneratorArgs {
generatorArgs := &GeneratorArgs{
return &GeneratorArgs{
OutputBase: DefaultSourceTree(),
GoHeaderFilePath: filepath.Join(DefaultSourceTree(), "k8s.io/gengo/boilerplate/boilerplate.go.txt"),
GeneratedBuildTag: "ignore_autogenerated",
}
generatorArgs.AddFlags(pflag.CommandLine)
return generatorArgs
}

// GeneratorArgs has arguments that are passed to generators.
Expand Down Expand Up @@ -148,9 +145,6 @@ func DefaultSourceTree() string {
// If you don't need any non-default behavior, use as:
// args.Default().Execute(...)
func (g *GeneratorArgs) Execute(nameSystems namer.NameSystems, defaultSystem string, pkgs func(*generator.Context, *GeneratorArgs) generator.Packages) error {
pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
pflag.Parse()

b, err := g.NewBuilder()
if err != nil {
return fmt.Errorf("Failed making a parser: %v", err)
Expand Down
7 changes: 7 additions & 0 deletions examples/deepcopy-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ limitations under the License.
package main

import (
"flag"

"k8s.io/gengo/args"
"k8s.io/gengo/examples/deepcopy-gen/generators"

Expand All @@ -76,6 +78,11 @@ func main() {
"Comma-separated list of import paths which bound the types for which deep-copies will be generated.")
arguments.CustomArgs = customArgs

// Register default flags.
arguments.AddFlags(pflag.CommandLine)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()

// Run it.
if err := arguments.Execute(
generators.NameSystems(),
Expand Down
7 changes: 7 additions & 0 deletions examples/defaulter-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ limitations under the License.
package main

import (
"flag"

"k8s.io/gengo/args"
"k8s.io/gengo/examples/defaulter-gen/generators"

Expand All @@ -63,6 +65,11 @@ func main() {
"Comma-separated list of import paths which are considered, after tag-specified peers, for conversions.")
arguments.CustomArgs = customArgs

// Register default flags.
arguments.AddFlags(pflag.CommandLine)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()

// Run it.
if err := arguments.Execute(
generators.NameSystems(),
Expand Down
8 changes: 8 additions & 0 deletions examples/import-boss/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,24 @@ limitations under the License.
package main

import (
"flag"
"os"

"k8s.io/gengo/args"
"k8s.io/gengo/examples/import-boss/generators"

"github.com/golang/glog"
"github.com/spf13/pflag"
)

func main() {
arguments := args.Default()

// Register default flags.
arguments.AddFlags(pflag.CommandLine)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()

if err := arguments.Execute(
generators.NameSystems(),
generators.DefaultNameSystem(),
Expand Down
8 changes: 8 additions & 0 deletions examples/set-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,24 @@ limitations under the License.
package main

import (
"flag"
"os"

"k8s.io/gengo/args"
"k8s.io/gengo/examples/set-gen/generators"

"github.com/golang/glog"
"github.com/spf13/pflag"
)

func main() {
arguments := args.Default()

// Register default flags.
arguments.AddFlags(pflag.CommandLine)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()

if err := arguments.Execute(
generators.NameSystems(),
generators.DefaultNameSystem(),
Expand Down

0 comments on commit b96b967

Please sign in to comment.