package ReportGen::Opts; use strict; # various command line options routines require Exporter; use ReportGen::Globs; @ReportGen::Opts::ISA = qw ( Exporter ); @ReportGen::Opts::EXPORT = qw ( &ParseOpts &Opts2Str ); # # parses command line options into a hash table # returns a list of anonymous options, undef on error # sub ParseOpts { my ($opts, $anonym, @args) = @_; # process configuration file first my $cfg = GetCfg(); foreach my $optName (keys %{$cfg}) { next unless exists $opts->{$optName}; $opts->{$optName} = $cfg->{$optName}->{body}; } @{$anonym} = () if $anonym; while (@args) { for (shift @args) { if (/^-/) { my $opt = $_; $opt =~ s/^-+//; if (!exists $opts->{$opt}) { warn("$0: unknown option `$_'\n"); return undef(); } $opts->{$opt} = shift @args; } else { push @{$anonym}, $_ if $anonym; last; } } } push @{$anonym}, @args if $anonym; return 1; } # converts options hash into a usage-like string sub Opts2Str { my $opts = shift; my $res = %{$opts} ? "options and their defaults:\n" : ''; foreach my $name (sort keys %{$opts}) { $res .= sprintf(" --%-30s %s\n", $name, $opts->{$name} || '[no default]'); } $res .= %{$opts} ? "note: defaults are overwritten by configuration file\n" : ''; return $res; }