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


In reply to Why is it good practice for a cli script to take switch args before file list? by leocharre

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.