Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #61 Add form feed to trimmed characters #62

Merged
merged 1 commit into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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