@@ -138,6 +138,16 @@ public static function setGlobal(self $global): void
138138 self ::$ global = $ global ;
139139 }
140140
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+
141151 /**
142152 * Class constructor.
143153 *
@@ -158,6 +168,17 @@ public function __construct(array $config = [])
158168 $ this ->flags ->setAutoBindArgs (false );
159169 }
160170
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+
161182 /**
162183 * @param FlagsParser $fs
163184 */
@@ -358,16 +379,20 @@ public static function handleException(Throwable $e): int
358379 * @param string $command
359380 * @param callable $handler
360381 * @param array{desc:string,options:array,arguments:array} $config
382+ *
383+ * @return self
361384 */
362- public function add (string $ command , callable $ handler , array $ config = []): void
385+ public function add (string $ command , callable $ handler , array $ config = []): self
363386 {
364- $ this ->addCommand ($ command , $ handler , $ config );
387+ return $ this ->addCommand ($ command , $ handler , $ config );
365388 }
366389
367390 /**
368391 * @param class-string|CmdHandlerInterface $handler
392+ *
393+ * @return self
369394 */
370- public function addHandler (string |CmdHandlerInterface $ handler ): void
395+ public function addHandler (string |CmdHandlerInterface $ handler ): self
371396 {
372397 if (is_string ($ handler )) {
373398 // class string.
@@ -382,15 +407,19 @@ public function addHandler(string|CmdHandlerInterface $handler): void
382407 $ config = $ handler ->metadata ();
383408 $ command = Valid::arrayHasNoEmptyKey ($ config , 'name ' );
384409
385- $ this ->addCommand ($ command , $ handler , $ config );
410+ return $ this ->addCommand ($ command , $ handler , $ config );
386411 }
387412
388413 /**
414+ * Add a command
415+ *
389416 * @param string $command
390417 * @param callable|object|class-string $handler
391418 * @param array{desc:string,options:array,arguments:array} $config
419+ *
420+ * @return self
392421 */
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
394423 {
395424 if (!$ command ) {
396425 throw new InvalidArgumentException ('command name can not be empty ' );
@@ -418,6 +447,7 @@ public function addCommand(string $command, callable|object|string $handler, arr
418447 }
419448
420449 $ this ->metadata [$ command ] = array_merge (self ::COMMAND_CONFIG , $ config );
450+ return $ this ;
421451 }
422452
423453 /**
@@ -628,6 +658,16 @@ public function setParams(array $params): void
628658 }
629659 }
630660
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+
631671 /**
632672 * @return FlagsParser
633673 */
0 commit comments