Need help implementing command line switch 
Author Message
 Need help implementing command line switch

I'm trying to implement an optional command line switch for my perl script 'foo':

        $ foo -s

What I've got (which, obviously, isn't right):


           ( $ARGV[$_] eq "-s" ) ? $silent = 1 : $silent = 0;
        }

The specific complaint I get is:

        Argument "-s" isn't numeric for array element at foo line 21.

Any help would be greatly appreciated.

TIA.

Maury



Wed, 06 Jan 1999 03:00:00 GMT  
 Need help implementing command line switch

[posted && cc'd]

+ I'm trying to implement an optional command line switch for my perl
script 'foo':
+
+         $ foo -s
+
+ What I've got (which, obviously, isn't right):
+

+            ( $ARGV[$_] eq "-s" ) ? $silent = 1 : $silent = 0;
+         }

Yep. Try "getopt" -- I think this is included in the Standard distribution,
but there are other versions available at CPAN <url:http://www.perl.com/CPAN>.

James

NAME
    getopt - Process single-character switches with switch
    clustering

    getopts - Process single-character switches with switch
    clustering

SYNOPSIS
        use Getopt::Std;
        getopt('oDI');  # -o, -D & -I take arg.  Sets opt_* as a side effect.
        getopts('oif:');  # -o & -i are boolean flags, -f takes an argument
                          # Sets opt_* as a side effect.

DESCRIPTION
    The getopt() functions processes single-character switches with
    switch clustering. Pass one argument which is a string
    containing all switches that take an argument. For each switch
    found, sets $opt_x (where x is the switch name) to the value of
    the argument, or 1 if no argument. Switches which take an
    argument don't care whether there is a space between the switch
    and the argument.

--
#!/bin/perl -s-- -export-a-crypto-system-sig -RSA-3-lines-PERL
$m=unpack(H.$w,$m."\0"x$w),$_=`echo "16do$w 2+4Oi0$d*-^1[d2%Sa
2/d0<X+d*La1=z\U$n%0]SX$k"[$m*]\EszlXx++p|dc`,s/^.|\W//g,print
pack('H*',$_)while read(STDIN,$m,($w=2*$d-1+length($n)&~1)/2)



Wed, 06 Jan 1999 03:00:00 GMT  
 Need help implementing command line switch


Quote:


> [posted && cc'd]

> + I'm trying to implement an optional command line switch for my perl
> script 'foo':
> +
> +         $ foo -s
> +
> + What I've got (which, obviously, isn't right):
> +

> +            ( $ARGV[$_] eq "-s" ) ? $silent = 1 : $silent = 0;
> +         }

> Yep. Try "getopt" -- I think this is included in the Standard distribution,
> but there are other versions available at CPAN <url:http://www.perl.com/CPAN>.

[ copy of 'man' text deleted ]

James,

Thanks for your help.  Obviously one of those should work, but :-)

I now have:

        require "getopt.pl";  # needed to deal with '-s' switch
        &Getopt('s');               # if '-s' switch found set $opt_s = 1 else !$opt_s
        $silent = $opt_s;
        ($silent) ? warn "Silent switch set\n" : warn "NO silent switch\n";  # debug only

What I get is:

Identifier "main::opt_s" used only once: possible typo at foo line 24.

Plus, of course, my own warn message:  NO silent switch

BTW, there's no question that we're finding getopt.pl and &Getopt() just fine.  We are.

Also, &Getopt() and &Getopts() behave exactly the same way.  I've tried 'em both.

Hey, I'll figure it out.  Somehow I always do.  But I wanted to thank you for pointing these routines out to me.

Maury



Thu, 07 Jan 1999 03:00:00 GMT  
 Need help implementing command line switch


Quote:
> I'm trying to implement an optional command line switch for my perl
script 'foo':

>    $ foo -s

Use the "getopts.pl" library, and do something like:

        &getopts('s');
        if ($opt_s) {
           .....
        }
--

====================================
George Everitt
Interleaf Professional Service Group
Southern Region
"Time flies like an arrow"
====================================



Mon, 11 Jan 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Help: Copy Files in Perl or -i Command Line Switch

2. how to implement files with wildcard in command line

3. Command Line Switches in Windows Perl??

4. -i.bak command line switch (suggestion of new features)

5. Ordering of command line switches

6. VMS PERL command line switches....

7. Adding command line switches

8. command line switches

9. Command line switch (-s)

10. perl - NT and blat command line switches

11. Command Line Switch and Win NT

12. Command line switch: -

 

 
Powered by phpBB® Forum Software