Hello harpreetsammi, and welcome to the Monastery!

You can use a subroutine to validate the argument. For example:

#! perl use strict; use warnings; use Getopt::Long; my ($name, $long); GetOptions ( 'name=s' => \&name_handler, long => \$long, ) or die "Error in command line arguments\n"; printf "Name is '%s', long is %s\n", $name, $long ? 'set' : 'not set'; sub name_handler { my (undef, $n) = @_; if ($n =~ /^--/) { die "Option 'name' requires an argument\n"; } else { $name = $n; } }

Output:

2:11 >perl 1098_SoPW.pl --name Fred --long Name is 'Fred', long is set 2:11 >perl 1098_SoPW.pl --name --long Option 'name' requires an argument Error in command line arguments 2:11 >

See Getopt::Long#User-defined-subroutines-to-handle-options.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Getopt::Long treating option as an argument by Athanasius
in thread getopt::long treating option as an argument by harpreetsammi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.