aaronriekenberg / rust-parallel

Fast command line app in rust/tokio to run commands in parallel. Similar interface to GNU parallel or xargs plus useful features. Listed in Awesome Rust utilities.
MIT License
146 stars 7 forks source link

[feature request] support --no-run-if-empty switch like parallel does #11

Closed IceCodeNew closed 7 months ago

IceCodeNew commented 7 months ago

What happened

test.sh

#!/bin/bash
function subfunc() {
    _var="$1"
    if [[ -z "_var" ]]; then
        exit 1
    fi
}
function list() {
    echo '
1
2
3
4
5
'
}

export -f subfunc
export -f list

function mainfunc() {
    _func='subfunc'
    list \
        | rust-parallel --dry-run --jobs 3 --shell $_func \
        | tee -a /dev/null

    # $_func "1"
}

mainfunc

bash test.sh

2024-01-25T07:54:05.416361Z  INFO rust_parallel::command: cmd="/bin/bash",args=["-c", "subfunc"],line=stdin:1
2024-01-25T07:54:05.416413Z  INFO rust_parallel::command: cmd="/bin/bash",args=["-c", "subfunc 1"],line=stdin:2
2024-01-25T07:54:05.416425Z  INFO rust_parallel::command: cmd="/bin/bash",args=["-c", "subfunc 2"],line=stdin:3
2024-01-25T07:54:05.416432Z  INFO rust_parallel::command: cmd="/bin/bash",args=["-c", "subfunc 3"],line=stdin:4
2024-01-25T07:54:05.416439Z  INFO rust_parallel::command: cmd="/bin/bash",args=["-c", "subfunc 4"],line=stdin:5
2024-01-25T07:54:05.416446Z  INFO rust_parallel::command: cmd="/bin/bash",args=["-c", "subfunc 5"],line=stdin:6
2024-01-25T07:54:05.416453Z  INFO rust_parallel::command: cmd="/bin/bash",args=["-c", "subfunc"],line=stdin:7

What is expected

To avoid rust-parallel calls command /bin/bash -c subfunc, since the subfunc is designed to work with exactly one arg from the input.

Is there any solution?

Yes, it would be nice to have a switch telling rust-parallel not to run empty input, as GNU Parallel does.

GNU Parallel manual

   --no-run-if-empty
   -r  Do not run empty input.

       If the stdin (standard input) only contains whitespace, do not run the command.

       If used with --pipe this is slow.

       See also: command --pipe --interactive
aaronriekenberg commented 7 months ago

Thanks @IceCodeNew will add this.

aaronriekenberg commented 7 months ago

Fixed in v1.14.0

https://github.com/aaronriekenberg/rust-parallel/releases/tag/v1.14.0

Thanks for reporting @IceCodeNew

IceCodeNew commented 7 months ago

Thanks for taking the time to implement it. I will give it a try ;-)