Author: jisom
Date: Thu Jan 12 03:15:53 2006
New Revision: 11120

Modified:
   trunk/runtime/parrot/library/Getopt/Obj.pir
   trunk/t/library/getopt_obj.t
Log:
Update Getopt::Obj to support a "push" semantic.
Close to common format.


Modified: trunk/runtime/parrot/library/Getopt/Obj.pir
==============================================================================
--- trunk/runtime/parrot/library/Getopt/Obj.pir (original)
+++ trunk/runtime/parrot/library/Getopt/Obj.pir Thu Jan 12 03:15:53 2006
@@ -13,6 +13,10 @@ library/Getopt/Obj.pir - parse long and 
      getopts = new "Getopt::Obj"
      getopts."notOptStop"(1)
 
+     # these two are identical, with the exception of the call to name
+
+     push getopts, "foo|f:s"
+
      $P0 = getopts."add"()
      $P0."name"("FooBar")
      $P0."long"("foo")
@@ -20,6 +24,15 @@ library/Getopt/Obj.pir - parse long and 
      $P0."optarg"(1)
      $P0."type"(.String)
 
+     # these two are identical
+
+     push getopts, "bar|b=s"
+
+     $P0 = getopts."add"()
+     $P0."long"("bar")
+     $P0."short"("b")
+     $P0."type"(.String)
+
      .local pmc opts
      opts = getopts."get_options"(argv)
      .local string foo
@@ -34,6 +47,7 @@ and of these forms, currently:
  --foo=bar         Set foo to bar
  -f bar            Set f to bar (with conditions)
  -fthis=that       If f is a Hash, set key 'this' to 'that'
+ -f this=that      If f is a Hash, set key 'this' to 'that'
  -f                Set f (if it's Boolean or has an optional argument)
  -fbar             Set f to bar if it's not a Boolean
  -abc              If -a -b and -c are all Booleans, set them all
@@ -177,16 +191,19 @@ error_0:
     ################  SHORT  ###########################
 
 shortarg:
+
     # get the first char
     key = substr arg, 1, 1
     # and the others...
     val = substr arg, 2
 
     (name, spec) = self."getNameForKey"(key)
+
     key = name
 
     $I1 = spec."type"()
     $I2 = length val
+
     unless $I1 == .Boolean goto else_6
     if $I2 == 0 goto beginstore
     # ok, boolean and bundled
@@ -235,9 +252,10 @@ error_2:
     throw $P0
 
     ################  STORE  ###########################
-
 beginstore:
+
     (name, spec) = self."getNameForKey"(key)
+
     # Store the value...
     $I0 = spec."type"()
     $S0 = typeof $I0
@@ -353,6 +371,118 @@ endfor:
     assign argv, argnew
 .end
 
+=item C<__push_string(STRING format)>
+
+A vtable method, invoked by e.g. C<push getopts, "foo|f=s">.  The format is as 
such.
+
+=over 4
+
+=item "foo|f"
+
+A long option of "foo" and a short option of "f" is set to C<.Boolean>.
+
+=item "foo"
+
+A long option of "foo" is set to C<.Boolean>.
+
+=item "f"
+
+A short option of "f" is set to C<.Boolean>.
+
+=item "f=s"
+
+A short option of "f" is set to C<.String>.  Use C<i> for C<.Integer>, C<f> for
+C<.Float>, C<@> for an C<.Array>, and C<%> for a C<.Hash>.
+
+
+=item "foo:s"
+
+A long option of "foo" is set to C<.String>, with "optarg" set to a true value.
+
+=back
+
+=cut
+
+.sub "__push_string" :method
+    .param string format
+    .local string key, type, long, short
+    $P0 = self."add"()
+
+    $I0 = index format, ':'
+    if $I0 == -1 goto process
+    substr format, $I0, 1, '='
+    $P0."optarg"(1)
+process:
+    $I0 = index format, '='
+    unless $I0 == -1 goto else_0
+    # Is a boolean
+    $I0 = index format, '|'
+    unless $I0 != -1 goto else_1
+    # long then a short
+    long = substr format, 0, $I0
+    inc $I0
+    # force one character
+    short = substr format, $I0, 1
+    $P0."long"(long)
+    $P0."short"(short)
+    .return()
+else_1:
+    $I0 = length format
+    unless $I0 == 1 goto long_0
+    $P0."short"(format)
+    .return()
+long_0:
+    $P0."long"(format)
+    .return()
+#---------------------------
+else_0:
+    $I0 = index format, '='
+    key = substr format, 0, $I0
+    inc $I0
+    # force one character
+    type = substr format, $I0, 1
+
+    if type == 's' goto str
+    if type == '@' goto array
+    if type == '%' goto hash
+    if type == 'i' goto integer
+    if type == 'f' goto flt
+str:
+    $P0."type"(.String)
+    goto endcase
+array:
+    $P0."type"(.Array)
+    goto endcase
+hash:
+    $P0."type"(.Hash)
+    goto endcase
+integer:
+    $P0."type"(.Integer)
+    goto endcase
+flt:
+    $P0."type"(.Float)
+endcase:
+    $I0 = index key, '|'
+    unless $I0 == -1 goto endif_2
+    # only short or long
+    $I0 = length key
+    unless $I0 == 1 goto else_3
+    $P0."short"(key)
+    .return()
+else_3:
+    $P0."long"(key)
+    .return()
+endif_2:
+    # short and long
+    $I0 = index key, '|'
+    long = substr key, 0, $I0
+    inc $I0
+    short = substr key, $I0
+    $P0."long"(long)
+    $P0."short"(short)
+    .return()
+.end
+
 =item C<Getopt::Obj::Spec add()>
 
 Adds a new option to the parsing.  You don't need to know what class it is
@@ -451,7 +581,7 @@ When a required argument is missing, thr
     #printerr "\n"
     #exit 1
     $P0 = new .Exception
-       $P0["_message"] = "Missing a required argument"
+    $P0["_message"] = "Missing a required argument"
     throw $P0
 .end
 
@@ -774,5 +904,10 @@ Copyright (C) 2006 The Perl Foundation. 
 This program is free software. It is subject to the same
 license as The Parrot Interpreter.
 
+=for vim
+
+" vim: ts=4 expandtab
+
 =cut
 
+

Modified: trunk/t/library/getopt_obj.t
==============================================================================
--- trunk/t/library/getopt_obj.t        (original)
+++ trunk/t/library/getopt_obj.t        Thu Jan 12 03:15:53 2006
@@ -556,137 +556,100 @@ argv[0] is -
 OUT
 
 # 13
-pir_output_is(<<'CODE', <<'OUT', "complex example, touch of everything");
-.include "library/dumper.pir"
+pir_output_is(<<'CODE', <<'OUT', "push interface");
 .sub main :main 
        .local pmc argv
        argv = new .ResizablePMCArray
-       push argv, '-DHi=Hello'
-       push argv, '-DBye=Good Bye'
-       push argv, '-DDefined'
-       push argv, '--define=hi=hello'
-       push argv, '--ignored'
-       push argv, '-i'
-       push argv, '--ignored=hello'
        push argv, '--foo=file'
-       push argv, '-I/usr/include'
-       push argv, 'argv'
-       push argv, '-I'
-       push argv, '/usr/local/include'
-       push argv, '-abc'
-       push argv, '-e'
-       push argv, 'codea'
-       push argv, 'stay'
-       push argv, '-'
-       push argv, '--bar=3.1'
-       push argv, '--baz=z'
-       push argv, '--ban=3.14'
-       push argv, '--baz'
-       push argv, 'with me'
-       push argv, '-f'
-       push argv, 'file.txt'
-       push argv, '-e'
-       push argv, 'codeb'
-       push argv, '-r'
-       push argv, '12'
+       push argv, '-bfile.txt'
        push argv, '-x'
-       push argv, 'foobar'
+       push argv, 'file.t'
+       push argv, '-z'
+       push argv, 'text'
+       push argv, '-I'
+       push argv, 'texta'
+       push argv, '-I'
+       push argv, 'textb'
+       push argv, '-Dfoo=bar'
+       push argv, '--define=bax=baz'
+       push argv, '-Dfoobar'
 
        load_bytecode "Getopt/Obj.pir"
        .local pmc getopts
        getopts = new "Getopt::Obj"
 
-       $P0 = getopts."add"()
-       $P0."name"("Foo")
-       $P0."long"("foo")
-       $P0."short"("f")
-       $P0."type"(.String)
-
-       $P0 = getopts."add"()
-       $P0."name"("Bar")
-       $P0."long"("bar")
-       $P0."short"("r")
-       $P0."type"(.Integer)
-
-       $P0 = getopts."add"()
-       $P0."long"("ban")
-       $P0."type"(.Float)
-
-       $P0 = getopts."add"()
-       $P0."long"("baz")
-       $P0."short"("z")
-       $P0."type"(.Boolean)
-
-       $P0 = getopts."add"()
-       $P0."name"("FooBar")
-       $P0."long"("foobar")
-       $P0."short"("x")
-       $P0."type"(.String)
-
-       $P0 = getopts."add"()
-       $P0."name"("code")
-       $P0."short"("e")
-       $P0."type"(.Array)
-
-       $P0 = getopts."add"()
-       $P0."short"("I")
-       $P0."type"(.Array)
-
-       $P0 = getopts."add"()
-       $P0."long"("define")
-       $P0."short"("D")
-       $P0."type"(.Hash)
-
-       $P0 = getopts."add"()
-       $P0."name"("alpha")
-       $P0."short"("a")
-       $P0 = getopts."add"()
-       $P0."name"("beta")
-       $P0."short"("b")
-       $P0 = getopts."add"()
-       $P0."name"("cab")
-       $P0."short"("c")
-       $P1 = getopts."get_options"(argv)
+       push getopts, 'foo=s'
+       push getopts, 'bar|b=s'
+       push getopts, 'bax|x=s'
+       push getopts, 'baz|z:s'
+       push getopts, 'I=@'
+       push getopts, 'define|D:%'
 
-       _dumper(argv, "argv")
-       _dumper($P1, "return")
+       $P1 = getopts."get_options"(argv)
+       
 
+       $S0 = $P1["foo"]
+       print "foo is "
+       print $S0
+       print "\n"
+
+       $S0 = $P1["bar"]
+       print "bar is "
+       print $S0
+       print "\n"
+
+       $S0 = $P1["bax"]
+       print "bax is "
+       print $S0
+       print "\n"
+
+       $S0 = $P1["baz"]
+       print "baz is "
+       print $S0
+       print "\n"
+
+       $S0 = $P1["I";0]
+       print "I[0] is "
+       print $S0
+       print "\n"
+
+       $S0 = $P1["I";1]
+       print "I[1] is "
+       print $S0
+       print "\n"
+
+       $S0 = $P1["define";"foo"]
+       print "define[foo] is "
+       print $S0
+       print "\n"
+
+       $S0 = $P1["define";"bax"]
+       print "define[bax] is "
+       print $S0
+       print "\n"
+
+       $S0 = $P1["define";"foobar"]
+       print "define[foobar] is "
+       print $S0
+       print "\n"
+
+       $S0 = argv[0]
+       print "argv[0] is "
+       print $S0
+       print "\n"
 
 .end
 CODE
-"argv" => ResizablePMCArray (size:7) [
-    "--ignored",
-    "-i",
-    "--ignored=hello",
-    "argv",
-    "stay",
-    "-",
-    "with me"
-]
-"return" => Hash {
-    "Bar" => 12,
-    "Foo" => "file.txt",
-    "FooBar" => "foobar",
-    "I" => ResizablePMCArray (size:2) [
-        "/usr/include",
-        "/usr/local/include"
-    ],
-    "alpha" => 1,
-    "ban" => 3.14,
-    "baz" => 1,
-    "beta" => 1,
-    "cab" => 1,
-    "code" => ResizablePMCArray (size:2) [
-        "codea",
-        "codeb"
-    ],
-    "define" => Hash {
-        "Bye" => "Good Bye",
-        "Defined" => 1,
-        "Hi" => "Hello",
-        "hi" => "hello"
-    }
-}
+foo is file
+bar is file.txt
+bax is file.t
+baz is 
+I[0] is texta
+I[1] is textb
+define[foo] is bar
+define[bax] is baz
+define[foobar] is 1
+argv[0] is text
 OUT
 
 =head1 AUTHOR

Reply via email to