Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Command Line Options without "-" or "- -"

by Smaug (Pilgrim)
on May 16, 2006 at 15:25 UTC ( [id://549798] : perlquestion . print w/replies, xml ) Need Help??

Smaug has asked for the wisdom of the Perl Monks concerning the following question:

Hi all

I have a job scheduling engine which can call a script on job failure. The script is parsed a series of values in the format:
call_type: "value" alert_id: "value"message: "value"

I have looked at Getopt but I can't get it to work since there are no preceeding - or - -.
I then tried to split @ARGV on ": " but found that sometimes the data has a ": " in it!!!
The above is slightly simplified for posting length, there are 16 values parsed each time the script is called.

Any help or point in the right direction would be great

Thanks!!!
  • Comment on Command Line Options without "-" or "- -"

Replies are listed 'Best First'.
Re: Command Line Options without "-" or "- -"
by izut (Chaplain) on May 16, 2006 at 16:07 UTC

    What about this, before parsing options using Getopt::Long, assuming the values will never have an ending ':' (untested):

    @ARGV = map { s/^(.*):$/--$1/ if /:$/; $_ } @ARGV;

    Igor 'izut' Sutton
    your code, your rules.

Re: Command Line Options without "-" or "- -"
by Smaug (Pilgrim) on May 16, 2006 at 15:28 UTC
    Ok..... Sorry!!! I got it working by doing this:
    while (<@ARGV>) { my $arg = shift @ARGV; @options = split(/: /,$arg); print "XXXXXXXX\t", $options[$_], "\tXXXXXXXX"; }

    But it still does not help me with the ": " being in the data sometimes.

    Thanks.
      If you have a space after the token, and your value is in quotes, the shell should separate them and put each one in a different item of ARGV.... it looks like you have that space, but I could be wrong.

      Regardless, split takes an optional third argument which limits how many array items are returned. So put a 2 at the end of your split.

                      - Ant
                      - Some of my best work - (1 2 3)

Re: Command Line Options without "-" or "- -"
by QM (Parson) on May 16, 2006 at 18:16 UTC
    I would have gone this way:
    $option_line = join ' ', @ARGV; #reconstitute my @pairs = $option_line =~ m/(\w+:\s+\w+)/g; # grab option pairs my %option; for $pair (@pairs) { # now split my ($name,$value) = split ":\s+", $pair, 2; # save each pair in hash $option{$name} = $value; }
    Or you could use G::D, but this might be overkill:
    our %opt_hash; #can't use my use Getopt::Declare; my $spec = q{ <name>: <value> Name/Value pair { $::opt_hash{$name} = $value; } [repeatable] }; # untested
    (I can't recall if whitespace is automagically optional, and I have to run %/;)

    Update: Missing $option_line corrected.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      $option_line = join ' ', @ARGV; #reconstitute my @pairs = m/(\w+:\s+\w+)/g; # grab option pairs
      You missed matching against "$option_line" ...
      my @pairs = ( $option_line =~ m/(\w+:\s+\w+)/g );