CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Need help with AutoComplete ARGV option

 



zohman
Novice

May 8, 2016, 9:31 PM

Post #1 of 9 (48614 views)
Need help with AutoComplete ARGV option Can't Post

Hi,

i have a mandatory $action field on some script, it's look like this:


Code
	my %actions = map { $_ => 1 } ('admin','backup','login','start','steam'); 

my ($action,$remote) = @ARGV;
$action = AutoComplete($action);

# some other code

if ($actions{$action}) {
# do something
}

sub AutoComplete {
my $action = shift;
$action = 'admin' if ($action =~ /^a$|^ad$|^adm$|^admi$/);
$action = 'backup' if ($action =~ /^b$|^ba$|^bac$|^back$|^backu$/);
$action = 'login' if ($action =~ /^l$|^lo$|^log$|^logi$/);
$action = 'start' if ($action =~ /^sta$|^star$/);
$action = 'steam' if ($action =~ /^ste$|^stea$/);
return $action;
}


So my action field is admin or backup or login or start or steam
i created the AutoComplete to allow using shortest name of the action name,
there is also syntax check for errors not for your concern, all working good.

i just want to make it generic,
right now AutoComplete is hard-coded and if i will add an action in the future for %actions
it will need the right regex in my AutoComplete.

if you going to direct me to 'use Getopt' please let me know how to do it because
i don't want to use switches (-/--)

Thanks,
Zohman.


Laurent_R
Veteran / Moderator

May 8, 2016, 11:35 PM

Post #2 of 9 (48605 views)
Re: [zohman] Need help with AutoComplete ARGV option [In reply to] Can't Post

It coujld be made generic for the three first actions, but the autocomplete rules for the next two ones (start and steam) are not built the same way (for obvious reasons). Difficult to lake something generic in this context.


FishMonger
Veteran / Moderator

May 9, 2016, 8:17 AM

Post #3 of 9 (48594 views)
Re: [zohman] Need help with AutoComplete ARGV option [In reply to] Can't Post


Quote
if you going to direct me to 'use Getopt' please let me know how to do it because
i don't want to use switches (-/--)


Why not use switchs? It (Getopt::Lomg) would solve your problem without having to create you own custom "AutoComplete" sub.

Is this a homework assignment where your instructor has ruled out the use of switches?


Code
#!/usr/bin/perl 

use strict;
use warnings;
use Getopt::Long;
use Data::Dumper;

my $file;
my $length;
my $verbose;

GetOptions (
"length=i" => \$length, # numeric
"file=s" => \$file, # string
"verbose" => \$verbose) # flag
or die("Error in command line arguments\n");

print Dumper($file, $length, $verbose);



Quote
c:\test>Perl-1.pl
$VAR1 = undef;
$VAR2 = undef;
$VAR3 = undef;

c:\test>Perl-1.pl -fi test.txt
$VAR1 = 'test.txt';
$VAR2 = undef;
$VAR3 = undef;

c:\test>Perl-1.pl -len 25
$VAR1 = undef;
$VAR2 = 25;
$VAR3 = undef;

c:\test>Perl-1.pl -ver
$VAR1 = undef;
$VAR2 = undef;
$VAR3 = 1;

c:\test>Perl-1.pl -v -f test.txt -l 10
$VAR1 = 'test.txt';
$VAR2 = 10;
$VAR3 = 1;



(This post was edited by FishMonger on May 9, 2016, 8:22 AM)


FishMonger
Veteran / Moderator

May 9, 2016, 8:28 AM

Post #4 of 9 (48590 views)
Re: [zohman] Need help with AutoComplete ARGV option [In reply to] Can't Post

In addition to using the Getopt::Long module, I'd create a dispatch table of all of the "actions". This makes it an easy to do a hash lookup and execution of the appropriate action sub.


(This post was edited by FishMonger on May 9, 2016, 8:29 AM)


FishMonger
Veteran / Moderator

May 9, 2016, 10:02 AM

Post #5 of 9 (48584 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.


Code
#!/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";
}



Quote
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)


zohman
Novice

May 10, 2016, 5:13 PM

Post #6 of 9 (48554 views)
Re: [FishMonger] Need help with AutoComplete ARGV option [In reply to] Can't Post

Okey, Thanks, i'll check if it can help me in the future,
i don't like to use switches it's make it to long..
so i may keep the AutoComplete sub and "hard-coded" every option,

i believe that there is someway to make it generic
but it should be very powerful regex..

c:\test>Perl-1.pl -r localhost -b
doing 'backup' things on localhost
VS
c:\test>Perl-1.pl localhost b
doing 'backup' things on localhost

Regards,
Zohman.


FishMonger
Veteran / Moderator

May 10, 2016, 8:30 PM

Post #7 of 9 (48548 views)
Re: [zohman] Need help with AutoComplete ARGV option [In reply to] Can't Post


Quote
i don't like to use switches it's make it to long.

Are you kidding me? I assure you, your plan will not only be more verbose but will also be less readable and less maintainable.

The module will handle the argument processing much cleaner and shorter than you can do and will handle edge cases you haven't thought about.

Good luck with your reinventing of the wheel.


BillKSmith
Veteran

Jun 19, 2016, 7:59 PM

Post #8 of 9 (47859 views)
Re: [zohman] Need help with AutoComplete ARGV option [In reply to] Can't Post

I realize that this thread is over six weeks old. I hope that it is old enough that I am no longer doing someone's homework. Clearly the use of modules is a safer and more maintainable solution, but the following meets the original specifications.

If the argument matches one (and only one) of the possible actions, take that action. Otherwise report that the argument as invalid.


Code
use strict; 
use warnings;
my %actions = map { $_ => 1 } ('admin','backup','login','start','steam');
my ($action,$remote) = @ARGV;
$action = AutoComplete($action, keys %actions);
printf '%-6s %d', $action, $remote;


sub AutoComplete {
my ($action, @actions) = @_;
die "Suspucious argument\n" if $action =~/[^\w]/;
my @matches = grep {/^$action/i} @actions;
return $matches[0] if @matches == 1;
die "Invalid option $action\n";
}


Warning: There is a known security issue with running regexes from an external source. You probably should validate $action before using it in the grep regex.
Good Luck,
Bill


zohman
Novice

Jun 19, 2016, 10:15 PM

Post #9 of 9 (47853 views)
Re: [BillKSmith] Need help with AutoComplete ARGV option [In reply to] Can't Post

This is what I've been looking for,
Thanks!!

Zohman.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives