args.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package args
  14. import (
  15. "fmt"
  16. "github.com/spf13/pflag"
  17. "k8s.io/gengo/args"
  18. "k8s.io/gengo/examples/deepcopy-gen/generators"
  19. )
  20. // CustomArgs is used by the gengo framework to pass args specific to this generator.
  21. type CustomArgs generators.CustomArgs
  22. // NewDefaults returns default arguments for the generator.
  23. func NewDefaults() (*args.GeneratorArgs, *CustomArgs) {
  24. genericArgs := args.Default().WithoutDefaultFlagParsing()
  25. customArgs := &CustomArgs{}
  26. genericArgs.CustomArgs = (*generators.CustomArgs)(customArgs) // convert to upstream type to make type-casts work there
  27. genericArgs.OutputFileBaseName = "deepcopy_generated"
  28. return genericArgs, customArgs
  29. }
  30. // AddFlags add the generator flags to the flag set.
  31. func (ca *CustomArgs) AddFlags(fs *pflag.FlagSet) {
  32. pflag.CommandLine.StringSliceVar(&ca.BoundingDirs, "bounding-dirs", ca.BoundingDirs,
  33. "Comma-separated list of import paths which bound the types for which deep-copies will be generated.")
  34. }
  35. // Validate checks the given arguments.
  36. func Validate(genericArgs *args.GeneratorArgs) error {
  37. _ = genericArgs.CustomArgs.(*generators.CustomArgs)
  38. if len(genericArgs.OutputFileBaseName) == 0 {
  39. return fmt.Errorf("output file base name cannot be empty")
  40. }
  41. return nil
  42. }