There is something up with the way the arguments are processed. The prefix symbol appears to apply escapes whereas the _pattern suffixed symbols do not.

Alongside the equal sign not being documented as mentioned recently in Solved- Getopt::Long- Trying to specify "prefix" configuration option produces error, apparantly you can also supply a regex.

With the proviso the qr is inline, a quoted regex pattern can be supplied to pattern_prefix that appears to do what we need, without the need to import long_pattern_prefix at all.

Though this is fairly simple case in that it is only distinguishing between single or double option introduction characters. On one system, so perhaps there is scope for further regex formulations in combination. It would be easy to expect that there is a kind of default fallback hieararchy to these for varying levels of complexity requirements. Documented intention would be even easier

use strict; use warnings; use Getopt::Long ( qw( :config posix_default gnu_compat bundling no_auto_abbrev no_ignore_case passthrough ), 'prefix_pattern=' . qr/((?<=)(?=--\w{2,})--)|((?<=)(?=-\w+)-)/ ); my $column=1; GetOptions( 'column|c=i' => \$column, # 'c=i'=> \$column, ) or die; # with passthrough, tell the user their option wasnt applied # print map "[$_]", @ARGV, "\n"; print "\$column is $column\n"; die if $column < 1; __END__ $ ~/Desktop/gol.pl --column=2 $column is 2 $ ~/Desktop/gol.pl --column 2 $column is 2 $ ~/Desktop/gol.pl --c=2 $column is 1 $ ~/Desktop/gol.pl --c 2 $column is 1 #'column|c=i' => \$column, $ ~/Desktop/gol.pl -c=2 $column is 1 # 'column=i' => \$column, # 'c=i'=> \$column, $ ~/Desktop/gol.pl -c=2 Value "=2" invalid for option c (number expected) Died at /c/Users/archimediiiica/Desktop/gol.pl line 53. $ ~/Desktop/gol.pl -c2 $column is 2 #passthrough $ ~/Desktop/gol.pl --c 2 [--c][2][ ]$column is 1 #passthrough $ ~/Desktop/gol.pl --c=2 [--c=2][ ]$column is 1

The difference between the alternation in the GetOptions call, is the Warning when -c=2 is not generated

Importing passthrough allows to see that this is still preferable to my earlier @ARGV processing solution, as it keeps the failed option and value pair together, so it could be nice to the user to let them know some option or other was not applied, else they may get unexpected results from default args.


Dont Coyote

In reply to Re: Enforce single hyphen for single character options in Getopt::Long by Don Coyote
in thread Enforce single hyphen for single character options in Getopt::Long by mrnoname1000

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.