pht has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,

I'm using Getopt::Euclid to create a command line parser up to this spec:

Usage:
    dbsetup.pl <dbfile> [options]

Required arguments:
    <dbfile>
        Database file name. Existing files will not be overwritten unless the
        force parameter is specified.

Options:
    -f | --force
        Force overwrite of existing database file.

    -m <X> | --mockup=<X>
        Insert mock-up data set X into the database.

The funny behavior I get is that './dbsetup -d', which has an invalid flag, will happily assume '-d' is the filename; is there a way to make it more gnu-getopt like? Ie. -d bails out with 'invalid flag' unless one specifically runs './dbsetup -- -d'?

Even more funny is that './dbsetup -m' will not die of something like '-m should be followed by a parameter and is not AND you didnt specify a filename', but also assume '-m' is the file name! Isn't this erroneous?

Thanks, Michal

  • Comment on Getopt::Euclid, strange behavior & how to fix?

Replies are listed 'Best First'.
Re: Getopt::Euclid, strange behavior & how to fix?
by JavaFan (Canon) on Nov 05, 2008 at 14:03 UTC
    If that's what you want, then maybe Getopt::Euclid isn't the right module for you.

    Getopt::Long will will warn about unknown options, or if a required argument is missing.

      That's a shame, because the idea behind Euclid is really nice.
        I guess you mean that some ideas of Getopt::Euclid are really nice, as you don't like some other ideas of Getopt::Euclid. ;-)
Re: Getopt::Euclid, strange behavior & how to fix?
by toolic (Bishop) on Nov 06, 2008 at 15:04 UTC
    is there a way to make it more gnu-getopt like?
    Yes. You could modify the Getopt::Euclid source code itself to suit your needs. The module seems to be pure Perl, and it has few dependencies. Here is a quote from the POD:
    This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
    Also, according to the POD, you can request new features:
    Please report any bugs or feature requests to bug-getopt-euclid@rt.cpan.org, or through the web interface at http://rt.cpan.org.
Re: Getopt::Euclid, strange behavior & how to fix?
by Anonymous Monk on Nov 07, 2008 at 06:38 UTC

    Not so strange. Your specification says that if your program has only one parameter then that parameter should be assigned to <dbfile>.

    You seem to have two choices: precede your filename with a flag (like --file) or to sanity check the contents of the string <dbfile>.