Move usage generation to a separate function

This commit is contained in:
Richard Hansen 2024-06-24 02:14:18 -04:00
parent bd688e9750
commit b4c4b5dc54

View file

@ -1248,12 +1248,12 @@ my @opt = (
); );
sub main { sub main {
my $opt_usage = process_args(@opt); process_args(@opt);
$saved_recap = ''; $saved_recap = '';
%saved_opt = %opt; %saved_opt = %opt;
$result = 'OK'; $result = 'OK';
if (opt('help')) { if (opt('help')) {
printf "%s\n", $opt_usage; print(usage(@opt), "\n");
$opt{'version'}('', ''); $opt{'version'}('', '');
} }
@ -2093,18 +2093,12 @@ sub init_config {
} }
} }
###################################################################### sub usage {
## process_args -
######################################################################
sub process_args {
my @spec = ();
my $usage = ""; my $usage = "";
for (@_) { for (@_) {
if (ref $_) { if (ref $_) {
my ($key, $specifier, $arg_usage) = @$_; my ($key, $specifier, $arg_usage) = @$_;
my $value = default($key); my $value = default($key);
push @spec, $key . $specifier;
$opt{$key} //= undef;
next unless $arg_usage; next unless $arg_usage;
$usage .= " $arg_usage"; $usage .= " $arg_usage";
if (defined($value) && $value ne '') { if (defined($value) && $value ne '') {
@ -2123,10 +2117,23 @@ sub process_args {
} }
$usage .= "\n"; $usage .= "\n";
} }
return $usage;
}
######################################################################
## process_args -
######################################################################
sub process_args {
my @spec = ();
for (@_) {
next if !ref($_);
my ($key, $specifier) = @$_;
push @spec, $key . $specifier;
$opt{$key} //= undef;
}
if (!GetOptions(\%opt, @spec)) { if (!GetOptions(\%opt, @spec)) {
$opt{"help"} = 1; $opt{"help"} = 1;
} }
return $usage;
} }
###################################################################### ######################################################################