Help using Getopt::Mixed

Discussion in 'Perl Misc' started by Stan Brown, Nov 2, 2003.

  1. Stan Brown

    Stan Brown Guest

    I'm trying to add a usage message to a script that uses Getopt::Mixed. I
    want to print this message whenever a bad commnad line option is passed to
    the script. I' using the module like this:

    use Getopt::Mixed "nextOption" ;


    Getopt::Mixed::init('F i e f=s d:i debug:i configfile=s');
    my $badOption = \&print_usage;
    while (($option, $value, $pretty) = nextOption()) {
    if(( $option eq 'f') || ( $option eq 'configfile' ))

    I just added the "print_usage" line, based upon my reading of the perldoc
    page on this module, but it's not calling the print_usage() function
    (which is defiend earlier in the script, when I pass it an undefined
    argument (-Z for instance).

    Instead, I get the following generic error mesage:

    webcam.pl: unrecognized option `-Z'

    What am I doing wrong here?

    --
    "They that would give up essential liberty for temporary safety deserve
    neither liberty nor safety."
    -- Benjamin Franklin
     
    Stan Brown, Nov 2, 2003
    #1
    1. Advertising

  2. On Sun, 2 Nov 2003 14:07:53 +0000 (UTC)
    Stan Brown <> wrote:
    > I'm trying to add a usage message to a script that uses
    > Getopt::Mixed. I want to print this message whenever a bad commnad
    > line option is passed to the script. I' using the module like this:
    >
    > use Getopt::Mixed "nextOption" ;
    >
    >
    > Getopt::Mixed::init('F i e f=s d:i debug:i configfile=s');
    > my $badOption = \&print_usage;
    > while (($option, $value, $pretty) = nextOption()) {
    > if(( $option eq 'f') || ( $option eq 'configfile' ))
    >
    > I just added the "print_usage" line, based upon my reading of the
    > perldoc page on this module, but it's not calling the print_usage()
    > function(which is defiend earlier in the script, when I pass it an
    > undefined argument (-Z for instance).
    >
    > Instead, I get the following generic error mesage:
    >
    > webcam.pl: unrecognized option `-Z'


    Try:

    --worked for me, but my not for you--
    #!/usr/bin/perl -w

    use strict;
    use warnings;
    use diagnostics;

    sub print_usage{
    print "Usage .....\n";
    print "Bad option was: $_[1]\n";
    #you _need_ the 'exit' here, or you'll get an infinite loop :)
    exit;
    }

    use Getopt::Mixed qw(nextOption) ;

    Getopt::Mixed::init('F i e f=s d:i debug:i configfile=s');
    $Getopt::Mixed::badOption = \&print_usage;
    while (my($option, $value) = nextOption()) {
    if(( $option eq 'f') || ( $option eq 'configfile' )){
    print "if stuff here ...\n";
    }
    }
    Getopt::Mixed::cleanup();
    ----end---

    I'm thinking the author left out the part about needing the
    '$Getopt::Mixed' from the begining of 'badOption'. 'badOption' is a
    reference to a subroutine that's used in the module. In order to set
    this reference, you need to define where the reference points to
    (which you tried) _ and_ what it's namespace is (which you didn't do,
    but the author did not make clear - the 'Getopt::Mixed' portion).

    Plsu, I don't think you need the $pretty" in the exampl you gave. I'm
    not even sure _where_ you got that from. There's no refereence to it
    in the documentation that I found. But, to be fair, I'd only read
    what I needed to read :)

    I know what I'm trying to say, but not sure if I'm saying it
    correctly. Bottom line - it worked for me and I'm thinking it'll work
    for you :)

    HTH

    --
    Jim

    Copyright notice: all code written by the author in this post is
    released under the GPL. http://www.gnu.org/licenses/gpl.txt
    for more information.

    a fortune quote ...
    Mark's Dental-Chair Discovery: Dentists are incapable of asking
    questions that require a simple yes or no answer.
     
    James Willmore, Nov 2, 2003
    #2
    1. Advertising

  3. Stan Brown

    Stan Brown Guest

    In <> James Willmore <> writes:

    >On Sun, 2 Nov 2003 14:07:53 +0000 (UTC)
    >Stan Brown <> wrote:
    >> I'm trying to add a usage message to a script that uses
    >> Getopt::Mixed. I want to print this message whenever a bad commnad
    >> line option is passed to the script. I' using the module like this:
    >>
    >> use Getopt::Mixed "nextOption" ;
    >>
    >>
    >> Getopt::Mixed::init('F i e f=s d:i debug:i configfile=s');
    >> my $badOption = \&print_usage;
    >> while (($option, $value, $pretty) = nextOption()) {
    >> if(( $option eq 'f') || ( $option eq 'configfile' ))
    >>
    >> I just added the "print_usage" line, based upon my reading of the
    >> perldoc page on this module, but it's not calling the print_usage()
    >> function(which is defiend earlier in the script, when I pass it an
    >> undefined argument (-Z for instance).
    >>
    >> Instead, I get the following generic error mesage:
    >>
    >> webcam.pl: unrecognized option `-Z'


    Thanks, that was exactly what I had wrong. Now it works.

    Thanks, again.
    --
    "They that would give up essential liberty for temporary safety deserve
    neither liberty nor safety."
    -- Benjamin Franklin
     
    Stan Brown, Nov 2, 2003
    #3
    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. Praveen

    using getopt() more than once

    Praveen, Dec 19, 2004, in forum: C Programming
    Replies:
    2
    Views:
    448
    Giovanni
    Dec 19, 2004
  2. Sunil
    Replies:
    5
    Views:
    359
    Bart Lateur
    Aug 14, 2003
  3. Sunil
    Replies:
    8
    Views:
    157
    Benjamin Goldberg
    Sep 2, 2003
  4. mothra
    Replies:
    3
    Views:
    379
    Shawn Corey
    Dec 2, 2004
  5. Replies:
    3
    Views:
    117
    Dave Angel
    Jan 8, 2014
Loading...

Share This Page