Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

how to deal with incorrect command line argument

by scripter87 (Novice)
on Oct 30, 2013 at 21:35 UTC ( #1060452=perlquestion: print w/replies, xml ) Need Help??

scripter87 has asked for the wisdom of the Perl Monks concerning the following question:

Hey monks, Got an issue that I would like help with if possible. I got this program that accepts arguments from the cl so say I typed perl mytest.pl test.html testb.html the program would do something with the arguments. The problem I have is that if I typed the arguments incorrectly say perl mytest.pl twst.html testb.html (they should both be as above) I want the program to report an error "warn" but continue and work with the correct argument. This is a snippet that I cant correctly use
foreach $_(@ARGV) { my $rename = $_; if (!-e $rename) { warn "$rename does not exist"; # previously I had this as die but +obviously that killed the program and did not continue with the corre +ct argument for(my $i=0;$i<$rename;$i++){ # I am not really sure if this loop +is the right idea #or what to put in it.. }

Replies are listed 'Best First'.
Re: how to deal with incorrect command line argument
by Kenosis (Priest) on Oct 30, 2013 at 21:58 UTC

    Perhaps the following will be helpful:

    use strict; use warnings; my @array; for (@ARGV) { if (-e) { push @array, $_; next; } warn qq{"$_" doesn't exist!}; } @ARGV = @array; print "@ARGV";

    This pushes the 'good' params onto @array and warns on the others, then reinitializes @ARGV with the 'good' ones.

      Slightly simplified, I think:

      use strict; use warnings; for (@ARGV) { if (-e) { &doTheWork($_); } else { warn qq{"$_" doesn't exist!}; } }

        Good suggestion! Or even just:

        use strict; use warnings; (-e) ? doTheWork($_) : warn qq{"$_" doesn't exist!} for @ARGV;
      Thanks my man, does the trick. May I ask a few questions on your solution though... I am going to comment it on your code and maybe you can correct me where I am wrong.
      my @array; # this an array to store the good arguments for (@ARGV) { # for the argumets array if (-e) { # if the file exists push @array, $_;# push the file that exists onto the @array # why use $_ here? next; # what purpose has this } warn qq{"$_" doesn't exist!}; } @ARGV = @array;
      I Appreciate the time you took to answer my question to man.

        You're most welcome! Am glad it helped.

        You're commenting is correct. You asked, "why use $_ here?" Because Perls default scalar ($_) is implicitly used in the for loop that's iterating through the elements of @ARGV. Note, also, that Perl's default scalar is also implicitly used in the expression if (-e) { as that expression is equivalent to if (-e $_) {.

        The purpose of next is to get the next element of @ARGV, otherwise a warn would occur.

Re: how to deal with incorrect command line argument
by AnomalousMonk (Bishop) on Oct 31, 2013 at 08:10 UTC
    ... I want the program to report an error "warn" but continue and work with the correct argument.

    In general, I find I have enough trouble when I try to guess what my programs should be doing; I certainly don't want my programs making those guesses!

    However, in the spirit of giving good advice and then immediately undercutting it, you might look at Text::Levenshtein and related fuzzy string comparison modules; see Levenshtein distance. The strategy might be something like "look for all file names in a given directory with an L-D (or other match metric) less than a given threshold, then use the file name with the least distance if that name is unique".

    >perl -wMstrict -le "use Text::LevenshteinXS; ;; my $tyop = 'twst.html'; for my $try (qw(test.html testb.html)) { my $d = distance($tyop, $try); print qq{'$tyop' < $d > '$try'}; } " 'twst.html' < 1 > 'test.html' 'twst.html' < 2 > 'testb.html'
      <speechless> </speechless>
      Um. Wow. I have some self-educational opportunities here, it seems.

        Make sure to have paramedics standing by before you start juggling your swiss army chainsaws like this :).

      The day that commands in the rm family start exposing that kind of behaviour, where not only they offer suggestions a la Google ("twst.htm doesn't exist - did you mean to rm test.htm instead?") but also assume that that's what I meant ("twst.htm doesn't exist, so I went out of my way for you and rm'ed test.html instead, which, hey, you only worked on for 8 hours straight and haven't backed up yet. You're welcome, bro.") I swear I'm never touching a computer again.

        ROFLSHIPMP OMG My wife is going to disown me I am laughing so hard.

        Nice way to end my PerlMonks day. Thank you!

Re: how to deal with incorrect command line argument
by Anonymous Monk on Oct 30, 2013 at 23:08 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1060452]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2020-10-19 22:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (207 votes). Check out past polls.

    Notices?