Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I take multiple filenames from the Raku command line?

This Raku program works as I expect:

sub MAIN($name) { say "Got $name" }

I can pass a single name on the command line:

$ raku m1.raku foo
Got foo

The obvious extension, however,

sub MAIN(@names) { say "Got $_" for @names }

doesn't work:

$ raku mm.raku foo
Usage:
  mm.raku <names>
$ raku mm.raku foo bar
Usage:
  mm.raku <names>

What am I doing wrong?

like image 576
cjm Avatar asked Jul 03 '21 22:07

cjm


People also ask

How do I replace the beginning of a filename with 1_?

Explanation: the expression s/^/1_/ says: "replace the beginning of the filename (that means this symbol -> ^ )' with 1_ ". obviously take it with care; it will remane ALL the files in the current directory that are 'visible' (filename not starting with a '.') You can use pyRenamer.

How do I rename multiple files in a directory?

Run Ranger and navigate to the directory that has the files you wish to rename. I would recommend putting all the .gifs in one directory. Make sure there are no other files except the ones you want to rename.

What does $ $do in a file name?

$ anchors the match to the end of the filename. So the dot, the final digits, and the suffix .gif are matched, and all replaced with just .gif. Show activity on this post.

Is it possible to write a shorter rename command?

It's possible to write a shorter rename command that ought to work. I've chosen this approach--among many possible approaches--because the command expresses precisely the naming scheme that you wish to operate on. This is to say that the solution resembles the problem.


1 Answers

What @cjm said.

However, you can go a little further than that, checking whether the names you specified, are actually files. And produce an error message if they are not. The trick is to use multi-dispatch:

subset File of Str where *.IO.f;

multi sub MAIN(*@files where @files.all ~~ File) {
    say "These are all files: @files.join(",")";
}
multi sub MAIN(*@files) {
    say "These are *NOT* files: @files.grep(* !~~ File).join(",")";
}

The first candidate will be run if all the names specified on the command line are in fact files. The second candidate will be run if the first didn't fire, implying that not all names specified are in fact files.

like image 144
Elizabeth Mattijsen Avatar answered Nov 23 '22 19:11

Elizabeth Mattijsen