-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Required flags #155
Required flags #155
Changes from all commits
7e05320
73e64a1
4b2fcdb
8f1fb06
cbd9529
e67e05f
6023f37
145da32
b5844af
a6482d2
aba73ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,21 +88,31 @@ func (c Command) Run(ctx *Context) error { | |
err = set.Parse(ctx.Args().Tail()) | ||
} | ||
|
||
if err != nil { | ||
fmt.Fprint(ctx.App.Writer, "Incorrect Usage.\n\n") | ||
// Define here so it closes over the above variables | ||
showErrAndHelp := func(err error) { | ||
fmt.Fprintln(ctx.App.Writer, err) | ||
fmt.Fprintln(ctx.App.Writer) | ||
ShowCommandHelp(ctx, c.Name) | ||
fmt.Fprintln(ctx.App.Writer) | ||
} | ||
|
||
if err != nil { | ||
showErrAndHelp(fmt.Errorf("Incorrect Usage.")) | ||
return err | ||
} | ||
|
||
nerr := normalizeFlags(c.Flags, set) | ||
if nerr != nil { | ||
fmt.Fprintln(ctx.App.Writer, nerr) | ||
fmt.Fprintln(ctx.App.Writer) | ||
ShowCommandHelp(ctx, c.Name) | ||
fmt.Fprintln(ctx.App.Writer) | ||
showErrAndHelp(nerr) | ||
return nerr | ||
} | ||
|
||
cerr := checkRequiredFlags(c.Flags, set) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here with respect to DRYing. |
||
if cerr != nil { | ||
showErrAndHelp(cerr) | ||
return cerr | ||
} | ||
|
||
context := NewContext(ctx.App, set, ctx.globalSet) | ||
|
||
if checkCommandCompletions(context, c.Name) { | ||
|
@@ -156,5 +166,8 @@ func (c Command) startApp(ctx *Context) error { | |
app.Action = helpSubcommand.Action | ||
} | ||
|
||
// set the writer to the original App's writer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a separate bug that I noticed when I tried to integrate the latest code with our codebase. The |
||
app.Writer = ctx.App.Writer | ||
|
||
return app.RunAsSubcommand(ctx) | ||
} |
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.
It'd be nice to DRY this up by combining with the above conditional. The only thing that differs is which error you print.