Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 13th, 2012, 12:48 PM
hcrosex3 hcrosex3 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 52 hcrosex3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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";



Reply With Quote
  #2  
Old August 13th, 2012, 01:04 PM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 511 Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 4 Days 19 h 57 m 48 sec
Reputation Power: 405
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.

Reply With Quote
  #3  
Old August 13th, 2012, 01:15 PM
hcrosex3 hcrosex3 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 52 hcrosex3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old August 13th, 2012, 01:54 PM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 511 Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 4 Days 19 h 57 m 48 sec
Reputation Power: 405
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.

Reply With Quote
  #5  
Old August 13th, 2012, 04:13 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,389 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 22 h 1 m 11 sec
Reputation Power: 4080
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Creating options for your script?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap