Author: bernhard
Date: Mon Dec 26 06:05:52 2005
New Revision: 10677

Modified:
   trunk/runtime/parrot/library/Getopt/Long.pir
   trunk/t/library/getopt_long.t
Log:
Teach 'Getopt/Long.pir' about '--' as the option terminator.


Modified: trunk/runtime/parrot/library/Getopt/Long.pir
==============================================================================
--- trunk/runtime/parrot/library/Getopt/Long.pir        (original)
+++ trunk/runtime/parrot/library/Getopt/Long.pir        Mon Dec 26 06:05:52 2005
@@ -40,6 +40,8 @@ library/Getopt/Long.pir - parse long and
 
 This Parrot library can be used for parsing command line options.
 The subroutine get_options() is exported into the namespace 'Getopt::Long'.
+Everything after '--' is not regarded as an option.
+Options and additional parameters cannot be mixed. Options come first.
 
 =head1 SUBROUTINES
 
@@ -65,6 +67,7 @@ Bernhard Schmalhofer - C<Bernhard.Schmal
 
 The Perl5 module L<Getopt::Long>.
 F<examples/library/getopt_demo.pir>
+F<t/library/getopt_long.t>
 
 =head1 COPYRIGHT
 
@@ -125,7 +128,6 @@ license as The Parrot Interpreter.
 
   # Now that we know about the allowed options,
   # we actually parse the argument vector
-  # TODO: do this correctly
   # shift from argv until a non-option is encountered
   .local pmc opt              # the return PMC
   opt = new Hash
@@ -144,19 +146,27 @@ license as The Parrot Interpreter.
     # have to clone it
     arg = clone arg
 
-    # Is arg a long option string like '--help'
+    # Is arg the option terminator '--'?
+    if arg  == "--" goto HANDLE_OPTION_TERMINATOR
+
+    # Is arg a long option string like '--help'?
     $S0 = substr arg, 0, 2
     if $S0 == "--" goto HANDLE_LONG_OPTION
 
-    # Is arg a short option string like '-v'
+    # Is arg a short option string like '-v'?
     $S0 = substr arg, 0, 1 
     if $S0 == "-" goto HANDLE_SHORT_OPTION
+
     # We are done with the option
     # and we don't want to loose the remaining arguments
     goto FINISH_PARSE_ARGV
 
+    HANDLE_OPTION_TERMINATOR:
+    # The '--' is not part of the remaining options
+    arg = shift argv
+    goto FINISH_PARSE_ARGV
+
     HANDLE_SHORT_OPTION:
-    # TODO: make it work for short options
     arg = shift argv
     arg = clone arg
     # get rid of the leading '-'
@@ -204,7 +214,7 @@ license as The Parrot Interpreter.
     if num_remaining_args > 0 goto NEXT_PARSE_ARGV
 
   FINISH_PARSE_ARGV:
-  # Nothing to do here
+  # Nothing to do 
 
   .return ( opt )
 .end

Modified: trunk/t/library/getopt_long.t
==============================================================================
--- trunk/t/library/getopt_long.t       (original)
+++ trunk/t/library/getopt_long.t       Mon Dec 26 06:05:52 2005
@@ -11,7 +11,7 @@ use Parrot::Test;
 
 =head1 NAME
 
-t/library/getopt_long.t - testing library/Getopt/Long.pir
+t/library/getopt_long.t - testing the module Getopt/Long.pir
 
 =head1 SYNOPSIS
 
@@ -20,7 +20,7 @@ t/library/getopt_long.t - testing librar
 =head1 DESCRIPTION
 
 This test program tries to handle command line arguments with the
-library F<runtime/parrot/library/Getopt/Long.pir>.
+module F<runtime/parrot/library/Getopt/Long.pir>.
 
 =cut
 
@@ -145,6 +145,77 @@ You have passed the additional argument:
 All args have been parsed.
 OUT
 
+# no. 2
+pir_output_is( <<'CODE', <<'OUT', "option terminator" );
+
+.sub test :main 
+
+  load_bytecode "Getopt/Long.pbc"
+  .local pmc get_options
+  find_global get_options, "Getopt::Long", "get_options"    # get the relevant 
sub
+
+  # Assemble specification for get_options
+  # in an array of format specifiers
+  .local pmc opt_spec    
+  opt_spec = new ResizableStringArray
+  # --string, string
+  push opt_spec, "string=s"
+  # --integer, integer
+  push opt_spec, "integer=i"
+
+  # This comes usually from the command line
+  .local pmc argv
+  argv = new PerlArray
+  push argv, "--string=asdf"
+  push argv, "--"
+  push argv, "--integer=42"
+  push argv, "something"
+
+  .local pmc opt
+  ( opt ) = get_options( argv, opt_spec )
+
+  # Now we do what the passed options tell
+  .local int is_defined
+
+  # handle the string option
+  is_defined = defined opt["string"]
+  unless is_defined goto NO_STRING_OPTION
+    .local string string_option
+    string_option = opt["string"]
+    print "You have passed the option '--string'. The value is '"
+    print string_option
+    print "'.\n"
+    goto END_STRING_OPTION
+  NO_STRING_OPTION:
+    print "You haven't passed the option '--string'. This is fine with me.\n"
+  END_STRING_OPTION:
+
+
+  # For some reason I can not shift from argv
+  .local string other_arg
+  .local int    cnt_other_args
+  cnt_other_args = 0
+  .local int num_other_args
+  num_other_args = argv
+  goto CHECK_OTHER_ARG_LOOP
+  REDO_OTHER_ARG_LOOP:
+    other_arg = argv[cnt_other_args]
+    print "You have passed the additional argument: '"
+    print other_arg
+    print "'.\n"
+    inc cnt_other_args
+  CHECK_OTHER_ARG_LOOP:
+  if cnt_other_args < num_other_args goto REDO_OTHER_ARG_LOOP
+  print "All args have been parsed.\n"
+.end
+
+CODE
+You have passed the option '--string'. The value is 'asdf'.
+You have passed the additional argument: '--integer=42'.
+You have passed the additional argument: 'something'.
+All args have been parsed.
+OUT
+
 =head1 AUTHOR
 
 Bernhard Schmalhofer - C<[EMAIL PROTECTED]>
@@ -157,5 +228,5 @@ F<runtime/parrot/library/Getopt/Long.pir
 
 
 ## remember to change the number of tests! :-)
-BEGIN { plan tests => 1; }
+BEGIN { plan tests => 2; }
 

Reply via email to