Perl Getops

J

Johhny

Hello,

I am in the process of learning perl. On my perl quest I am trying to
learn getops. How do I make it so that if I type the command for
example

../perl_script.pl

without any additional flags it will automatically show the help or a
title saying what it does.

Currently I have something like this

==SNIP==

sub get_options()
{
use Getopt::Std;
my $opt_string = 'hvdf:';
getopts( "$opt_string", \%opt ) or usage();
usage() if $opt{h};
}

==SNIP==

Any assistance would be greatly appreciated.
 
U

usenet

Johhny said:
How do I make it so that if I type the command for example
./perl_script.pl
without any additional flags it will automatically show the help or a
title saying what it does.

Your sample code calls a subroutine "usage()". What is that? It's not
exported by GetOpt:Std. Is this your own subroutine? It should be easy
to do something like:
usage() unless @ARGV; #show usage if no parameters were passed

But you really should be using POD to annotate your script. I prefer
GetOpt::Long, and I have some sample code in my "skeleton" script to
remind me of the proper syntax. Here is the code (which will do what
you are asking):

#!/usr/bin/perl
use strict; use warnings;

use Pod::Usage;
use Getopt::Long qw:)config auto_version);
my %opt;
GetOptions ( \%opt, # always include the first two...
'help|h|?', # Show help (POD) --help or --?
'man', # Show manpage (POD) --man
'test', # Run program in test mode
#here are some usage examples of of other parameter types
'config|ini=s', # Alternate configuration (.ini) file
'string_opt=s', # Required String --config=/tmp/x.ini
'integer_opt:i', # Optional Integer --border[=2]
'def_int_opt:5', # Optional integer but with default value
'option', # Boolean (true) --option
'opt2!', # Boolean (negatable) --opt2 or --no-opt2
'more+' # Incrementable --more --more --more
) || pod2usage( {-verbose => 1} );

pod2usage( {-verbose => 1} ) if $opt{'help'} || ! @ARGV;
pod2usage( {-verbose => 2} ) if $opt{'man'};

__END__

=head1 NAME

sample - Using Getopt::Long and Pod::Usage

=head1 SYNOPSIS

whatever you want

=cut
 
P

Paul Lalli

Johhny wrote:
#!/usr/bin/perl
use strict; use warnings;

use Pod::Usage;
use Getopt::Long qw:)config auto_version);
my %opt;
GetOptions ( \%opt, # always include the first two...
'help|h|?', # Show help (POD) --help or --?
'man', # Show manpage (POD) --man
'test', # Run program in test mode
#here are some usage examples of of other parameter types
'config|ini=s', # Alternate configuration (.ini) file
'string_opt=s', # Required String --config=/tmp/x.ini
'integer_opt:i', # Optional Integer --border[=2]
'def_int_opt:5', # Optional integer but with default value
'option', # Boolean (true) --option
'opt2!', # Boolean (negatable) --opt2 or --no-opt2
'more+' # Incrementable --more --more --more
) || pod2usage( {-verbose => 1} );

pod2usage( {-verbose => 1} ) if $opt{'help'} || ! @ARGV;
pod2usage( {-verbose => 2} ) if $opt{'man'};

Er, did you actually test that? GetOptions() removes arguments from
@ARGV as it processes. If the user ran the program with, for example,
--test and --integer_opt 10, then '--test', '--integer_opt', and '10'
would be removed from @ARGV after the call to GetOptions, leaving @ARGV
empty, and causing the pod2usage statement to execute.

In the case of the OP, you'd have to examine the size of @ARGV *before*
the call to GetOptions in order to determine if the user actually gave
any command line options.

As far as I know, there is no way to have GetOptions require that any
particular option is actually passed on the command line. It will
simply die (or return false?) if an unknown option is passed.

If I am wrong about this, someone please let me know. Is there a way
to make GetOptions fail if a particular option is not included?

Paul Lalli
 
U

usenet

Paul said:
Er, did you actually test that?

Um, yeah, I tested it without any options. It worked (of course).
GetOptions() removes arguments from
@ARGV as it processes.

Yeah, I didn't realize that and didn't test for that case (oops).

OK, then, the code ought to have a statement like this:

pod2usage( {-verbose => 1} ) unless @ARGV;

before the GetOptions statement (and omit the "|| ! @ARGV;" from the
latter statement).
 
G

Glenn Jackman

At 2005-11-17 01:12PM said:
How do I make it so that if I type the command [...]
without any additional flags it will automatically show the help or a
title saying what it does.

sub get_options()
{
use Getopt::Std;
my $opt_string = 'hvdf:';
getopts( "$opt_string", \%opt ) or usage();
usage() if $opt{h};

usage() if $opt{h} or keys(%opt) == 0;


also, you might as well put all your 'use' statements at the top of the
script, because they will all be executed at compile time anyway.
 
J

John W. Krahn

Johhny said:
I am in the process of learning perl. On my perl quest I am trying to
learn getops. How do I make it so that if I type the command for
example

./perl_script.pl

without any additional flags it will automatically show the help or a
title saying what it does.

die "usage: $0 [options] file\n" unless @ARGV;


John
 
A

Ala Qumsieh

Glenn said:
At 2005-11-17 01:12PM said:
How do I make it so that if I type the command [...]
without any additional flags it will automatically show the help or a
title saying what it does.

sub get_options()
{
use Getopt::Std;
my $opt_string = 'hvdf:';
getopts( "$opt_string", \%opt ) or usage();
usage() if $opt{h};


usage() if $opt{h} or keys(%opt) == 0;

usage() if $opt{h} || !%opt;
also, you might as well put all your 'use' statements at the top of the
script, because they will all be executed at compile time anyway.

This rule is too general. Some use() statements are lexically scoped,
and thus can't go at the top of the script.

--Ala
 
A

Anno Siegel

Ala Qumsieh said:
Glenn said:
At 2005-11-17 01:12PM said:
How do I make it so that if I type the command [...]
without any additional flags it will automatically show the help or a
title saying what it does.

sub get_options()
{
use Getopt::Std;
my $opt_string = 'hvdf:';
getopts( "$opt_string", \%opt ) or usage();
usage() if $opt{h};


usage() if $opt{h} or keys(%opt) == 0;

usage() if $opt{h} || !%opt;
also, you might as well put all your 'use' statements at the top of the
script, because they will all be executed at compile time anyway.

This rule is too general. Some use() statements are lexically scoped,
and thus can't go at the top of the script.

Even those use() statements that could got to the top are sometimes
better positioned close to where they are actually needed. "use Getopt"
is a good example: The getopts() function is usually called only once
per script, so I'd put the use() statement close to that call.

I wouldn't put it inside the sub body, however. Directly before the
"sub get_options" line is a good place.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,379
Messages
2,571,945
Members
48,806
Latest member
LizetteRoh

Latest Threads

Top