#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    52
    Rep Power
    3

    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";


  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    828
    Rep Power
    496
    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.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    52
    Rep Power
    3
    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.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    828
    Rep Power
    496
    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.
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    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

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo