Hello Monks,

I've been googling the heck out of this but couldn't find anything about it. If anyone will know I know you guys will.

So in my script I'm using the Getopt::Long module.
If for example, I have the following command line command (below):

$ ./my_script.pl --opt1=test1 --cfg=test 2 --location=fake file name

Is there a way to specifiy that "If any $ARGVs are found AFTER the '--cfg=' option then add those to the specified cfg filename separated by a '-' or '_'..."
I used the below code to sort of do what I wanted, but if the options aren't in a specific order it won't work correct. I want to be able to make sure users don't mess that up when executing by putting spaces in the filename.


This code below sort of does what I want, but if say a random word is before/after any other arguments it adds them to the cfg filename as well.

Executing Command with:
./my_script --cfg=my test 2.cfg --location=fake location name
####START CODE my $location; my $user_cfg; #Define argument types GetOptions('location=s' => \$location, 'cfg=s' => \$user_cfg); # Process Command Line Arguments if ( @ARGV > 0 ) { #Check if cfg option was specified if ($user_cfg) { #Replace any spaces found in a "quoted" filename #i.e. ("sample test" --> "sample-test") if ($user_cfg =~ /\s/) { $user_cfg =~ s/\s+/-/g; } if (@ARGV) { foreach my $opt (@ARGV) { $user_cfg .= "-$opt"; } } } } print "location = $location\n"; print "cfg File = $user_cfg\n"; ### END CODE

______ACTUAL OUTPUT______

location = fake
cfg File = my-test-2.cfg-location-name

This is what I would want:
my-test-2.cfg

If that really isn't possible, is there some kind of global variable (like $?, $!, etc...) that holds that entire line just executed through the command line (i.e. a variable that has this as it's value "./my_script --cfg=my test 2.cfg --location=fake location name")...? If there is a variable such as that then I could just parse it myself.


Any suggestions would be great.


Thanks in Advance,
Matt



In reply to Parsing Command Line Options by mmartin

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.