denoland / deno_task_shell

Cross-platform shell for deno task.
https://crates.io/crates/deno_task_shell
MIT License
108 stars 18 forks source link

[feature request] Support xargs -I <replace_str> #77

Open niedzielski opened 1 year ago

niedzielski commented 1 year ago

The xargs command is super useful but is too constrained to use for invocations that depend on positional parameters. This feature request asks for the traditional -I/-i/--replace flags to be added.

Eg:

$ echo -e 'a\nb\nc'|xargs -I arg echo 1 arg 2
1 a 2
1 b 2
1 c 2

Unfortunately, -I always constrains the maximum number of lines processed at a time to 1 and approximates the following loop:

$ for i in a b c; do echo 1 $i 2; done
1 a 2
1 b 2
1 c 2

Although both looping and xargs -I provide similar functionality, I think they serve different uses. Simple loops are wanted for simple lists and xargs is wanted for pipes.

Use case

In the absence of this support, task users are at the mercy of the subcommands they call. For example, here's how you would have to do copy with the current version of xargs:

$ echo -e 'a\nb\nc'|xargs cp -t dist

However, many tools don't support non-positional arguments like the target parameter so this isn't an option. Even Deno's own cp command doesn't support -t so this also fails:

{
  "tasks": {
    "hello": "echo -e 'a\nb\nc'|xargs cp -t dist"
  }
}
$ deno task hello
Task hello echo -e 'a
b
c'|xargs cp -t dist
cp: unsupported flag: -t

References

dsherret commented 1 year ago

It would be good to split these out into two issues. Looping probably won't be implemented because if you are doing loops then you probably shouldn't be writing that in a task and instead use JavaScript for that (deno run ...), but having feature parity with xargs is desirable.

niedzielski commented 1 year ago

Thanks, I've made the split. Feel free to close the loop issue if it doesn't make sense--I opened it because I thought it was likely to come up again and it'd be nice to centralize that discussion.

dsherret commented 1 year ago

@niedzielski thanks! I'll keep it open for now as a "suggestion" to see what other use cases come up.

j-murata commented 2 months ago

Excuse me for interrupting.

I posted https://github.com/denoland/deno/discussions/25248 in the Discussions of the deno repository (as I was unaware of the existence of this deno_task_shell repository), but if this xargs -I feature request is implemented in the future, could it be a solution to my problem?

» How do I insert the deno task argument into the middle of that command?