help message with no input options

S

Slickuser

I am trying to print out the help message if no user input option is
used:

test2.pl runme #Print help usage

test2.pl runme -app "C:/a.exe" #Run script

I have try using scalar @ARGV == 0 but it doesn't help either.
Any idea? Thanks.


use strict;
use warnings;
use Getopt::Long;

main();
exit(0);

sub main
{
my $subcmds =
{
"runme" =>
{
'sub' => \&subcmd_runme
}
};
my $cmd = shift @ARGV;
defined $cmd or $cmd = '';
$subcmds->{$cmd} or die " No such command!\n";
$subcmds->{$cmd}{'sub'}->(@ARGV);
}

sub subcmd_runme
{
GetOptions
(
'app|a=s' => \my $opt_app,
'file|f:s' => \my $opt_file,
'help|h' => \my $opt_help
);

#print help if no user input options?
print "Execute script \n";
}
 
S

smallpond

I am trying to print out the help message if no user input option is
used:


perl -e 'warn "No args" unless defined($ARGV[0])'
No args at -e line 1.

--S
 
S

Slickuser

Replace the sub routine and I still get error if I used input
arguments options.
test2.pl runme
No args at line 33
Execute script
test2.pl runme -app "C:/a.exe"
No args at line 33
Execute script


sub subcmd_runme
{
GetOptions
(
'app|a=s' => \my $opt_app,
'file|f:s' => \my $opt_file,
'help|h' => \my $opt_help
);

#print help if no user input options?
warn "No args" unless defined($ARGV[0]); ### line 33

print "Execute script \n";
}

I am trying to print out the help message if no user input option is
used:

perl -e 'warn "No args" unless defined($ARGV[0])'
No args at -e line 1.

--S
 
S

smallpond

Replace the sub routine and I still get error if I used input
arguments options.


Many community colleges offer inexpensive introductory programming
classes.

--S
 
C

cartercc

I am trying to print out the help message if no user input option is
used:

Check the command line arguments by looking at @ARGV. If the length of
@ARGV doesn't match what you want, print a usage message, like this:

$length = @ARGV;
if ($length != $whatever_you_expect) {
print "usage: whatever you expect\n";
}
else {
call_main();
}
exit;

CC
CC
 
S

Slickuser

test2.pl runme
Please enter subcommand user input options.

It shouldn't give me this message when I have options, but it does.

test2.pl runme -app a
Please enter subcommand user input options.

@ARGV is always zero.

sub subcmd_runme
{
GetOptions
(
'app|a=s' => \my $opt_app,
'file|f:s' => \my $opt_file,
'help|h' => \my $opt_help
);

#print help if no user input options?
my $length = @ARGV;
#my $length2 = scalar(@ARGV);
#print "$length $length2 \n";
if ($length == 0) {
die "Please enter subcommand user input options.\n";
}

print "Execute script \n";
}
 
B

Brian Helterlilne

Slickuser said:
test2.pl runme
Please enter subcommand user input options.

It shouldn't give me this message when I have options, but it does.

test2.pl runme -app a
Please enter subcommand user input options.

@ARGV is always zero.

sub subcmd_runme
{
GetOptions
(
'app|a=s' => \my $opt_app,
'file|f:s' => \my $opt_file,
'help|h' => \my $opt_help
);

GetOptions() alters @ARGV by removing all your options. Anything left
in @ARGV would be unrecognized. Try testing @ARGV first.
 

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

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top