The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Perl Programming
|
Creating options for your script?
Discuss Creating options for your script? in the Perl Programming forum on Dev Shed. Creating options for your script? Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 13th, 2012, 12:48 PM
|
|
Contributing User
|
|
Join Date: May 2012
Posts: 52
Time spent in forums: 10 h 19 m 15 sec
Reputation Power: 1
|
|
|
Creating options for your script?
I would like to make it that a certain command is ran based on what the user puts in when running it from the the terminal. for example.
rose_prog.pl -cs -ll
would run a give the output
of running the command bhosts -w -R 'cs&&ll'
I have everything done as for giving my output my issue is how do Iwrite a script that takes what the person put into the terminal and creates a string with each option being seperated by &&.
Right now I have it set to accepting just -corp I want it to accept multiple options not just -corp but whatever they type in as long as the name exists within the $resource_name hash. From there it should build a string.
PHP Code:
foreach my $resource (@lsinfo) {
@words = split /\s+/, $resource;
$resource = $words[0];
$resource_name{$resource} = 1;
}
# Process arguments
while ($next_arg = shift @ARGV) {
if ( $next_arg eq "-l") {
$output = 1;
next;
}
if ($next_arg eq "-corp"){
$host_name = $next_arg;
next;
}
print " $next_arg is not a valid choice \n";
}
|

August 13th, 2012, 01:04 PM
|
|
|
|
For processing command line arguments, take a look at the Getopts, Getopts::Long and related Perl modules.
Using a hash as a dispatch table might also be better that a series of if (), unless you have only two, of course.
For the reste, I did not understand quite well what you want to do, but if you hust want to join two or several arguments separating them with &&, look at the join function.
|

August 13th, 2012, 01:15 PM
|
|
Contributing User
|
|
Join Date: May 2012
Posts: 52
Time spent in forums: 10 h 19 m 15 sec
Reputation Power: 1
|
|
|
I added in the join yes that is what I was trying to do. My hash includes multiple names such as
CS
dekstop
sever
I want the user to be able to only select a name which is within that hash.
so if they did rose_prog -cs -server
it would then build the string $hostname = cs&&server
if they typed rose_prog -cs -bobby.
it would report that bobby isn't a choice.
I'm not sure if you can use getops with a hash.
|

August 13th, 2012, 01:54 PM
|
|
|
|
You can't really prevent the user from typing a wrong option (well, you probably can, but what it takes is probably overkill), but it is quite easy to loop until the user enters a valid choice.
|

August 13th, 2012, 04:13 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
My favorite module for this type of work is Getopt::Euclid. It builds the list of getopt options based on the POD documentation that you add to your program, so your docs and options will never go out of sync.
It also knows to handle invalid options. If you don't document an option, then Getopt::Euclid will automatically throw an error and say that the option is invalid.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|