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

Support use of curly braces in commands when using regular expression #9

Closed aaronriekenberg closed 11 months ago

aaronriekenberg commented 12 months ago

Reported by @jyrimatti :

thanks for the feature! ...but with the command having any other use of curly braces breaks completely, for example:

> echo 'input' | rust-parallel -r '.*' echo 'as json: {"id":123, "input":{0}}'
as json: }

I guess a good-enough fix would be to only consider references of format {[a-zA-Z0-9_]+} (or something similar) and nothing else?

aaronriekenberg commented 12 months ago

This is because we replace all { characters with ${ here before calling expand

As suggested above could try for more specific format to replace like {[a-zA-Z0-9_]+} before calling expand.

Will work on this. Thanks for reporting @jyrimatti !

aaronriekenberg commented 12 months ago

Fixed in version 1.10.0. Added an example for this https://github.com/aaronriekenberg/rust-parallel/wiki/Examples#processing-csv-inputs-using-regular-expression

Thanks @jyrimatti !

jyrimatti commented 12 months ago

Thanks, that was quick :)

Another related thing I noticed is that dollar signs aren't escaped, which results in somewhat surprising behavior. It seems (perhaps) that a $ can be part of the command only if not followed by a letter, or if followed by parentheses:

> echo 'input' | rust-parallel -r '.*' -s 'foo={0}; echo $foo 1$ "$foo" "$(echo bar)"' 
1$  bar

The correct behavior can be achieved by escaping the dollar sign:

> echo 'input' | rust-parallel -r '.*' -s 'foo={0}; echo $$foo 1$$ "$$foo" "$$(echo bar)"'
input 1$ input bar

I don't know Rust and I don't know how you might want to solve this, since having to deal with both shell-escaping and regex-escaping is becoming a bit complex. Viable option might be at least:

  1. Automatically escape $ and other special characters
  2. Mention this somehow in the documentation
aaronriekenberg commented 12 months ago

Thanks @jyrimatti for testing and reporting this.

Will give some thought on how to solve this. Reopening this issue for now.

aaronriekenberg commented 11 months ago

@jyrimatti Made updates so that $ characters are escaped by the program before the expand function is called.

This fixes your scenario above. Made a release 1.10.1 - could you please test?

Have some possible thoughts on a design that does not use the regex expand function at all that might be simpler. Will experiment with this.

Also need to update the manual and examples with something similar to above as more things are becoming possible now :)

Thanks!

aaronriekenberg commented 11 months ago

Decided to stay with expand function approach in parser::regex::RegexProcessor

Did a bit of code cleanup to RegexProcessor

Updated the manual:

  1. Added section on regular expression capture group special characters: https://github.com/aaronriekenberg/rust-parallel/wiki/Manual#capture-group-special-characters
  2. Added shell commands section: https://github.com/aaronriekenberg/rust-parallel/wiki/Manual#shell-commands

Above changes are released in version 1.10.2

@jyrimatti I think issues you have reported are fixed and documentation is updated. Please comment or reopen if you find more issues. Thank you! 😄

jyrimatti commented 11 months ago

Thanks, seems to work for my use cases 👍