-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add help statement for cli #460
Conversation
Pull Request Test Coverage Report for Build 917
💛 - Coveralls |
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.
Thanks for the PR!
internal/cli/main.go
Outdated
@@ -145,6 +163,11 @@ Database drivers: `+strings.Join(database.List(), ", ")+"\n") | |||
} | |||
|
|||
case "goto": | |||
if flag.Arg(1) == helpArgument { |
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.
Use a flag.NewFlagSet()
to parse the the argument. Can you create create a new method that creates a FlagSet with -help
argument parsing and use that for all of the sub-commands?
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 updated the PR
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.
Thanks for addressing my feedback! Some of the code is repetitive and can be refactored.
internal/cli/main.go
Outdated
if err := createFlagSet.Parse(args); err != nil { | ||
log.Println(err) | ||
} | ||
|
||
if *help { | ||
log.Println(createUsage) |
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.
Use fmt.Fprintln(os.Stderr, ...)
for help messages so they're not modified when -verbose
is specified.
internal/cli/main.go
Outdated
if err := createFlagSet.Parse(args); err != nil { | ||
log.Println(err) | ||
} | ||
|
||
if *help { |
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.
Factor this out into a common function: func handleSubCmdHelp(help bool, usage string, flagSet flag.FlagSet)
internal/cli/main.go
Outdated
@@ -145,11 +176,25 @@ Database drivers: `+strings.Join(database.List(), ", ")+"\n") | |||
} | |||
|
|||
case "goto": | |||
|
|||
gotoSet := flag.NewFlagSet("goto", flag.ExitOnError) |
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.
Factor this out into a common function that creates a new FlagSet
with a help flag: func newFlagSetWithHelp(name string, errHandling flag.ErrorHandling) (*FlagSet, bool)
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.
Thanks again for the PR and for addressing my feedback!
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.
Fix linter issues
internal/cli/main.go
Outdated
|
||
func handleSubCmdHelp(help bool, usage string, flagSet *flag.FlagSet) { | ||
if help { | ||
fmt.Fprintln(os.Stderr, createUsage) |
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.
createUsage
=> usage
internal/cli/main.go
Outdated
os.Exit(0) | ||
} | ||
} | ||
func newFlagSetWithHelp(name string, errHandling flag.ErrorHandling) (*flag.FlagSet, *bool) { |
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.
Looks like the errHandling
param is unused.
Might as well return bool
instead of *bool
.
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 guess I have to return a pointer to a bool otherwise the value will always be the one at the creation and I will not get updated after flag
has finished to parse the arguement.
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.
Remove the errHandling
param since it's always being passed the same value, which triggers the unparam
linter:
internal/cli/main.go:41:38: `newFlagSetWithHelp` - `errHandling` always receives `flag.ExitOnError` (`1`) (unparam)
func newFlagSetWithHelp(name string, errHandling flag.ErrorHandling) (*flag.FlagSet, *bool) {
^
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.
Thanks for the fixes. Noticed one more issue now that the diff is much smaller.
internal/cli/main.go
Outdated
if err := downFlagSet.Parse(args); err != nil { | ||
log.fatalErr(err) | ||
log.Println(err) |
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.
We should be consistent about how parse failures are handled in sub-commands. Previously, both the down
and drop
sub-commands would log.fatalErr
so let's continue doing that.
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.
Other flagset parsing that need to be updated to log.fatalErr
internal/cli/main.go
Outdated
extPtr := createFlagSet.String("ext", "", "File extension") | ||
dirPtr := createFlagSet.String("dir", "", "Directory to place file in (default: current working directory)") | ||
formatPtr := createFlagSet.String("format", defaultTimeFormat, `The Go time format string to use. If the string "unix" or "unixNano" is specified, then the seconds or nanoseconds since January 1, 1970 UTC respectively will be used. Caution, due to the behavior of time.Time.Format(), invalid format strings will not error`) | ||
createFlagSet.BoolVar(&seq, "seq", seq, "Use sequential numbers instead of timestamps (default: false)") | ||
createFlagSet.IntVar(&seqDigits, "digits", seqDigits, "The number of digits to use in sequences (default: 6)") | ||
|
||
if err := createFlagSet.Parse(args); err != nil { | ||
log.Println(err) |
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.
log.fatalErr
internal/cli/main.go
Outdated
gotoSet, helpPtr := newFlagSetWithHelp("goto") | ||
|
||
if err := gotoSet.Parse(args); err != nil { | ||
log.Println(err) |
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.
log.fatalErr
internal/cli/main.go
Outdated
upSet, helpPtr := newFlagSetWithHelp("up") | ||
|
||
if err := upSet.Parse(args); err != nil { | ||
log.Println(err) |
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.
log.fatalErr
internal/cli/main.go
Outdated
forceSet, helpPtr := newFlagSetWithHelp("force") | ||
|
||
if err := forceSet.Parse(args); err != nil { | ||
log.Println(err) |
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.
log.fatalErr
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.
Thanks again for the PR and for addressing all of the feedback!
if flag.Arg(1) != "" { | ||
if flag.NArg() > 0 { |
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.
This breaks the CLI using migrate up
(sans limit argument) because flag.NArg()
is always > 0 because flag.Arg(0)
is always up
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.
See: #467
Refers to #372