Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-19 06:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found