
FishMonger
Veteran
/ Moderator
May 9, 2016, 10:02 AM
Post #5 of 9
(52978 views)
|
Re: [FishMonger] Need help with AutoComplete ARGV option
[In reply to]
|
Can't Post
|
|
Here's another example using the dispatch table and the argument handling is closer to what you are wanting.
#!/usr/bin/perl use 5.010; use strict; use warnings; use Getopt::Long; my %dispatch = ( admin => \&admin, backup => \&backup, login => \&login, start => \&start, steam => \&steam, default => \&default, ); my $action = 'default'; my $remote; GetOptions ( 'admin' => sub { $action = shift }, 'backup' => sub { $action = shift }, 'login' => sub { $action = shift }, 'start' => sub { $action = shift }, 'steam' => sub { $action = shift }, 'remote=s' => \$remote, ) or die("Error in command line arguments\n"); $dispatch{$action}->($remote); sub admin { my $host = shift; say "doing 'admin' things on $host"; } sub backup { my $host = shift; say "doing 'backup' things on $host"; } sub login { my $host = shift; say "doing 'login' things on $host"; } sub start { my $host = shift; say "doing 'start' things on $host"; } sub steam { my $host = shift; say "doing 'steam' things on $host"; } sub default { my $host = shift; say "doing 'default' things on $host"; }
c:\test>Perl-1.pl -st Option st is ambiguous (start, steam) Error in command line arguments c:\test>Perl-1.pl -r localhost -b doing 'backup' things on localhost c:\test>Perl-1.pl -a -r localhost doing 'admin' things on localhost
(This post was edited by FishMonger on May 9, 2016, 10:29 AM)
|