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

I'd like to present a descrition of the script I wrote along with the command line syntax and argument list when the program is called without] any filenames or options set. i.e.
% perl my_program This program does... myprogram [option][filename] -x does something -y does something else
I'm just learning and I haven't been able to find anything related for reference. Thanks in advance.

Replies are listed 'Best First'.
RE: No arguments or filenames
by DrManhattan (Chaplain) on Aug 07, 2000 at 19:16 UTC

    The command line arguments for your script live in the array @ARGV:

    #!/usr/bin/perl -w if (!@ARGV) { print STDERR << "SYNTAX"; myprogram [option][filename] -x does something -y does something else SYNTAX exit 1; }

    Also, take a look at the Getopt::Std module to handle your switch processing.

    -Matt

Re: No arguments or filenames
by KM (Priest) on Aug 07, 2000 at 19:16 UTC
    You can take a look at Getopt::Long, Getopt::Simple, Getopt::*, etc.. or do something like, or some variant of:

    unless (@ARGV) { print qq{I am helpful.}; }

    Cheers,
    KM

(Guildenstern) Re: No arguments or filenames
by Guildenstern (Deacon) on Aug 07, 2000 at 19:16 UTC
    try:
    usage() unless @ARGV; ... sub usage() { #print out program usage info, flags, etc #then exit() }

    This will print the usage info and exit if there are no command line parameters (which are stored in @ARGV)
Re: No arguments or filenames
by TheOtherGuy (Sexton) on Aug 08, 2000 at 01:00 UTC
    Based on your example, you want to use the GetOpt module. A possible example of what has worked for me in the past, with some default values:
    use strict; (my $cmd = $0) =~ s|^.*/||; # Program name # ---------------------------------------- # Handle Command Line options use Getopt::Std; &getopts ('d:r:H'); my $DATE; # $opt_d my $RCFILE; # $opt_r # -------------------- # Date if ($Getopt::Std::opt_d) { $DATE = $Getopt::Std::opt_d; } else { $DATE = "TODAY"; } # -------------------- # Resource File if ($Getopt::Std::opt_r) { $RCFILE = $Getopt::Std::opt_r; } elsif ( defined($ENV{RCFILE}) ) { $RCFILE = "$ENV{RCFILE}"; } else { warn "ERROR: No RC File Specified\n"; $Getopt::Std::opt_H = 1; } # -------------------- # Help if ($Getopt::Std::opt_H) { warn "$cmd:\n"; warn " (-m) <milestone>\n"; warn " (-r) <rcfile>\n"; warn " (-H) <Help>\n"; exit 1; }
RE: No arguments or filenames
by Anonymous Monk on Aug 07, 2000 at 22:06 UTC
    You can do something like my $usage = qq(Usage text); die $usage if !@ARGV; You could also make usage a sub and call it when @ARGV is empty: You should also check out GetOpt::*