Skip to content

Instantly share code, notes, and snippets.

@PaulGWebster
Created September 14, 2021 18:12
Show Gist options
  • Select an option

  • Save PaulGWebster/841bbacc7c9822740a0c7efb25fcc0ec to your computer and use it in GitHub Desktop.

Select an option

Save PaulGWebster/841bbacc7c9822740a0c7efb25fcc0ec to your computer and use it in GitHub Desktop.
random perl
sub process_command_line_options(@args) {
my $getopt = $global->{'obj'}->{'gol'};
my $captured_options = {};
$getopt->getoptionsfromarray(
\@args,
\%{$captured_options},
'help+',
'action:s{1,1}',
'path:s{1,1}',
'args:s@'
);
# We may want to exit from here instantly, so let's make a function to help
my $exit = sub($return_data) {
$return_data->{valid} //= 0;
$return_data
};
# If help is specified or nothing was show help and exit
my $captured_option_count = scalar keys %{$captured_options};
if ($captured_options->{help} || $captured_option_count == 0) {
command_help('commands');
}
elsif (my $action = lc($captured_options->{action})) {
if ($action !~ m/^add|del|enable|disable$/i) {
return $exit->($captured_options);
}
}
# finally return to the caller
$exit->($captured_options)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment