9
9
"github.com/gookit/color"
10
10
"github.com/gookit/gcli/v3/events"
11
11
"github.com/gookit/gcli/v3/helper"
12
+ "github.com/gookit/goutil/cflag"
12
13
"github.com/gookit/goutil/cliutil"
13
14
"github.com/gookit/goutil/strutil"
14
15
)
@@ -41,8 +42,10 @@ type Logo struct {
41
42
42
43
// AppConfig struct
43
44
type AppConfig struct {
44
- RunBefore func () bool
45
- RunAfter func () bool
45
+ BeforeRun func () bool
46
+ AfterRun func () bool
47
+ BeforeAddOpts func (opts * Flags )
48
+ AfterBindOpts func (app * App ) bool
46
49
}
47
50
48
51
// App the cli app definition
@@ -55,6 +58,12 @@ type App struct {
55
58
// for manager commands
56
59
commandBase
57
60
61
+ AppConfig
62
+
63
+ fs * Flags
64
+ // app flag options
65
+ opts * GlobalOpts
66
+
58
67
// Name app name
59
68
Name string
60
69
// Desc app description
@@ -99,6 +108,7 @@ func NewApp(fns ...func(app *App)) *App {
99
108
app := & App {
100
109
Name : "GCliApp" ,
101
110
Desc : "This is my console application" ,
111
+ opts : newDefaultGlobalOpts (),
102
112
// set a default version.
103
113
// Version: "1.0.0",
104
114
// config
@@ -108,16 +118,21 @@ func NewApp(fns ...func(app *App)) *App {
108
118
commandBase : newCommandBase (),
109
119
}
110
120
121
+ app .fs = NewFlags ("appOptions" ).WithConfigFn (func (opt * FlagsConfig ) {
122
+ opt .WithoutType = true
123
+ opt .Alignment = AlignLeft
124
+ })
125
+
111
126
// internal core
112
127
Logf (VerbCrazy , "create new core on init application" )
113
128
app .core = core {
114
129
cmdLine : CLI ,
115
130
// init
116
131
Hooks : & Hooks {},
117
- gFlags : NewFlags ("app.GOptions " ).WithConfigFn (func (opt * FlagsConfig ) {
118
- opt .WithoutType = true
119
- opt .Alignment = AlignLeft
120
- }),
132
+ // gFlags: NewFlags("appOptions ").WithConfigFn(func(opt *FlagsConfig) {
133
+ // opt.WithoutType = true
134
+ // opt.Alignment = AlignLeft
135
+ // }),
121
136
}
122
137
123
138
// init commandBase
@@ -153,25 +168,23 @@ func (app *App) Config(fn func(a *App)) {
153
168
func (app * App ) bindingGlobalOpts () {
154
169
Logf (VerbDebug , "will begin binding global options" )
155
170
// global options flag
156
- // gf := flag.NewFlagSet(app.Args[0], flag.ContinueOnError)
157
- gf := app .GlobalFlags ()
171
+ fs := app .fs
158
172
159
173
// binding global options
160
- // bindingCommonGOpts(gf)
161
- gOpts .bindingFlags (gf )
174
+ app .opts .bindingFlags (fs )
162
175
// add more ...
163
- gf .BoolOpt (& gOpts .showVer , "version" , "V" , false , "Display app version information" )
176
+ fs .BoolOpt (& gOpts .ShowVersion , "version" , "V" , false , "Display app version information" )
164
177
// This is a internal option
165
- gf .BoolVar (& gOpts .inCompletion , & FlagMeta {
178
+ fs .BoolVar (& gOpts .inCompletion , & FlagMeta {
166
179
Name : "in-completion" ,
167
180
Desc : "generate completion scripts for bash/zsh" ,
168
181
// hidden it
169
182
Hidden : true ,
170
183
})
171
184
172
185
// support binding custom global options
173
- if app .GOptsBinder != nil {
174
- app .GOptsBinder ( gf )
186
+ if app .BeforeAddOpts != nil {
187
+ app .BeforeAddOpts ( fs )
175
188
}
176
189
}
177
190
@@ -225,7 +238,7 @@ func (app *App) AddCommand(c *Command) {
225
238
// init command
226
239
c .app = app
227
240
// inherit global flags from application
228
- c .core .gFlags = app .gFlags
241
+ // c.core.gFlags = app.gFlags
229
242
230
243
// do add command
231
244
app .commandBase .addCommand (app .Name , c )
@@ -275,46 +288,59 @@ func (app *App) AddAliases(name string, aliases ...string) {
275
288
// }
276
289
277
290
/*************************************************************
278
- * run command
291
+ * parse global options
279
292
*************************************************************/
280
293
281
- // parseGlobalOpts parse global options
282
- func (app * App ) parseGlobalOpts (args []string ) (ok bool ) {
294
+ // parseAppOpts parse global options
295
+ func (app * App ) doParseOpts (args []string ) error {
296
+ err := app .fs .Parse (args )
297
+ if err != nil {
298
+ if cflag .IsFlagHelpErr (err ) {
299
+ return nil
300
+ }
301
+ Logf (VerbWarn , "parse global options err: <red>%s</>" , err .Error ())
302
+ }
303
+
304
+ return err
305
+ }
306
+
307
+ // parseAppOpts parse global options
308
+ func (app * App ) parseAppOpts (args []string ) (ok bool ) {
283
309
Logf (VerbDebug , "will begin parse application options" )
284
310
285
311
// parse global options
286
- err := app .core . doParseGOpts (args )
312
+ err := app .doParseOpts (args )
287
313
if err != nil { // has error.
288
314
color .Error .Tips (err .Error ())
289
315
return
290
316
}
291
317
292
- app .args = app .gFlags .FSetArgs ()
318
+ app .args = app .fs .FSetArgs ()
293
319
if app .Fire (events .OnGOptionsParsed , map [string ]any {"args" : app .args }) {
294
320
Logf (VerbDebug , "stop continue on the event %s return True" , events .OnGOptionsParsed )
295
321
return
296
322
}
297
323
298
324
// check global options
299
- if gOpts . showHelp {
325
+ if app . opts . ShowHelp {
300
326
app .showApplicationHelp ()
301
327
return
302
328
}
303
329
304
- if gOpts . showVer {
330
+ if app . opts . ShowVersion {
305
331
app .showVersionInfo ()
306
332
return
307
333
}
308
334
309
335
// disable color
310
- if gOpts .NoColor {
336
+ if app . opts .NoColor {
311
337
color .Enable = false
312
338
}
313
339
314
- Debugf ("global option parsed, verbose level: <mgb>%s</>" , gOpts . verbose .String ())
340
+ Debugf ("global option parsed, Verbose level: <mgb>%s</>" , app . opts . Verbose .String ())
315
341
316
342
// TODO show auto-completion for bash/zsh
317
- if gOpts .inCompletion {
343
+ if app . opts .inCompletion {
318
344
app .showAutoCompletion (app .args )
319
345
return
320
346
}
@@ -468,7 +494,7 @@ func (app *App) Run(args []string) (code int) {
468
494
Debugf ("will begin run cli application. args: %v" , args )
469
495
470
496
// parse global flags
471
- if false == app .parseGlobalOpts (args ) {
497
+ if false == app .parseAppOpts (args ) {
472
498
return app .exitOnEnd (code )
473
499
}
474
500
@@ -684,7 +710,7 @@ func (app *App) showApplicationHelp() {
684
710
// render help text template
685
711
s := helper .RenderText (AppHelpTemplate , map [string ]any {
686
712
"Cs" : app .commands ,
687
- "GOpts" : app .gFlags .String (),
713
+ "GOpts" : app .fs .String (),
688
714
// app version
689
715
"Version" : app .Version ,
690
716
"HasSubs" : app .hasSubcommands ,
0 commit comments