Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

ARGV issue

by carbonettis (Initiate)
on Oct 02, 2011 at 21:50 UTC ( #929185=perlquestion: print w/ replies, xml ) Need Help??
carbonettis has asked for the wisdom of the Perl Monks concerning the following question:

Hey Monks, I would like to write a subroutine to look for command-line arguments such as -help, -h, and --help. I wanna call my subrutine from a main program and the thing is: if I give the program any of the named command-line arguments, when you pass them into the subroutine it should return a true value. If this is the case, the program should print out a help message in a $USAGE variable and exit. My try is:
#!/usr/bin/perl -w $check = mycheck($#ARGV); if ($check != 1) { $usage = "Help arguments have been found"; print $usage, "\n"; } sub mycheck { my($#ARGV) = @_; $return = 0; #Variable RETURN set as false (set as 0) foreach $arg (0 .. $#ARGV) { if (($arg eq '-help') || ($arg eq '-h') || ($arg eq '--help')) + { $return = 1; #Variable return now is true exit; } else { print "No help arguments found\n"; } } return $return }
But it looks like not working and I cannot find the reason. Any ideas? when I run it it returns "syntax error at ex4HW01.pl line 13, near "0;" THANKS!

Comment on ARGV issue
Download Code
Re: ARGV issue
by ikegami (Pope) on Oct 02, 2011 at 21:57 UTC

    You have a couple mistakes.

    • my($#ARGV) = @_; makes no sense.

    • $arg contains a number, so it'll never be equal to any of the strings to which you compare it. You would need to loop over @ARGV.

    I'd use Getopt::Long.

    use Getopt::Long qw( GetOptions ); sub usage { print STDERR "...simple usage...\n"; exit(1); } sub help { print "...more extensive help...\n"; exit(0); } GetOptions( 'h|?|help' => \&help, ... ) or usage();

    You might also find Pod::Usage of interest.

Re: ARGV issue
by jwkrahn (Monsignor) on Oct 02, 2011 at 22:24 UTC

    Try it like this:

    #!/usr/bin/perl use warnings; use strict; if ( grep /^(?:--help|-help|-h)$/, @ARGV ) { print "Help arguments have been found\n"; } else { print "No help arguments found\n"; }
Re: ARGV issue
by TomDLux (Vicar) on Oct 03, 2011 at 02:39 UTC

    I use Getopts:;Long to parse command line arguments, and Pod::usage to display the POD documentation which is in the same file.

    use POSIX qw{strftime}; use Getop:;Long; use Pod::Usage; my %options = (date => strftime( %Y-%m-%d); Getopts( \%options, 'date=s', 'help', 'man' ) or pod2usage( { '-message' => 'Error processing command line args', + '-verbose' => 0, '-exitval' => 1, }); pod2usage( { '-verbose' => 1 }) if exists $options{help}; pod2usage( { '-verbose' => 2 }) if exists $options{man}; __END__ =head1 NAME myprog - Use it to demo Getopt::Long and Pod::Usage =head1 SYNOPSIS myprog [-date yyyy-mm-dd] [-help] --man' =head1 ARGUMENTS =over 4 =item -date yyyy-mm-dd The date to process. If not specified, today's date is used. =item -help Display a brief summary of the documentation, but not as brief as shown for a command line argument error. =item -man Display complete man page. =back =head1 DESCRIPTION This is what the program does, and how it works. =head1 AUTHOR me =cut

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Reaped: Re: ARGV issue
by NodeReaper (Curate) on Oct 03, 2011 at 13:22 UTC
Re: ARGV issue
by dwm042 (Priest) on Oct 03, 2011 at 17:56 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2015-07-05 16:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls