GetOpts: boolean and argument value combos

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default GetOpts: boolean and argument value combos

    Hey Perl Peps,

    I am stuck in the mudd and hoping someone can give me a few clues that will help get me back on track.

    I want to submit some arguments like so:
    ../script1.pl -book -title HP3 -chapter 04

    NOTE: -book does not have an argument
    Now I have been using GetOpt::Long and everything works except the first value -book. Since I want to treat -book like a boolean, I do
    not give it value, which causes problems because I guess it needs to receive some sort of value in it's current format. This is causing problems
    where the value of the second argument -title does not show. Is there a syntactical form I can follow to treat -book like a boolean and feed the other arguments
    values? Can I continue to use Getopt::Long?

    Any suggestions would be much appreciated.

    CODE
    -----------------------------------
    use Getopt::Long;
    use File::Path;
    #use strict;


    GetOptions (
    "book=s" =>\$book,
    "magazine=s" =>\$magazine,
    "video=s" =>\$video,
    "c=s" => \$chapter,
    "t=s" => \$title,
    );

    ##Routes to appropriate subroutine
    print "SHOW TEST: $sPath\n";
    if ($book) {
    itemCreator($chapter, $title);
    }
    ###SUBROUTINES
    sub showCreator {
    my($chapter, $title) = @_;
    print "\n";
    print "CHAPTER: $chapter\n"; ###Not appearing because -book did not receive an arguement :(
    print "TITLE: $title: $template\n";

    }


    Trina Espinoza Guest

  2. Similar Questions and Discussions

    1. #40782 [NEW]: boolean argument type to function throws Catchable fatal error.
      From: tims at arizona dot edu Operating system: Debian Linux PHP version: 5.2.1 PHP Bug Type: Scripting Engine problem Bug...
    2. Getopts::std
      I am using various switches in my script. I have the Getopts::std module and I wrote the following line: getopts('F:S:D:T:I:plen', \%OPTS); How...
    3. boolean cfsqltype
      Can someone please advise what cfsqltype should I used for boolean (true/false)? I've tried cf_sql_bit, but my results were inconsistent. i.e. ...
    4. Problem using Getopts package
      I am getting errors when I try to get command line parameters. For example this small code doesn't work on my machine....
    5. Big favour needed to test site combos
      Hi All I know this is a big favour, but I knew I'd get good honest feedback from you. Basically users are saying that I'm having time out...
  3. #2

    Default RE: GetOpts: boolean and argument value combos

    > Since I want to treat -book like a boolean

    The problem is that you are telling Getopt::Long that "book" expects a
    string value:
    > "book=s" =>\$book,
    This is untested, but this should work.
    > "book" =>\$book,
    It sets the value of $book to 1 if the option is present.

    See the Getopt::Long docs, the section on Simple Options.
    [url]http://search.cpan.org/author/JV/Getopt-Long-2.34/lib/Getopt/Long.pm#Simple_[/url]
    options

    Rob


    -----Original Message-----
    From: Trina Espinoza [mailto:trina@theorphanage.com]
    Sent: Tuesday, September 23, 2003 9:11 PM
    To: [email]beginners@perl.org[/email]
    Subject: GetOpts: boolean and argument value combos


    Hey Perl Peps,

    I am stuck in the mudd and hoping someone can give me a few clues that will
    help get me back on track.

    I want to submit some arguments like so:
    ../script1.pl -book -title HP3 -chapter 04

    NOTE: -book does not have an argument
    Now I have been using GetOpt::Long and everything works except the first
    value -book. Since I want to treat -book like a boolean, I do
    not give it value, which causes problems because I guess it needs to receive
    some sort of value in it's current format. This is causing problems
    where the value of the second argument -title does not show. Is there a
    syntactical form I can follow to treat -book like a boolean and feed the
    other arguments
    values? Can I continue to use Getopt::Long?

    Any suggestions would be much appreciated.

    CODE
    -----------------------------------
    use Getopt::Long;
    use File::Path;
    #use strict;


    GetOptions (
    "book=s" =>\$book,
    "magazine=s" =>\$magazine,
    "video=s" =>\$video,
    "c=s" => \$chapter,
    "t=s" => \$title,
    );

    ##Routes to appropriate subroutine
    print "SHOW TEST: $sPath\n";
    if ($book) {
    itemCreator($chapter, $title);
    }
    ###SUBROUTINES
    sub showCreator {
    my($chapter, $title) = @_;
    print "\n";
    print "CHAPTER: $chapter\n"; ###Not appearing because -book did not
    receive an arguement :(
    print "TITLE: $title: $template\n";

    }

    Rob Hanson Guest

  4. #3

    Default Re: GetOpts: boolean and argument value combos


    --On Tuesday, September 23, 2003 18:10 -0700 Trina Espinoza
    <trina@theorphanage.com> wrote:
    > Hey Perl Peps,
    >
    > I am stuck in the mudd and hoping someone can give me a few clues
    > that will help get me back on track.
    >
    > I want to submit some arguments like so:
    > ./script1.pl -book -title HP3 -chapter 04
    >
    > NOTE: -book does not have an argument
    > Now I have been using GetOpt::Long and everything works except the
    > first value -book. Since I want to treat -book like a boolean, I do
    > not give it value, which causes problems because I guess it needs
    > to receive some sort of value in it's current format. This is
    > causing problems where the value of the second argument -title does
    > not show. Is there a syntactical form I can follow to treat -book
    > like a boolean and feed the other arguments values? Can I continue
    > to use Getopt::Long?
    Sure. There are two ways to do this, depending on whether you want
    '--nobook' to set 'book' to false or not. "book" by itself does the
    plain version: false if not present, true if present. "book=!" does
    false if not present, true if present and not followed by '--nobook'.
    (If nobook found afterward the book value will be set to false.)

    Daniel T. Staal

    ---------------------------------------------------------------
    This email copyright the author. Unless otherwise noted, you
    are expressly allowed to retransmit, quote, or otherwise use
    the contents for non-commercial purposes. This copyright will
    expire 5 years after the author's death, or in 30 years,
    whichever is longer, unless such a period is in excess of
    local copyright law.
    ---------------------------------------------------------------
    Daniel Staal Guest

  5. #4

    Default Re: GetOpts: boolean and argument value combos

    Thanks for the suggestions. Your help was much appreciated. I ended using
    "book" =>\$book, instead of book=s in my code.
    After that my script no longer expected the string, which is good news.
    Thanks to you both for helping me with that. I can continue
    to script away again :)

    -T
    > >
    > >
    > > ----- Original Message -----
    > > From: "Trina Espinoza" <trina@theorphanage.com>
    > > To: <beginners@perl.org>
    > > Sent: Tuesday, September 23, 2003 6:10 PM
    > > Subject: GetOpts: boolean and argument value combos
    > >
    > >
    > > Hey Perl Peps,
    > >
    > > I am stuck in the mudd and hoping someone can give me a few clues that
    > will
    > > help get me back on track.
    > >
    > > I want to submit some arguments like so:
    > > ./script1.pl -book -title HP3 -chapter 04
    > >
    > > NOTE: -book does not have an argument
    > > Now I have been using GetOpt::Long and everything works except the first
    > > value -book. Since I want to treat -book like a boolean, I do
    > > not give it value, which causes problems because I guess it needs to
    > receive
    > > some sort of value in it's current format. This is causing problems
    > > where the value of the second argument -title does not show. Is there a
    > > syntactical form I can follow to treat -book like a boolean and feed the
    > > other arguments
    > > values? Can I continue to use Getopt::Long?
    > >
    > > Any suggestions would be much appreciated.
    > >
    > > CODE
    > > -----------------------------------
    > > use Getopt::Long;
    > > use File::Path;
    > > #use strict;
    > >
    > >
    > > GetOptions (
    > > "book=s" =>\$book,
    > > "magazine=s" =>\$magazine,
    > > "video=s" =>\$video,
    > > "c=s" => \$chapter,
    > > "t=s" => \$title,
    > > );
    > >
    > > ##Routes to appropriate subroutine
    > > print "SHOW TEST: $sPath\n";
    > > if ($book) {
    > > itemCreator($chapter, $title);
    > > }
    > > ###SUBROUTINES
    > > sub showCreator {
    > > my($chapter, $title) = @_;
    > > print "\n";
    > > print "CHAPTER: $chapter\n"; ###Not appearing because -book did
    > not
    > > receive an arguement :(
    > > print "TITLE: $title: $template\n";
    > >
    > > }
    > >
    > >
    >
    Trina Espinoza Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139