http://qs321.pair.com?node_id=562315

leocharre has asked for the wisdom of the Perl Monks concerning the following question:

I have a cli script that takes as arguments a file path, and optionally, some switches. I am using Getopt::Std right now.

I was having some trouble with the interface. I wanted the user to have to specify switches (-f-a -w hatever) - but I wanted the script to catch any ARGV that does not have a switch and take it as a file argument..

I wanted the following to be valid ways to call the script:

script ./thisfile -f -a
script -f -a ./thisfile
script -f -a ./thisfile ./thisfilealso

Getopt::Std allows me to do this.. but if I specify the arguments first, then the file arguments
metadata -f value -a nothervalue ./thisfile
But if I specify files first, it ignores switches or the file arguments

This is where I get the options..

my $self = { opt => {}, }; getopts('f:v:i:a',$self->{opt}); # -f field -v value -i # what file(s) did user specify. my $files = @ARGV; # only gets fed if arg flags are called via cli bef +ore the file list

Upon further reading, I see that it is widely recomended (for example in Getopt:Long ) that one provide switch arguments first, and then a file list..
And off the top of my head, I think most cli utilities take args before paths.. Why?

I think in the case of this script I'm working on.. It may be more convenient to specify path first. The script is an interface to showing and editing metadata on a file or directory .. here are some usage examples:

metafile ./path/2/file
metafile ./path/2/file -f author -v Joe
metafile ./path/2/file -f author -v Joe -a
metafile ./path/2/file -f author

the constant is that they provide filepath(s).. so I feel silly asking them to turn the above examples to..

metafile ./path/2/file
metafile -f author -v Joe ./path/2/file
metafile -f author -v Joe -a ./path/2/file
metafile -f author ./path/2/file