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
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |