@@ -138,6 +138,16 @@ public static function setGlobal(self $global): void
138
138
self ::$ global = $ global ;
139
139
}
140
140
141
+ /**
142
+ * @param callable(self): void $fn
143
+ *
144
+ * @return $this
145
+ */
146
+ public static function newWith (callable $ fn ): self
147
+ {
148
+ return (new self )->config ($ fn );
149
+ }
150
+
141
151
/**
142
152
* Class constructor.
143
153
*
@@ -158,6 +168,17 @@ public function __construct(array $config = [])
158
168
$ this ->flags ->setAutoBindArgs (false );
159
169
}
160
170
171
+ /**
172
+ * @param callable(self): void $fn
173
+ *
174
+ * @return $this
175
+ */
176
+ public function config (callable $ fn ): self
177
+ {
178
+ $ fn ($ this );
179
+ return $ this ;
180
+ }
181
+
161
182
/**
162
183
* @param FlagsParser $fs
163
184
*/
@@ -358,16 +379,20 @@ public static function handleException(Throwable $e): int
358
379
* @param string $command
359
380
* @param callable $handler
360
381
* @param array{desc:string,options:array,arguments:array} $config
382
+ *
383
+ * @return self
361
384
*/
362
- public function add (string $ command , callable $ handler , array $ config = []): void
385
+ public function add (string $ command , callable $ handler , array $ config = []): self
363
386
{
364
- $ this ->addCommand ($ command , $ handler , $ config );
387
+ return $ this ->addCommand ($ command , $ handler , $ config );
365
388
}
366
389
367
390
/**
368
391
* @param class-string|CmdHandlerInterface $handler
392
+ *
393
+ * @return self
369
394
*/
370
- public function addHandler (string |CmdHandlerInterface $ handler ): void
395
+ public function addHandler (string |CmdHandlerInterface $ handler ): self
371
396
{
372
397
if (is_string ($ handler )) {
373
398
// class string.
@@ -382,15 +407,19 @@ public function addHandler(string|CmdHandlerInterface $handler): void
382
407
$ config = $ handler ->metadata ();
383
408
$ command = Valid::arrayHasNoEmptyKey ($ config , 'name ' );
384
409
385
- $ this ->addCommand ($ command , $ handler , $ config );
410
+ return $ this ->addCommand ($ command , $ handler , $ config );
386
411
}
387
412
388
413
/**
414
+ * Add a command
415
+ *
389
416
* @param string $command
390
417
* @param callable|object|class-string $handler
391
418
* @param array{desc:string,options:array,arguments:array} $config
419
+ *
420
+ * @return self
392
421
*/
393
- public function addCommand (string $ command , callable |object |string $ handler , array $ config = []): void
422
+ public function addCommand (string $ command , callable |object |string $ handler , array $ config = []): self
394
423
{
395
424
if (!$ command ) {
396
425
throw new InvalidArgumentException ('command name can not be empty ' );
@@ -418,6 +447,7 @@ public function addCommand(string $command, callable|object|string $handler, arr
418
447
}
419
448
420
449
$ this ->metadata [$ command ] = array_merge (self ::COMMAND_CONFIG , $ config );
450
+ return $ this ;
421
451
}
422
452
423
453
/**
@@ -628,6 +658,16 @@ public function setParams(array $params): void
628
658
}
629
659
}
630
660
661
+ /**
662
+ * @param string $name
663
+ *
664
+ * @return void
665
+ */
666
+ public function setName (string $ name ): void
667
+ {
668
+ $ this ->params ['name ' ] = $ name ;
669
+ }
670
+
631
671
/**
632
672
* @return FlagsParser
633
673
*/
0 commit comments