hello mrnoname1000 and welcome to the Monastery.

Such requirements will probably subvert system conventions on some or other systems.

However, a slight manipulation of the @ARGV at the right place will allow you to restrict to requirement.

The return value from the grep will be true in scalar context, so you could use the expression as a conditional rather than assignement if you further wanted to amend the default value in some way based on this format of option.

#!/usr/bin/perl use strict; use warnings; use Getopt::Long qw( :config posix_default gnu_compat bundling no_auto_abbrev no_ignore_case ); my $column=1; @ARGV = grep $_ !~ /\A--c\b/, @ARGV; #if( grep s/\A--c\b=?.*//, @ARGV ){ $column = 100 }; # \b can be replaced with (?!o) for finer resolution #if( grep s/\A--c(?!o)=?.*//, @ARGV ){ $column = 100 }; GetOptions( 'column|c=i' => \$column, ) or die; print "\$column is $column\n"; die if $column < 1; __END__ # grep filtered $ ~/Desktop/gol.pl --c 2 --c=2 --c= 2 --c = 2 $column is 1 # grep substitution conditional $ ~/Desktop/gol.pl --c= 2 --c = 2 --c 2 --c=2 $column is 100

The first way greps all the arguments that dont match your condition, and reassigns the newly created list back into @ARGV

The second way uses a substitution to clear the element, and returns true if it occurs, allowing the conditional block to be entered.

ok heres the one that didnt quite work at first, as I almost posted a response that solved the problem, but then also discounted the long option without equal sign.

#!/usr/bin/perl # first attempt, non-working as the # long option is also discounted use strict; use warnings; use Getopt::Long qw( :config posix_default gnu_compat bundling no_auto_abbrev no_ignore_case ); my $column = 1; #print map "[$_]", @ARGV, "\n"; #exclude the unwanted format with a filtering grep @ARGV = grep $_ !~ /\A--c=/, @ARGV; # remove the offending format using substition operator and test to se +e if you did # if( grep s/\A--c=.*//, @ARGV ){ $column = 100 }; #print map "[$_]", @ARGV, "\n"; GetOptions( 'column|c=i' => \$column, ) or die; print "\$column is $column\n"; die if $column < 1; __END__ $ ~/Desktop/gol.pl --column=2 $column is 2 $ ~/Desktop/gol.pl -c2 $column is 2 $ ~/Desktop/gol.pl --c=2 $column is 1 $ ~/Desktop/gol.pl -c=2 Value "=2" invalid for option c (number expected) Unknown option: = Unknown option: 2 Died at ~/Desktop/gol.pl line 19. # using grep and substitution as conditional $ ~/Desktop/gol.pl --c=2 $column is 100 #but also doesnt allow $ ~/Desktop/gol.pl --column 2 $column is 1

I realised the code needed to not discount the long option so had to use a word border match, or forward notahead for more specific filtering.

Hope this helps


perl -M="Don Coyote" -e 'while(<ARGV>){$.== 1 and s!.*!pack(q{C*},043,041,057,0165,0163,0162,057,0142,0151,0156,057,0160,0145,0162,0154)!e }' *

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.