help message with no input options

Discussion in 'Perl Misc' started by Slickuser, Sep 2, 2008.

  1. Slickuser

    Slickuser Guest

    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";
    }
     
    Slickuser, Sep 2, 2008
    #1
    1. Advertising

  2. Slickuser

    smallpond Guest

    On Sep 2, 1:25 pm, Slickuser <> wrote:
    > 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
     
    smallpond, Sep 2, 2008
    #2
    1. Advertising

  3. Slickuser

    Slickuser Guest

    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";
    }

    On Sep 2, 11:58 am, smallpond <> wrote:
    > On Sep 2, 1:25 pm, Slickuser <> wrote:
    >
    > > 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
     
    Slickuser, Sep 2, 2008
    #3
  4. Slickuser

    smallpond Guest

    On Sep 2, 4:23 pm, Slickuser <> wrote:
    > Replace the sub routine and I still get error if I used input
    > arguments options.
    >



    Many community colleges offer inexpensive introductory programming
    classes.

    --S
     
    smallpond, Sep 2, 2008
    #4
  5. Slickuser

    cartercc Guest

    On Sep 2, 1:25 pm, Slickuser <> wrote:
    > 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
     
    cartercc, Sep 2, 2008
    #5
  6. Slickuser

    Slickuser Guest

    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";
    }

    On Sep 2, 2:06 pm, cartercc <> wrote:
    > On Sep 2, 1:25 pm, Slickuser <> wrote:
    >
    > > 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
     
    Slickuser, Sep 2, 2008
    #6
  7. Slickuser wrote:
    > 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.

    >
    > #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";
    > }
     
    Brian Helterlilne, Sep 3, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cliff  Martin
    Replies:
    1
    Views:
    3,055
    Larry Smith
    Jan 31, 2007
  2. jacksu
    Replies:
    0
    Views:
    525
    jacksu
    Oct 9, 2007
  3. Utkado
    Replies:
    2
    Views:
    2,163
  4. Bryan Richardson
    Replies:
    6
    Views:
    302
    Bryan Richardson
    Feb 25, 2008
  5. Sijo Kg
    Replies:
    1
    Views:
    104
    Justin Collins
    Jan 7, 2009
Loading...

Share This Page