@@ -12,14 +12,12 @@ import (
1212 "github.com/brandon1024/cmder/getopt"
1313)
1414
15- var (
16- // ErrIllegalCommandConfiguration is an error returned when a [Command] provided to [Execute] is illegal.
17- ErrIllegalCommandConfiguration = errors .New ("cmder: illegal command configuration" )
15+ // ErrIllegalCommandConfiguration is an error returned when a [Command] provided to [Execute] is illegal.
16+ var ErrIllegalCommandConfiguration = errors .New ("cmder: illegal command configuration" )
1817
19- // ErrEnvironmentBindFailure is an error returned when [Execute] failed to update flag value from environment
20- // variable (see [WithEnvironmentBinding]).
21- ErrEnvironmentBindFailure = errors .New ("cmder: failed to update flag from environment variable" )
22- )
18+ // ErrEnvironmentBindFailure is an error returned when [Execute] failed to update a flag value from environment
19+ // variables (see [WithEnvironmentBinding]).
20+ var ErrEnvironmentBindFailure = errors .New ("cmder: failed to update flag from environment variable" )
2321
2422// Execute runs a [Command].
2523//
7371// # Usage and Help Texts
7472//
7573// Whenever the user provides the '-h' or '--help' flag at the command line, [Execute] will display command usage and
76- // exit. The format of the help text can be adjusted by configuring [UsageTemplate ]. By default, usage information will
77- // be written to stderr, but this can be adjusted by setting [UsageOutputWriter ].
74+ // exit. The format of the help text can be adjusted with [WithUsageTemplate ]. By default, usage information will
75+ // be written to stderr, but this can be adjusted by setting [WithUsageOutput ].
7876//
7977// If a command's [Run] routine returns [ErrShowUsage] (or an error wrapping [ErrShowUsage]), [Execute] will render
8078// help text and exit with status 2.
@@ -86,7 +84,9 @@ func Execute(ctx context.Context, cmd Command, op ...ExecuteOption) error {
8684
8785 // prepare executor options
8886 ops := & ExecuteOptions {
89- args : os .Args [1 :],
87+ args : os .Args [1 :],
88+ usageTemplate : CobraUsageTemplate ,
89+ usageWriter : os .Stderr ,
9090 }
9191 for _ , f := range op {
9292 f (ops )
@@ -100,14 +100,14 @@ func Execute(ctx context.Context, cmd Command, op ...ExecuteOption) error {
100100
101101 // if help was requested, display and exit
102102 if cmd , ok := helpRequested (stack ); ok {
103- return usage (* cmd )
103+ return usage (* cmd , ops )
104104 }
105105
106- return execute (ctx , stack )
106+ return execute (ctx , stack , ops )
107107}
108108
109109// execute traverses the command stack recursively executing the lifecycle routines at each level.
110- func execute (ctx context.Context , stack []command ) error {
110+ func execute (ctx context.Context , stack []command , ops * ExecuteOptions ) error {
111111 if len (stack ) == 0 {
112112 return nil
113113 }
@@ -122,22 +122,22 @@ func execute(ctx context.Context, stack []command) error {
122122 )
123123
124124 // run init (if applicable)
125- if err := this .onInit (ctx ); err != nil {
125+ if err := this .onInit (ctx , ops ); err != nil {
126126 return err
127127 }
128128
129129 // if this is a leaf, run, otherwise recurse
130130 if len (stack ) == 1 {
131- err = this .run (ctx )
131+ err = this .run (ctx , ops )
132132 } else {
133- err = execute (ctx , stack [1 :])
133+ err = execute (ctx , stack [1 :], ops )
134134 }
135135 if err != nil {
136136 return err
137137 }
138138
139139 // run destroy (if applicable)
140- if err := this .onDestroy (ctx ); err != nil {
140+ if err := this .onDestroy (ctx , ops ); err != nil {
141141 return err
142142 }
143143
@@ -154,42 +154,42 @@ type command struct {
154154}
155155
156156// onInit calls the [RunnableLifecycle] init routine if present on c.
157- func (c command ) onInit (ctx context.Context ) error {
157+ func (c command ) onInit (ctx context.Context , ops * ExecuteOptions ) error {
158158 var err error
159159
160160 if cmd , ok := c .Command .(RunnableLifecycle ); ok {
161161 err = cmd .Initialize (ctx , c .args )
162162 }
163163
164164 if errors .Is (err , ErrShowUsage ) {
165- _ = usage (c )
165+ _ = usage (c , ops )
166166 os .Exit (2 )
167167 }
168168
169169 return err
170170}
171171
172172// run calls the [Runnable] run routine of c.
173- func (c command ) run (ctx context.Context ) error {
173+ func (c command ) run (ctx context.Context , ops * ExecuteOptions ) error {
174174 err := c .Run (ctx , c .args )
175175 if errors .Is (err , ErrShowUsage ) {
176- _ = usage (c )
176+ _ = usage (c , ops )
177177 os .Exit (2 )
178178 }
179179
180180 return err
181181}
182182
183183// onDestroy calls the [RunnableLifecycle] destroy routine if present on c.
184- func (c command ) onDestroy (ctx context.Context ) error {
184+ func (c command ) onDestroy (ctx context.Context , ops * ExecuteOptions ) error {
185185 var err error
186186
187187 if cmd , ok := c .Command .(RunnableLifecycle ); ok {
188188 err = cmd .Destroy (ctx , c .args )
189189 }
190190
191191 if errors .Is (err , ErrShowUsage ) {
192- _ = usage (c )
192+ _ = usage (c , ops )
193193 os .Exit (2 )
194194 }
195195
0 commit comments