refactor: embed *flag.FlagSet

Embed *flag.FlagSet directly into the subcommand types.
This commit is contained in:
Dan Anglin 2023-08-11 19:26:24 +01:00
parent cee274318d
commit b9dbdb2c61
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 8 additions and 24 deletions

View file

@ -16,7 +16,7 @@ import (
) )
type GenerateCommand struct { type GenerateCommand struct {
fs *flag.FlagSet *flag.FlagSet
input string input string
output string output string
employmentHistory int employmentHistory int
@ -24,24 +24,16 @@ type GenerateCommand struct {
func NewGenerateCommand() *GenerateCommand { func NewGenerateCommand() *GenerateCommand {
gc := GenerateCommand{ gc := GenerateCommand{
fs: flag.NewFlagSet("generate", flag.ExitOnError), FlagSet: flag.NewFlagSet("generate", flag.ExitOnError),
} }
gc.fs.StringVar(&gc.input, "input", "", "specify the CV JSON file that you want to input to the builder.") gc.StringVar(&gc.input, "input", "", "specify the CV JSON file that you want to input to the builder.")
gc.fs.StringVar(&gc.output, "output", "", "specify the name of the output CV file.") gc.StringVar(&gc.output, "output", "", "specify the name of the output CV file.")
gc.fs.IntVar(&gc.employmentHistory, "employment-history", 10, "show employment history within these number of years.") gc.IntVar(&gc.employmentHistory, "employment-history", 10, "show employment history within these number of years.")
return &gc return &gc
} }
func (g *GenerateCommand) Parse(args []string) error {
return g.fs.Parse(args)
}
func (g *GenerateCommand) Name() string {
return g.fs.Name()
}
func (g *GenerateCommand) Run() error { func (g *GenerateCommand) Run() error {
historyLimit := time.Now().AddDate(-1*g.employmentHistory, 0, 0) historyLimit := time.Now().AddDate(-1*g.employmentHistory, 0, 0)

View file

@ -6,7 +6,7 @@ import (
) )
type VersionCommand struct { type VersionCommand struct {
fs *flag.FlagSet *flag.FlagSet
fullVersion bool fullVersion bool
} }
@ -19,22 +19,14 @@ var (
func NewVersionCommand() *VersionCommand { func NewVersionCommand() *VersionCommand {
vc := VersionCommand{ vc := VersionCommand{
fs: flag.NewFlagSet("version", flag.ExitOnError), FlagSet: flag.NewFlagSet("version", flag.ExitOnError),
} }
vc.fs.BoolVar(&vc.fullVersion, "full", false, "prints the full version") vc.BoolVar(&vc.fullVersion, "full", false, "prints the full version")
return &vc return &vc
} }
func (v *VersionCommand) Parse(args []string) error {
return v.fs.Parse(args)
}
func (v *VersionCommand) Name() string {
return v.fs.Name()
}
func (v *VersionCommand) Run() error { func (v *VersionCommand) Run() error {
var version string var version string
if v.fullVersion { if v.fullVersion {