Command line builder utilities.
use Fernandokbs\ShellBuilder\ShellBuilder;
ShellBuilder::create('ruby')
->withFlag('-v')
->getExecuteCommand();
# output = ruby -v
ShellBuilder::create('ls')
->withFlags(['-l', '-a'])
->getExecuteCommand();
# output = ls -l -a
ShellBuilder::create('ruby')
->withFlag('-v')
->withOption('-e', 'puts "Hello World"')
->getExecuteCommand();
# output = ruby -v -e puts "Hello"
or withOptions
, either as an array:
ShellBuilder::create('gpg')
->withOptions([
'--recipient' => '[email protected]',
'--sign' => './doc.txt',
])
->getExecuteCommand();
# output = gpg --recipient [email protected] --sign ./doc.txt
ShellBuilder::create('diff')
->withArgument('./file1.txt')
->getExecuteCommand();
# output = diff ./file1.txt
or #withArguments
, either as a array:
ShellBuilder::create('diff')
->withArguments(['./file1.txt', './file2.txt'])
->getExecuteCommand();
# output = diff ./file1.txt ./file2.txt
Command lines can be prefixed with environment variables using withEnvironmentVariable
ShellBuilder::create('php')
->withEnvironmentVariable('APP_ENV', 'testing')
->getExecuteCommand();
# => APP_ENV=testing php'
or withEnvironmentVariables
, either as an array:
hellBuilder::create('php')
->withEnvironmentVariables([
'APP_ENV' => 'testing',
'APP_DEBUG' => 'true',
])
->getExecuteCommand();
# => APP_ENV=testing APP_DEBUG=true php
Subcommands can be added using withSubcommand
ShellBuilder::create('git')
->withFlag('--no-pager')
->withSubCommand('log')
->getExecuteCommand();
# => git --no-pager log
Subcommands also support options via withFlag
, withFlags
, withOption
, withOptions
, via a closure:
ShellBuilder::create('git')
->withFlag('--no-pager')
->withSubCommand('log', function ($builder) {
$builder->withFlag('--oneline');
})
->getExecuteCommand();
# => git --no-pager log --oneline