Skip to content

Commit

Permalink
Merge pull request #39 from akash-akya/dev
Browse files Browse the repository at this point in the history
Correct default stderr behaviour
  • Loading branch information
akash-akya authored Jan 9, 2025
2 parents 42463db + 375eb3d commit 165f9eb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
21 changes: 17 additions & 4 deletions lib/ex_cmd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ defmodule ExCmd do
"X 250 X\n"
```
With stderr set to :disable (default)
```
iex> ExCmd.stream!(["sh", "-c", "echo foo; echo bar >&2"])
...> |> Enum.to_list()
["foo\n"]
```
With stderr set to :redirect_to_stdout
```
Expand All @@ -127,14 +135,15 @@ defmodule ExCmd do
"foo\nbar\n"
```
With stderr set to :disable
With stderr set to :console
```
iex> ExCmd.stream!(["sh", "-c", "echo foo; echo bar >&2"], stderr: :disable)
...> |> Enum.to_list()
["foo\n"]
ExCmd.stream!(["sh", "-c", "echo foo; echo bar >&2"], stderr: :console)
|> Enum.to_list()
# STDERR output will be written to the console
```
For more details about stream API, see `ExCmd.stream!/2` and `ExCmd.stream/2`.
For more details about inner working, please check `ExCmd.Process`
Expand Down Expand Up @@ -234,6 +243,8 @@ defmodule ExCmd do
@spec stream!(nonempty_list(String.t()),
input: Enum.t() | collectable_func(),
exit_timeout: timeout(),
cd: String.t(),
env: [{String.t(), String.t()}],
stderr: :console | :redirect_to_stdout | :disable,
ignore_epipe: boolean(),
max_chunk_size: pos_integer()
Expand All @@ -255,6 +266,8 @@ defmodule ExCmd do
@spec stream(nonempty_list(String.t()),
input: Enum.t() | collectable_func(),
exit_timeout: timeout(),
cd: String.t(),
env: [{String.t(), String.t()}],
stderr: :console | :redirect_to_stdout | :disable,
ignore_epipe: boolean(),
max_chunk_size: pos_integer()
Expand Down
10 changes: 5 additions & 5 deletions lib/ex_cmd/process.ex
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ defmodule ExCmd.Process do
You can change the behavior by setting `:stderr`:
1. `:console` - stderr output is redirected to console (Default)
1. `:console` - stderr output is redirected to console
2. `:redirect_to_stdout` - stderr output is redirected to stdout
3. `:disable` - stderr output is redirected `/dev/null` suppressing all output. See below for more details.
3. `:disable` - stderr output is redirected `/dev/null` suppressing all output. See below for more details. (Default)
### Using `redirect_to_stdout`
Expand Down Expand Up @@ -347,9 +347,9 @@ defmodule ExCmd.Process do
These can be accessed in the external program
* `stderr` - different ways to handle stderr stream.
1. `:console` - stderr output is redirected to console (Default)
1. `:console` - stderr output is redirected to console
2. `:redirect_to_stdout` - stderr output is redirected to stdout
3. `:disable` - stderr output is redirected `/dev/null` suppressing all output
3. `:disable` - stderr output is redirected `/dev/null` suppressing all output (Default)
See [`:stderr`](#module-stderr) for more details and issues associated with them
Expand All @@ -361,7 +361,7 @@ defmodule ExCmd.Process do
@spec start_link(nonempty_list(String.t()),
cd: String.t(),
env: [{String.t(), String.t()}],
stderr: :console | :disable | :stream
stderr: :console | :redirect_to_stdout | :disable
) :: {:ok, t} | {:error, any()}
def start_link([cmd | args], opts \\ []) do
opts = Keyword.merge(@default_opts, opts)
Expand Down
2 changes: 1 addition & 1 deletion lib/ex_cmd/process/exec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ defmodule ExCmd.Process.Exec do
defp normalize_stderr(stderr) do
case stderr do
nil ->
{:ok, :console}
{:ok, :disable}

stderr when stderr in [:console, :disable, :redirect_to_stdout] ->
{:ok, stderr}
Expand Down
2 changes: 1 addition & 1 deletion lib/ex_cmd/stream.ex
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ defmodule ExCmd.Stream do
defp normalize_stderr(stderr) do
case stderr do
nil ->
{:ok, :console}
{:ok, :disable}

stderr when stderr in [:console, :redirect_to_stdout, :disable] ->
{:ok, stderr}
Expand Down

0 comments on commit 165f9eb

Please sign in to comment.