I've seen lots of scripts where you can invoke them with flags followed by parameters (hopefully that's the right terminology)like this:
./myscript.pl -a param_a -b param_b -d param_d -c param_c input1 input
+2
The problem is I don't know how to take that input and use it in the script. I know how to use @ARGV, but that would mean no flags and the params would have to be in the correct and same order every time.
The only solution I can think of right now is to loop through @ARGV.
Check if the value starts with a "-"
if it does then figure out which flag it is and assign the correct variable with the next value in @ARGV
if the value doesn't start with a "-" and the previous value doesn't either, then take that as normal input and proceed with the script.
if the value doesn't start with a "-" but the previous value does, then treat it as the value for that flag
Something about this just says to me there has to be a better way. Is there?
Thanks in advance
Edit: Big thanks to
borisz for the first, most concise, and most useful answer. Thanks to everyone else as well.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.