Skip to content

Commit

Permalink
Merge pull request #62 from mikehaertl/61-trim-form-feed
Browse files Browse the repository at this point in the history
Issue #61 Add form feed to trimmed characters
  • Loading branch information
mikehaertl authored Apr 19, 2023
2 parents 6c716ad + 56e1acf commit e79ea52
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
php-shellcommand
===========
================

[![GitHub Tests](https://github.com/mikehaertl/php-shellcommand/workflows/Tests/badge.svg)](https://github.com/mikehaertl/php-shellcommand/actions)
[![Packagist Version](https://img.shields.io/packagist/v/mikehaertl/php-shellcommand?label=version)](https://packagist.org/packages/mikehaertl/php-shellcommand)
Expand Down Expand Up @@ -61,15 +61,15 @@ $command->addArg('--name=', "d'Artagnan");

// Add argument with several values
// results in --keys key1 key2
$command->addArg('--keys', array('key1','key2'));
$command->addArg('--keys', ['key1','key2']);
```

### Pipe Input Into Command

From string:
```php
<?php
$command = new ('jq') // jq is a pretty printer
$command = new ('jq'); // jq is a pretty printer
$command->setStdIn('{"foo": 0}');
if (!$command->execute()) {
echo $command->getError();
Expand Down Expand Up @@ -115,19 +115,19 @@ fclose($fh);
```php
<?php
// Create command with options array
$command = new Command(array(
$command = new Command([
'command' => '/usr/local/bin/mycommand',

// Will be passed as environment variables to the command
'procEnv' => array(
'procEnv' => [
'DEMOVAR' => 'demovalue'
),
],

// Will be passed as options to proc_open()
'procOptions' => array(
'procOptions' => [
'bypass_shell' => true,
),
));
],
]);
```

## API
Expand Down Expand Up @@ -185,7 +185,7 @@ pass `command`, `execCommand` and `args` as options. This will call the respecti
and `=`, the (optional) `$value` will be separated by a space. The key will get
escaped if `$escapeArgs` is `true`.
* `$value`: The optional argument value which will get escaped if `$escapeArgs` is `true`.
An array can be passed to add more than one value for a key, e.g. `addArg('--exclude', array('val1','val2'))`
An array can be passed to add more than one value for a key, e.g. `addArg('--exclude', ['val1','val2'])`
which will create the option "--exclude 'val1' 'val2'".
* `$escape`: If set, this overrides the `$escapeArgs` setting and enforces escaping/no escaping
* `setStdIn()`: String or resource to supply to command via standard input.
Expand Down
18 changes: 12 additions & 6 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,30 +328,36 @@ public function addArg($key, $value = null, $escape = null)

/**
* @param bool $trim whether to `trim()` the return value. The default is `true`.
* @param string $characters the list of characters to trim. The default
* is ` \t\n\r\0\v\f`.
* @return string the command output (stdout). Empty if none.
*/
public function getOutput($trim = true)
public function getOutput($trim = true, $characters = " \t\n\r\0\v\f")
{
return $trim ? trim($this->_stdOut) : $this->_stdOut;
return $trim ? trim($this->_stdOut, $characters) : $this->_stdOut;
}

/**
* @param bool $trim whether to `trim()` the return value. The default is `true`.
* @param string $characters the list of characters to trim. The default
* is ` \t\n\r\0\v\f`.
* @return string the error message, either stderr or an internal message.
* Empty string if none.
*/
public function getError($trim = true)
public function getError($trim = true, $characters = " \t\n\r\0\v\f")
{
return $trim ? trim($this->_error) : $this->_error;
return $trim ? trim($this->_error, $characters) : $this->_error;
}

/**
* @param bool $trim whether to `trim()` the return value. The default is `true`.
* @param string $characters the list of characters to trim. The default
* is ` \t\n\r\0\v\f`.
* @return string the stderr output. Empty if none.
*/
public function getStdErr($trim = true)
public function getStdErr($trim = true, $characters = " \t\n\r\0\v\f")
{
return $trim ? trim($this->_stdErr) : $this->_stdErr;
return $trim ? trim($this->_stdErr, $characters) : $this->_stdErr;
}

/**
Expand Down

0 comments on commit e79ea52

Please sign in to comment.