Case-insensitive Command Line Args 
Author Message
 Case-insensitive Command Line Args

I adapted a version of grep I found somewhere, and got it working for
the most part, to my liking.  However, I want to be able to enter
command line args such as "GREP -IL"  in upper case, just in case I am
set to upper case.  I tried adding the noted line, but it is ignored.
Can someone suggest what I should change?  I am trying to avoid
putting lc() on each reference to $opt in the program.

#-------------------
# i ignore case
# c count only
# l list name only
# e regex
#-------------------

use Getopt::Std;
my %opt;
getopts('ivcle:',\%opt);
$opt = lc($opt);         #  <----- added this
my $pat = $opt{'e'} || shift;

my $previous_file = "";



Quote:
}

my $mcount = 0;
while (<>) {
    if (($opt{i})? /$pat/io : /$pat/o) {
        $mcount++;
        if ($ARGV ne $previous_file) {
            print "File: $ARGV\n" unless ($opt{'c'} || $opt{'l'} ||
$opt{'v'});
        }
        $previous_file = $ARGV;
        unless ($opt{'c'} || $opt{'l'} || $opt{'v'}) {
            print "$_";
        }
    }
    if (eof) {
        if ($opt{'c'} && $mcount) {
            printf("%5d $ARGV\n",$mcount);
        } else {
            if ($opt{'l'} && $mcount) {
                print "$ARGV\n";
            }
        }
        $mcount = 0;
        $. = 0;
    }
Quote:
}



Mon, 05 Apr 2004 13:57:06 GMT  
 Case-insensitive Command Line Args

Quote:

> I adapted a version of grep I found somewhere, and got it working for
> the most part, to my liking.  However, I want to be able to enter
> command line args such as "GREP -IL"  in upper case, just in case I am
> set to upper case.  I tried adding the noted line, but it is ignored.
> Can someone suggest what I should change?  I am trying to avoid
> putting lc() on each reference to $opt in the program.

> #-------------------
> # i ignore case
> # c count only
> # l list name only
> # e regex
> #-------------------

> use Getopt::Std;
> my %opt;
> getopts('ivcle:',\%opt);
> $opt = lc($opt);         #  <----- added this

This definitely won't work, since %opt and $opt are two completely
different beasts.

Since you have only 5 options, why not write
if (exists($opt{'I'}) $opt{'i'} = $opt{'I'};    # similar for C, L, E, V

or

foreach (keys %opt) {
   next if /[a-z]/;
   $opt{lc($_)} = $opt{$_};
   # delete $opt{$_};

Quote:
}

HTH,
--
Josef M?llers (Pinguinpfleger bei FSC)
        If failure had no penalty success would not be a prize
                                                -- T.  Pratchett


Mon, 05 Apr 2004 15:15:22 GMT  
 Case-insensitive Command Line Args

Quote:

>I want to be able to enter
>command line args such as "GREP -IL"  in upper case
>I tried adding the noted line, but it is ignored.

Your added line is the only line that operates on the
$opt scalar variable. $opt is not related to %opt in
any way, they are in separate namespaces (glob slots).

Quote:
>Can someone suggest what I should change?  I am trying to avoid
>putting lc() on each reference to $opt in the program.


could get tricky.

Quote:
>use Getopt::Std;
>my %opt;
>getopts('ivcle:',\%opt);
>$opt = lc($opt);         #  <----- added this

You should be running with "use strict;" enabled. It would have
immediately pointed out why that line has no effect...

I'd allow either case in the getopts() call, and then patch
up the %opts hash:

----------------------------
#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Std;

my %opt;
getopts('ivcle:IVCLE:',\%opt);
adjust_case(\%opt);

print "$_ ==> $opt{$_}\n" for sort keys %opt;

sub adjust_case {



      if ( $key =~ /^[A-Z]$/ ) {
          $href->{lc $key} = $href->{$key};
          delete $href->{$key}
      }
   }

Quote:
}

----------------------------

Fails horribly if called with switches in _both_ cases though...

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Mon, 05 Apr 2004 16:33:25 GMT  
 Case-insensitive Command Line Args

Quote:

>However, I want to be able to enter
>command line args such as "GREP -IL"  in upper case, just in case I am
>set to upper case.  I tried adding the noted line, but it is ignored.
>use Getopt::Std;
>my %opt;
>getopts('ivcle:',\%opt);
>$opt = lc($opt);         #  <----- added this

 getopts('ivcle:IVCLE:',\%opt);         # List all valid options here
 $opt{i} = $opt{I} if exists $opt{I};
 $opt{v} = $opt{V} if exists $opt{V};
 $opt{c} = $opt{C} if exists $opt{C};
 $opt{l} = $opt{L} if exists $opt{L};
 $opt{e} = $opt{E} if exists $opt{E};

        -Joe
--
See http://www.inwap.com/ for PDP-10 and "ReBoot" pages.



Tue, 06 Apr 2004 09:49:14 GMT  
 Case-insensitive Command Line Args
On Fri, 19 Oct 2001 08:49:14 +0000 (UTC),

Quote:

>  getopts('ivcle:IVCLE:',\%opt);            # List all valid options here
>  $opt{i} = $opt{I} if exists $opt{I};
>  $opt{v} = $opt{V} if exists $opt{V};
>  $opt{c} = $opt{C} if exists $opt{C};
>  $opt{l} = $opt{L} if exists $opt{L};
>  $opt{e} = $opt{E} if exists $opt{E};

map { $opt{"\L$_"} = $opt{$_} if exists $opt{$_} } qw/I V C L E/;

Yep, this is one of them cases where you can't use a for modifier, and I
just don't want to waste four lines where one will do just as fine.

Martien
--
                                |
Martien Verbruggen              | The world is complex; sendmail.cf
                                | reflects this.
                                |



Tue, 06 Apr 2004 15:13:13 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Hiding command-line args?

2. command-line args style flags from a file

3. Debug .pl Syntax of command line args ?

4. command line args not working correctly

5. HELP: How to program command line args

6. perl command line args

7. Passing Args to script via web and command line

8. perl command line args

9. Command line args..?

10. Command line args

11. Question: Perl for Windows command line args?

12. Getting command line args

 

 
Powered by phpBB® Forum Software