thinking out loud

richardc on 2002-11-28T11:21:00

I just sent this out, and then remembered that the fine people of use.perl have made good suggestions about this in the past, so here we go.


On Tuesday, Nov 26, 2002, at 11:36 Europe/London, Tatsuhiko Miyagawa                                                                
wrote:                                                                                                                              
>BTW, why not including a executable script to replace /usr/bin/find in                                                             
>File::Find::Rule distribution?                                                                                                     
>                                                                                                                                   
>  % findrule -type file -mp3 title="youth gone wild" -mp3 length="3min" /mp3                                                                                                                 
>  % findrule -md5 blahblahblah /archives                                                                                           
>  % findrule -type file -image_x 120                                                                                               
                                                                                                                                    
It's a good idea, so I started on one yesterday[1].  Unfortunately I                                                                
quickly hit on a problem of how to group without splitting arguments in                                                             
a way that wouldn't be ideal.                                                                                                       
                                                                                                                                    
What I currently have is something that works like so:                                                                              
                                                                                                                                    
 % findrule file name [ red blue ]                                                                                                  
 # files called red or blue, below the current directory                                                                            
                                                                                                                                    
 % findrule file name red blue                                                                                                      
 # files called red, below the directory called blue                                                                                
                                                                                                                                    
But it kindof expects you to know what takes arguments (like name) and                                                              
what doesn't (like file), which I'm not sure is so great.  There's                                                                  
possibly adding - to indicate that you mean a clause, but then what if                                                              
you really want to pass something starting with - as an argument.  Hmmm.                                                            
                                                                                                                                    
 % findrule -file -name red blue                                                                                                    
 # files called red or blue, below the current directory                                                                            
                                                                                                                                    
 % findrule -file -name red -- blue                                                                                                 
 # files called red, below the directory called blue                                                                                


 % findrule -file -name -name                                                                                                       
 # syntax error?  I really wanted files called -name                                                                                
                                                                                                                                    
I'm more just thinking out loud right now, but if you have any                                                                      
suggestions I'd be glad to steal them from you.                                                                                     
                                                                                                                                    
Thanks                                                                                                                              
                                                                                                                                    
[1] http://mirth.unixbeard.net/svn/richardc/perl/File-Find-Rule/findrule                                                            
                                                                                                                                    
--                                                                                                                                  
Richard Clamp                                                                                               


PPT

nwetters on 2002-11-28T12:00:20

I guess if you're building a workalike for unix find, you should follow the syntax used by that command (probably the GNU version, or maybe BSD if that's what floats your boat).

Either way, it's probably worth letting tomc know about your script, as the version of find currently on the Perl Power Tools site doesn't implement a lot of the find functionality.

RE: PPT

richardc on 2002-11-28T12:38:59

Ah yes, *lightbulb*. I can steal the GNU convention of putting where first, rather than last, giving us:

% findrule -file -name red blue
# find files called red or blue, below the current directory

% findrule blue -file -name red
# find files called red, below the directory blue

Though I think it still leaves the ambiguity about finding things named -*, maybe we could use '[' ']' to disambiguate that (of course then we can't find things called '[' or ']'

% findrule -file -name [ -red ]
# find files called -red, below the current directory

wildcards

barbie on 2002-11-28T16:01:06

If the -* is entered on the command line, in all Unices I've used, the shell will pre-populate the list. To get around it using "-*" will allow the string to be passed as an argument. Perhaps you could then specifically check for the wildcard character when reading the arguments and options, and deal with it appropriately.

re: wildcards

richardc on 2002-11-28T16:39:07

If the -* is entered on the command line, in all Unices I've used, the shell will pre-populate the list. To get around it using "-*" will allow the string to be passed as an argument.

Sorry, I think you've taken my ramblings a bit too literally. I was talking of nailing down the meaning of -name -red. If I apply a simple "things matching /^-/ are commands" rule then that translates to the Perl ->name()->red(), so how would I get to ->name( '-red' ), which is what I really wanted.

Perhaps you could then specifically check for the wildcard character when reading the arguments and options, and deal with it appropriately.

File::Find::Rule->name quite happily copes with globbing patterns by delegating to Text::Glob, so we already have that one licked.

...escaping

barbie on 2002-11-28T21:16:36

Quotes could still be used to escape a file ("-red") or pattern ("-*") that could be misled as an option name. Although the use of [] might be obvious in Perl syntax, I've not come across that style of grouping command line arguments. Not that that is a reason not to use them.

Nope.

Dom2 on 2002-11-29T15:38:58

You can't do that. Well, you could, but you'd have to double-quote, ie:

-name '"-red"'

Just to get around the shells quoting. Ewww.

-Dom

Don't use '[' and ']' in command lines

htoug on 2002-12-03T07:35:54

Korn and Bash shells use [ .. ] as a shorthand for test .., so it would be unwise to use those characters.

Grouping characters

richardc on 2002-12-03T10:26:47

Korn and Bash shells use [ .. ] as a shorthand for test .., so it would be unwise to use those characters.

Odd, not from my the bash on my box, and not in a script either.[0]

This was giving me a little trouble before though, more for consistency with find(1) than anything, so I switched it over to using '(' and ')' as parameters. The hard part is finding a set of grouping characters that aren't significant in any shell, since () themselves are used by bash and zsh.

[0]

bash-2.05a$ ls [ foo ]
ls: [: No such file or directory
ls: ]: No such file or directory
foo
bash-2.05a$ cat foo
#!/bin/bash
ls [ foo ]
bash-2.05a$ ./foo
ls: [: No such file or directory
ls: ]: No such file or directory
foo