-
-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extract `func (c *CommandArgumentValidator) validate(args []strin… #86
Conversation
53a3f84
to
5bcfd42
Compare
cmd/alias.go
Outdated
default: | ||
c.UI.Error(fmt.Sprintf("Error: too many arguments (expected 0, got %d)\n", len(args))) | ||
argCountErr := validateArgumentCount(args, 0, 0) | ||
if argCountErr != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we made this a struct with methods too? It could look something like:
type CommandArgumentValidator struct {
args []string{}
required Int
optional Int
// or maybe initialize with UI too
}
func (c *CommandArgumentValidator) validate(ui cli.Ui) int {
// etc
ui.Error(argCountErr.Error())
ui.Output(c.Help())
return 1
}
// in a command
validator := CommandArgumentValidator{args: args, required: 0, optional: 0}
validator.validate(c.UI)
One reason I like this is for the named parameters
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.
Usage:
commandArgumentValidator := &CommandArgumentValidator{required: 1, optional: 1}
commandArgumentErr := commandArgumentValidator.validate(args)
if commandArgumentErr != nil {
c.UI.Error(commandArgumentErr.Error())
c.UI.Output(c.Help())
return 1
}
To keep CommandArgumentValidator
simple., I am not passing c.UI
& c.Help()
to CommandArgumentValidator
.
Polluting CommandArgumentValidator
with c.UI
& c.Help()
to make 2 less lines of code doesn't worth and looks like a violation of single responsibility principle to me. :
commandArgumentValidator := &CommandArgumentValidator{required: 1, optional: 1, UI: c.UI, Help: c.Help()}
commandArgumentErr := commandArgumentValidator.validate(args)
if commandArgumentErr != nil {
return 1
}
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I agree 👍
validateArgumentCount
func (c *DotEnvCommand) Run(args []string) int
func (c *DotEnvCommand) Run(args []string) int
func (c *CommandArgumentValidator) validate(args []string) (err error)
2bb6bae
to
1427619
Compare
cmd/command_argument_validator.go
Outdated
|
||
expectedCount := fmt.Sprintf("exactly %d", c.required) | ||
if (c.optional > 0) { | ||
expectedCount = fmt.Sprintf("between %d and %d", c.required, c.required+c.optional) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just declare totalArgs := c.required+c.optional
up front?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
cmd/command_argument_validator.go
Outdated
} | ||
|
||
if argCount > c.required+c.optional { | ||
err = fmt.Errorf("Error: too many arguments (expected %s, got %d)\n", expectedCount, len(args)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can re-use argCount
in both places here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
cmd/command_argument_validator.go
Outdated
err = fmt.Errorf("Error: missing arguments (expected %s, got %d)\n", expectedCount, len(args)) | ||
} | ||
|
||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 does it complain if you explicitly do return err
? Makes more sense to me if not (even though this works)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- added explicitly return in case proposal: Go 2: remove bare return golang/go#21291 happens
- return inside the if block to short-circuit it
cmd/command_argument_validator.go
Outdated
return fmt.Errorf("Error: too many arguments (expected %s, got %d)\n", expectedCount, len(args)) | ||
} | ||
if argCount < c.required { | ||
return fmt.Errorf("Error: missing arguments (expected %s, got %d)\n", expectedCount, len(args)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I meant you can use argCount
instead of len(args)
in both these error messages?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i need coffee
func (c *CommandArgumentValidator) validate(args []string) (err error)
Example usage:
Note:
validateArgumentCount
doesn't support unlimited arguments, i.e:trellis-cli/cmd/exec.go
Lines 28 to 36 in c043cfd