Here is what I am trying to do. I have a command line option that is defined in Getopt::Long as having an optional string value as follows:

use Getopt::Long; GetOptions( "ot:s" => \$ot ) or die("Error in command line arguments\n +");

The problem occurs when -ot (without a value) is followed by either another option or a program argument. The -ot option will take the next word as its value.

I know I can signal the end of options by using -- on the command line, but for backward compatibility before this program used Getopt::Long (the program previously used "/usr/bin/perl -s" to process command line options), I would like to tell Getopt::Long to always require an equals sign before a value so that -ot (without a value) can be followed by a program argument without the invervening -- in the command line.

Here is what I am aiming for:

Scenario #1:

myprog.pl -ot=123 arg1 arg2 ...

This works fine. Variable $ot='123'

Scenario #2:

myprog.pl -ot arg1 arg2 ...

I need $ot to be set to the empty string '' instead of 'arg1', without the need of an intervening -- on the command line (hence the need to force Getopt::Long to require an equals sign between the option name and its value).

Again, my goal is backward compatibility with the way this program was called before adding Getopt::Long, because this program is very central to the system and is literally called in thousands of places. TIA for any suggestions.

"It's not how hard you work, it's how much you get done."


In reply to Force Getopt::Long to Require ONLY Equals Sign Between Option Name and Value by roho

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.