|P is for Practical|
Parsing Command Line Optionsby mmartin (Monk)
|on Jan 12, 2012 at 20:35 UTC||Need Help??|
mmartin has asked for the
wisdom of the Perl Monks concerning the following question:
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
location = fake
cfg File = my-test-2.cfg-location-name
This is what I would want:
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,