in reply to Enforce single hyphen for single character options in Getopt::Long

use Getopt::Long qw( :config posix_default gnu_compat bundling no_auto_abbrev no_ignore_case ); my $column = 1; GetOptions( 'column|c=i' => \$column, ) or die; ...

I want this program to accept exactly two strings as options, --column and -c, but it also accepts --c. I want the prefix for short options to be - and only -, similar to how -- is treated for long options when bundling is enabled

posix_default sets POSIXLY_CORRECT and then from Configuring Getopt::Long ...
prefix_pattern
A Perl pattern that identifies the strings that introduce options. Default is --|-|\+ unless environment variable POSIXLY_CORRECT has been set, in which case it is --|-.

Replies are listed 'Best First'.
Re^2: Enforce single hyphen for single character options in Getopt::Long
by parv (Parson) on Feb 25, 2024 at 10:10 UTC

    Post-detour, from some combinations of G::L options of ...

    (nothing provided) prefix=- long_prefix_pattern=-- posix_default getopt_compat no_posix_default no_getopt_compat

    ... only any one of the following combinations produces the desired outcome of failure on --o option ...

    prefix=- long_prefix_pattern=-- prefix=-, long_prefix_pattern=--

    After running the program below, above was obtained thus ...

    perl5.36 getopt-test-combination.pl > out 2> err grep '^ok.+input:.+--o' out

    ... there could be bugs or something being overlooked.

    Update: Oh there is a deficit! Above listed options fails with only --o as the option, but set a value with options of -o --o ...

    grep --color=always '^not ok.+input: -o --o.+parsed: [0-9]+.+(long_)?p +refix' out | head -n 3 not ok 6 - input: -o --o; parsed: 1; conf: ( prefix=- ) not ok 9 - input: -o --o; parsed: 1; conf: ( long_prefix_pattern=-- ) not ok 30 - input: -o --o; parsed: 1; conf: ( prefix=-, long_prefix_pa +ttern=-- ) ...

    ... same as noted in OP. Nothing has changed. Sh?ucks (╯°□°)╯︵ ┻━┻

      sub parse_option( @conf ) { ... my $option; ... try { # Use incremented value type to count number of times options ma +tched. GetOptions( 'option|o+' => \$option ) or die $!; $show_progress and warn qq[option set: $option]; } catch { warn qq[Option parsing failed of @arg: $_]; ## CHANGED. # Need to set so as death of "GetOptions" does not. $option = undef; } ; return $option; }

      With above change, the 3 sets of options now also behave the same for both inputs of --o & -o --o.