Changes to NGetOpt in newgetopt.pl 
Author Message
 Changes to NGetOpt in newgetopt.pl

I made some changes to NGetOpt in the newgetopt.pl perl library:

    1) allow options to be abbreviated to the shortest unique prefix
       (added the variable $allowabbrev to toggle this). Note that
       an exact match always is always preferred over a partial match.

    2) add an extra opt-specifier '>' to denote an option that is an alias
       for another option. This provides the ability to have 2 or more
       options mean the same thing, but to only set one opt_XXX variable
       regardless of which option is given are given (it can also be used
       to disambiguate prefixes that are too short to be unique).

       For example:
         do NGetOpt("checkin", "ci>checkin", "checkout", "co>checkout");

       now lets me use '-ci' or '-checkin' to set "$opt_checkin"

       Also:
         do NGetOpt("help", "heapsize=i", "h>help");

       lets me specify that '-h' will match '-help' even though it might
       have been considered ambiguous with '-heapsize. Note however that
       '-he' would still be ambiguous (unless I also add "he>help").

    3) If $ignorecase was set (which was the default), then

         do NGetOpt("Foo", "bar")

       would set "$opt_foo" (instead of "$opt_Foo") if '-Foo' was given.
       I changed it to set $opt_Foo. This is probably the correct behavior,
       but may break existing scripts. I will understand if you choose not
       too accept this fix, however, if you do NOT, this effect should be
       documented (that it lowercases the name of the $opt_XXX variable if
       $ignorecase is set).

I am not yet proficient at writing fast, efficient perl code, so if you
have the time or inclination to try and make my changes run faster, by all
means do so (and drop me a note saying what you did).

Context diffs follow:

*** /usr/local/lib/perl/newgetopt.pl    Wed Oct  5 13:27:21 1994
--- /u/appleton/newgetopt.pl    Mon Dec  5 12:44:24 1994
***************
*** 18,26 ****
--- 18,31 ----
  #    for mandatory arguments or ":" for optional arguments) and an
  #    argument type specifier: "n" or "i" for integer numbers, "f" for
  #    real (fix) numbers or "s" for strings.
+ #

  #    Value(s) are not set, but pushed.
  #
+ #    The argument specifier ">" may be used, followed by the name of
+ #    another option, to indicate that the option is an alias for the
+ #    option to the right of the ">" character).
+ #
  #  - if the first option of the list consists of non-alphanumeric
  #    characters only, it is interpreted as a generic option starter.
  #    Everything starting with one of the characters from the starter
***************
*** 52,57 ****
--- 57,69 ----
  #    -foo --          -> $opt_foo = '--'
  #
  # HISTORY
+ # 5-Dec-1994            Brad Appleton
+ #    Add $allowabbrev to allow options to be abbreviated to a unique prefix.
+ #    Add '>' option specifier for options that are aliases for another option.
+ #    If $ignorecase is set and an option name is specified as "Foo", then
+ #      the variable set should be $opt_Foo (not $opt_foo).
+ #
+
  # 2-Jun-1992          Johan Vromans  
  #    Do not use //o to allow multiple NGetOpt calls with different delimeters.
  #    Prevent typeless option from using previous $array state.
***************
*** 78,87 ****
  #    Set options w/o argument to 1.
  #    Correct the dreadful semicolon/require bug.

-
  {   package newgetopt;
      $debug = 0;                       # for debugging
      $ignorecase = 1;          # ignore case when matching options
  }

  sub NGetOpt {
--- 90,99 ----
  #    Set options w/o argument to 1.
  #    Correct the dreadful semicolon/require bug.

  {   package newgetopt;
      $debug = 0;                       # for debugging
      $ignorecase = 1;          # ignore case when matching options
+     $allowabbrev = 1;           # allow abbreviation to a unique prefix
  }

  sub NGetOpt {
***************
*** 113,125 ****
      # Verify correctness of optionlist.
      %opctl = ();

!       $opt =~ tr/A-Z/a-z/ if $ignorecase;

!           print STDERR ("Error in option spec: \"", $opt, "\"\n");
            $error++;
            next;
        }
-       $opctl{$1} = defined $2 ? $2 : "";
      }

      return 0 if $error;
--- 125,145 ----
      # Verify correctness of optionlist.
      %opctl = ();


!           $opctl{$1} = defined $2 ? $2 : "";
!             if ( $2 =~ /^>(\w+)$/ ) {
!                 unless ( defined $opctl{$1} ) {
!                     print STDERR ("Error in option spec: \"", $opt, "\"\n",
!                         "(alias must refer to a previously defined option)\n");
!                   $error++;
!                   next;
!                 }
!             }
!         } else {
!             print STDERR ("Error in option spec: \"", $opt, "\"\n");
            $error++;
            next;
        }
      }

      return 0 if $error;
***************
*** 158,169 ****
        }

        # Look it up.
!       $opt =~ tr/A-Z/a-z/ if $ignorecase;
!       unless  ( defined ( $type = $opctl{$opt} ) ) {
!           print STDERR ("Unknown option: ", $opt, "\n");
            $error++;
            next;
!       }

        # Determine argument status.
        print STDERR ("=> found \"$type\" for ", $opt, "\n") if $debug;
--- 178,213 ----
        }

        # Look it up.
!         $grepexp = 'm/^${opt}';
!         $grepexp .= '$' unless ($allowabbrev);
!         $grepexp .= '/';
!         $grepexp .= 'i' if ($ignorecase);
!         print STDERR ("=> grepexp = \"$grepexp\" for $opt\n") if $debug;


!            ## See if there was an exact match
!            $grepexp =~ s|/(i?)$|\$/$1|;


!         }
!         print STDERR sprintf("=> $opt matches: \"%s\"\n",




!             print STDERR ("$_ option: ", $opt, "\n");
            $error++;
            next;
!         }

!         $type = $opctl{$opt};
!
!         ## See if we matched an alias
!         while ( $type =~ /^>(\w+)$/ ) {
!             print STDERR ("=> found \"$1\" for alias \"$opt\"\n") if $debug;
!             $opt = $1;
!             $type = $opctl{$opt};
!         }

        # Determine argument status.
        print STDERR ("=> found \"$type\" for ", $opt, "\n") if $debug;

--

 "And miles to go before I sleep."    DISCLAIMER: I said it, not my employer!



Sat, 24 May 1997 05:02:51 GMT  
 Changes to NGetOpt in newgetopt.pl
Well, I heard from the author of newgetopt.pl who told me that the
features I added are in version 1.14. Here is his reply:

[Quoting Brad Appleton, on December  5 1994, 13:03, in "Changes to newgetopt"]

Quote:
> I made some changes to NGetOpt in the newgetopt.pl perl library.

It appears to me that you took the 1.13 version that comes with perl
4.036. The more recent 1.14 version implements all your features (and
more).

Quote:
>     1) allow options to be abbreviated to the shortest unique prefix
>        (added the variable $allowabbrev to toggle this). Note that
>        an exact match always is always preferred over a partial match.

#   $autoabbrev      Allow option names to be abbreviated to uniqueness.
#                    Default is 1 unless environment variable
#                    POSIXLY_CORRECT has been set.

Quote:
>     2) add an extra opt-specifier '>' to denote an option that is an alias
>        for another option. This provides the ability to have 2 or more
>        options mean the same thing, but to only set one opt_XXX variable
>        any are given (it can also be used to disambiguate prefixes that
>        are too short).

# The option name may actually be a list of option names, separated by
# '|'s, e.g. "foo|bar|blech=s". In this example, options 'bar' and
# 'blech' will set $opt_foo instead.

Quote:
>     3) If $ignorecase was set (which was the default), then

>          do NGetOpt("Foo", "bar")

>        would set "$opt_foo" (instead of "$opt_Foo") if '-Foo' was given.
>        I changed it to set $opt_Foo.

I agree that this is the correct behaviour. I don't think it will
affect existing programs so I will fix it.

Thanks for the efforts, I really hope that the 1.14 version suits you.
It's in ftp://ftp.nl.net/pub/programming/languages/perl/newgetopt.pl.gz .

--

 "And miles to go before I sleep."    DISCLAIMER: I said it, not my employer!



Sat, 24 May 1997 23:55:52 GMT  
 Changes to NGetOpt in newgetopt.pl
Quote:

>I made some changes to NGetOpt in the newgetopt.pl perl library:

Have you taken a look at the Getopt::Long module in the Perl 5 library?
It's based on newgetopt.pl 1.14 dated 12th Feb 1994. It supports
everything you want and more.

Quote:

> "And miles to go before I sleep."    DISCLAIMER: I said it, not my employer!

Regards,
Tim Bunce.


Sun, 25 May 1997 02:26:01 GMT  
 Changes to NGetOpt in newgetopt.pl

Quote:


>>I made some changes to NGetOpt in the newgetopt.pl perl library:

>Have you taken a look at the Getopt::Long module in the Perl 5 library?
>It's based on newgetopt.pl 1.14 dated 12th Feb 1994. It supports
>everything you want and more.

I appears that the Getopt::Long module is based on newgetopt.pl 1.14
(which indeed has almost all Brad needs, and more), but the 'normal'
newgetopt.pl in the perl5 library is still the old 1.13 version.
A small packaging bug...


Tue, 27 May 1997 22:18:31 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Loading Lotus spreadsheet into Delphi (DB)Grid

2. finding other loaded programs

3. abbrev.pl + newgetopt.pl = NEAT!

4. Source: newgetopt.pl version 1.14

5. Abbreviation support for newgetopt.pl

6. any additional docs for newgetopt.pl?

7. newgetopt.pl

8. Newgetopt.pl, version 1.13.

9. Official patch to newgetopt.pl 1.9

10. bug in newgetopt.pl

11. newgetopt.pl by Johan Vromans version 1.13

12. newgetopt.pl

 

 
Powered by phpBB® Forum Software