Marcel now has an operator, `args`, that works like Linux's `xargs`.
For example, suppose you have a directory containing log files, and you want to
delete logs that are more than 100 days old. Locating
such files is easy:
shell script
ls -f | select (f: now() - f.mtime > days(100))
What you would like to do is to take each `File` in the resulting stream, and
delete it. The `args` operator takes items arriving on the input
stream, and makes them available to operators. So to do the file removal:
shell script
ls -f | select (f: now() - f.mtime > days(100)) | args [f: rm (f)]
- `ls ... | select ...` produces a stream of `File`s.
- These `File`s flow into the `args` input stream.
- `args` has a pipeline with parameter `f`. Each arriving `File` is bound to `f`, and
the pipeline is executed.
- `rm (f)` removes `File` `f`.
You can also pass all of the qualifying `File`s at once:
shell script
ls -f | select (f: now() - f.mtime > days(100)) | args --all [files: rm (quote_files(files))]
Now, all of the qualifying `File`s are bound to the pipelines `files` parameter.
Because the args pipeline relies on the host OSs `rm` command, carefully quoting
filenames is necessary. `quote_files` takes `files`, a `list` of `File`s, and returns
a string containing all the `File`s names, separated by spaces, and each quoted, (e.g.
to properly handle a filename such as `this filename has four spaces`.)
Actually, you can do the same cleanup by relying on the fact that `File`s implement
the `pathlib.Path` interface, which has an `unlink` method. So this works too:
shell script
ls -f | select (f: now() - f.mtime > days(100)) | map (f: f.unlink())