Here is how to parse command-line options of types:
--opt=value
--no-opt
--opt
in a really quick way without any modules like Getopt
Despite of its small code size, it supports default values,
complaints about undefined options, YES/NO-style options.
my %opts = (
# %known_opts enumerates allowed opts as well as specifies default
# and initial values
my %known_opts = (
'debug' => 0,
'verbose' => 1,
'log' => 'log.txt', # default value
'file' => '', # actually like declaration of option
),
#options itself
my %specified_opts = (
(map {/^--([\-_\w]+)=(.*)$/} @ARGV), # --opt=smth
(map {/^no-?(.*)$/i?($1=>0):($_=>1)} map {/^--([\-_\w]+)$/} @ARGV)
+, # --opt --no-opt --noopt
),
);
die "option '$_' is not recognized" for grep {!exists $known_opts{$_}}
+ keys %specified_opts;
@ARGV = grep {!/^--/} @ARGV;
# then normal program goes here