in reply to Re: generic getopt to hash
in thread generic getopt to hash

You code doesn't allow for parameters that aren't options. Let's introduce --. And let's handle the case with no previous --option. Then you can do

command --option ... -- file1 file2
sub parse_args { my $last_opt = ''; my %opts; while (@ARGV) { my $arg = shift(@ARGV); if ($arg eq '--') { last; } elsif ($arg =~ s/^--?//) { $opts{$last_opt = $arg} = []; } else { push @{ $opts{$last_opt} }, $arg; } } unshift @ARGV, @{ delete $opts{''} } if $opts{''}; return \%opts; }

Supporting val=0 as an option makes no sense. It would prevent you from having values with equal signs in them.

If -list a b c wasn't supported, we could do away with requiring --.