@@ -43,6 +43,8 @@ Single file console framework to help you write scripts quickly, **v2.0 requires
4343 * [ Run Sub-Process] ( #run-sub-process )
4444 * [ Hide Command Name] ( #hide-command-name )
4545 * [ Custom Output] ( #custom-output )
46+ * [ Disable the Output] ( #disable-the-output )
47+ * [ Override ` exec() ` ] ( #override-exec )
4648 * [ Delegating Multiple Tasks] ( #delegating-multiple-tasks )
4749 * [ Contributing and PR is Welcome] ( #contributing-and-pr-is-welcome )
4850<!-- TOC -->
@@ -806,11 +808,16 @@ Use `exec()` to run a sub-process, it will instantly print the output and return
806808$app->exec('ls');
807809$app->exec('git status');
808810$app->exec('git commit ...');
809- $code = $app->exec('git push');
811+ $result = $app->exec('git push');
810812
811- if ($code !== 0) {
813+ // All output will instantly print to STDOUT
814+
815+ if ($result->code !== 0) {
812816 // Failure
813817}
818+
819+ $result->code; // 0 is success
820+ $result->success; // BOOL
814821```
815822
816823Use ` mustExec() ` to make sure a sub-process should run success, otherwise it will throw an exception.
@@ -853,23 +860,50 @@ $log = '';
853860
854861$app->exec(
855862 'cmd ...',
856- function (string $data, bool $err) use ($app, & $log) {
863+ output: function (string $data, bool $err) use ($app, & $log) {
857864 $app->write($data, $err);
858865
859866 $log .= $data;
860867 }
861868);
862869```
863870
871+ ### Disable the Output
872+
873+ Use ` false ` to disable the output, you can get full output from result object after sub-process finished.
874+
875+ Note, the output will only write to result object if ` output ` set to ` false ` . If you set ` output ` as closure or
876+ keep default ` NULL ` , the output will be empty in result object.
877+
878+ ``` php
879+ $result = $app->exec('cmd ...', output: false);
880+
881+ $result->output; // StdOutput of sub-process
882+ $result->errOutput; // StdErr Output of sub-process
883+
884+
885+ // Below will not write to the result object
886+ $result = $app->exec('cmd ...');
887+ // OR
888+ $result = $app->exec('cmd ...', output: function () { ... });
889+
890+ $result->output; // Empty
891+ $result->errOutput; // Empty
892+ ```
893+
894+ ### Override ` exec() `
895+
864896By now, running sub-process by ` prop_open() ` is in BETA, if ` prop_open() ` not work for your environment, simply override
865897` exec() ` to use PHP ` system() ` instead.
866898
867899``` php
868- public function exec(string $cmd, \Closure|null $output = null, bool $showCmd = true): int
900+ public function exec(string $cmd, \Closure|null $output = null, bool $showCmd = true): ExecResult
869901{
870- $this->writeln('>> ' . $cmd);
902+ !$showCmd || $this->writeln('>> ' . $cmd);
903+
904+ $returnLine = system($cmd, $code);
871905
872- return system($cmd );
906+ return new \Asika\SimpleConsole\ExecResult($code, $returnLine, $returnLine );
873907}
874908```
875909
0 commit comments