[Gd-chatter] r11126 - in trunk/libraries: network/koala/sources/koala network/koala/sources/koala-app network/wiki utilities/getopt

cgay at gwydiondylan.org cgay at gwydiondylan.org
Thu Jan 11 08:15:18 CET 2007


Author: cgay
Date: Thu Jan 11 08:15:14 2007
New Revision: 11126

Modified:
   trunk/libraries/network/koala/sources/koala-app/koala-app.dylan
   trunk/libraries/network/koala/sources/koala-app/library.dylan
   trunk/libraries/network/koala/sources/koala/koala-main.dylan
   trunk/libraries/network/koala/sources/koala/library-unix.dylan
   trunk/libraries/network/koala/sources/koala/library.dylan
   trunk/libraries/network/koala/sources/koala/variables.dylan
   trunk/libraries/network/wiki/classes.dylan
   trunk/libraries/network/wiki/library.dylan
   trunk/libraries/network/wiki/wiki.dylan
   trunk/libraries/utilities/getopt/getopt-exports.dylan
   trunk/libraries/utilities/getopt/getopt.dylan
Log:
job: koala
Added print-synopsis method to getopt (should make defargparser-synopsis use
it) and added a distributed argument parser to koala so that using libraries
(like wiki) can add arguments they need.  Added --xmpp command-line argument to
the wiki, mostly to test out the argument parser, but also because xmpp-bot
seems a bit broken.


Modified: trunk/libraries/network/koala/sources/koala-app/koala-app.dylan
==============================================================================
--- trunk/libraries/network/koala/sources/koala-app/koala-app.dylan	(original)
+++ trunk/libraries/network/koala/sources/koala-app/koala-app.dylan	Thu Jan 11 08:15:14 2007
@@ -2,15 +2,10 @@
 Synopsis: Koala HTTP Server Application
 Author:   Carl Gay
 
-define function main
-    () => ()
-  let config-file =
-    if(application-arguments().size > 0)
-      application-arguments()[0]
-    end;
-  start-server(config-file: config-file);
-end;
+// It's really kind of annoying that we have to make another whole
+// library (with two files) just to call the main function.  I wonder
+// if there's a good way to fix it.
 
 begin
-  main();
+  koala-main();
 end;

Modified: trunk/libraries/network/koala/sources/koala-app/library.dylan
==============================================================================
--- trunk/libraries/network/koala/sources/koala-app/library.dylan	(original)
+++ trunk/libraries/network/koala/sources/koala-app/library.dylan	Thu Jan 11 08:15:14 2007
@@ -5,13 +5,13 @@
 define library koala-app
   use dylan;
   use system, import: { operating-system };
-  use koala;
+  use koala,  import: { koala };
 end;
 
 
 define module koala-app
   use dylan;
   use operating-system, import: { application-arguments };
-  use koala, import: { start-server };
+  use koala, import: { koala-main };
 end;
 

Modified: trunk/libraries/network/koala/sources/koala/koala-main.dylan
==============================================================================
--- trunk/libraries/network/koala/sources/koala/koala-main.dylan	(original)
+++ trunk/libraries/network/koala/sources/koala/koala-main.dylan	Thu Jan 11 08:15:14 2007
@@ -22,6 +22,17 @@
     test-koala();
   end;
 
+  add-option-parser-by-type(*argument-list-parser*,
+                            <parameter-option-parser>,
+                            description: "Location of the koala configuration file",
+                            long-options: #("config"),
+                            short-options: #("c"));
+  add-option-parser-by-type(*argument-list-parser*,
+                            <simple-option-parser>,
+                            description: "Display this help message",
+                            long-options: #("help"),
+                            short-options: #("h"));
+
   //init-server();
 end;
 
@@ -29,3 +40,20 @@
   init-koala();
 end;
 
+// This is defined here rather than in koala-app because wiki needs it too.
+define function koala-main ()
+  let parser = *argument-list-parser*;
+  parse-arguments(parser, application-arguments());
+  if (option-value-by-long-name(parser, "help")
+        | ~empty?(parser.regular-arguments))
+    let desc = "The Koala web server, a multi-threaded web server with\n"
+      "Dylan Server Pages and XML RPC, written in Dylan.";
+    print-synopsis(parser,
+                   stream: *standard-output*,
+                   usage: "koala [options]",
+                   description: desc);
+    exit-application(0);
+  else
+    start-server(config-file: option-value-by-long-name(parser, "config"));
+  end;
+end function koala-main;

Modified: trunk/libraries/network/koala/sources/koala/library-unix.dylan
==============================================================================
--- trunk/libraries/network/koala/sources/koala/library-unix.dylan	(original)
+++ trunk/libraries/network/koala/sources/koala/library-unix.dylan	Thu Jan 11 08:15:14 2007
@@ -5,6 +5,7 @@
 License:   Functional Objects Library Public License Version 1.0
 Warranty:  Distributed WITHOUT WARRANTY OF ANY KIND
 
+// If you update this file don't forget to update library.dylan as well.
 
 define library koala
   use functional-dylan,
@@ -25,6 +26,7 @@
 //  use win32-kernel;
   use base64;
   use memory-manager;
+  use getopt;
 
   export koala;
   export koala-extender;
@@ -271,6 +273,11 @@
   create
     static-file-responder;
 
+  // main() function
+  create
+    koala-main,
+    *argument-list-parser*;
+
 end module koala;
 
 // Additional interface for extending the server
@@ -322,7 +329,7 @@
     prefix: "xml$";
   use xml-rpc-common;
   use base64;
-//  use win32-kernel, import: { LoadLibrary, FreeLibrary };
+  use getopt;
 end module httpi;
 
 define module dsp

Modified: trunk/libraries/network/koala/sources/koala/library.dylan
==============================================================================
--- trunk/libraries/network/koala/sources/koala/library.dylan	(original)
+++ trunk/libraries/network/koala/sources/koala/library.dylan	Thu Jan 11 08:15:14 2007
@@ -5,6 +5,7 @@
 License:   Functional Objects Library Public License Version 1.0
 Warranty:  Distributed WITHOUT WARRANTY OF ANY KIND
 
+// If you update this file don't forget to update library-unix.dylan as well.
 
 define library koala
   use functional-dylan,
@@ -25,7 +26,8 @@
   use win32-kernel;
   use base64;
   use memory-manager;
-  
+  use getopt;
+
   export koala;
   export koala-extender;
   export dsp;
@@ -271,6 +273,11 @@
   create
     static-file-responder;
 
+  // main() function
+  create
+    koala-main,
+    *argument-list-parser*;
+
 end module koala;
 
 // Additional interface for extending the server
@@ -296,7 +303,6 @@
   use koala;
   use koala-extender;
   use memory-manager;
-
   use locators,
     rename: { <http-server> => <http-server-url>,
               <ftp-server> => <ftp-server-url>,
@@ -324,6 +330,7 @@
   use xml-rpc-common;
   use win32-kernel, import: { LoadLibrary, FreeLibrary };
   use base64;
+  use getopt;
 end module httpi;
 
 define module dsp

Modified: trunk/libraries/network/koala/sources/koala/variables.dylan
==============================================================================
--- trunk/libraries/network/koala/sources/koala/variables.dylan	(original)
+++ trunk/libraries/network/koala/sources/koala/variables.dylan	Thu Jan 11 08:15:14 2007
@@ -63,3 +63,9 @@
 // to make logging work before vhosts are initialized.
 define variable *temp-log-target*
   = make(<stream-log-target>, stream: *standard-output*);
+
+// Command-line arguments parser.  The expectation is that libraries that use
+// and extend koala (e.g., wiki) may want to add their own <option-parser>s to
+// this before calling koala-main().
+define variable *argument-list-parser* :: <argument-list-parser>
+  = make(<argument-list-parser>);

Modified: trunk/libraries/network/wiki/classes.dylan
==============================================================================
--- trunk/libraries/network/wiki/classes.dylan	(original)
+++ trunk/libraries/network/wiki/classes.dylan	Thu Jan 11 08:15:14 2007
@@ -43,13 +43,18 @@
 end;
 
 define function xmpp-worker ()
-  block()
-    *xmpp-bot* := make(<xmpp-bot>, jid: "dylanbot at jabber.berlin.ccc.de/serva", password: "fnord");
-    sleep(3); //this is for safety reasons, xml-parser is not thread-safe!
-  exception (e :: <condition>)
-    *xmpp-bot* := #f
-  end;
+  if (option-value-by-long-name(*argument-list-parser*, "xmpp"))
+    block()
+      *xmpp-bot* := make(<xmpp-bot>,
+                         jid: "dylanbot at jabber.berlin.ccc.de/serva",
+                         password: "fnord");
+      sleep(3); //this is for safety reasons, xml-parser is not thread-safe!
+    exception (e :: <condition>)
+      *xmpp-bot* := #f
+    end block;
+  end if;
 end;
+
 define method save (diff :: <wiki-page-diff>) => ()
   next-method();
   block()
@@ -94,6 +99,11 @@
 end;
 
 begin
-  main()
+  add-option-parser-by-type(*argument-list-parser*,
+                            <simple-option-parser>,
+                            description: "Whether to enable the XMPP bot",
+                            long-options: #("xmpp"));
+  register-url("/wiki/wiki.css", maybe-serve-static-file);
+  register-init-function(xmpp-worker);
+  koala-main();
 end;
-

Modified: trunk/libraries/network/wiki/library.dylan
==============================================================================
--- trunk/libraries/network/wiki/library.dylan	(original)
+++ trunk/libraries/network/wiki/library.dylan	Thu Jan 11 08:15:14 2007
@@ -20,7 +20,8 @@
   use collection-extensions, import: { sequence-diff };
   use string-extensions, import: { substring-search };
   use xmpp-bot;
-  //use meta;
+  use getopt;
+
   export wiki;
 end;
 
@@ -48,6 +49,7 @@
   use simple-xml, import: { escape-xml };
   use substring-search;
   use xmpp-bot;
+  use getopt;
 end;
 
 

Modified: trunk/libraries/network/wiki/wiki.dylan
==============================================================================
--- trunk/libraries/network/wiki/wiki.dylan	(original)
+++ trunk/libraries/network/wiki/wiki.dylan	Thu Jan 11 08:15:14 2007
@@ -635,15 +635,3 @@
 
 
 define variable *xmpp-bot* = #f;
-define function main
-    () => ()
-  let config-file =
-    if(application-arguments().size > 0)
-      application-arguments()[0]
-    end;
-  register-url("/wiki/wiki.css", maybe-serve-static-file);
-  xmpp-worker();
-  start-server(config-file: config-file);
-end;
-
-

Modified: trunk/libraries/utilities/getopt/getopt-exports.dylan
==============================================================================
--- trunk/libraries/utilities/getopt/getopt-exports.dylan	(original)
+++ trunk/libraries/utilities/getopt/getopt-exports.dylan	Thu Jan 11 08:15:14 2007
@@ -44,6 +44,7 @@
     // <option-parser>
       short-option-names, short-option-names-setter,
       long-option-names, long-option-names-setter,
+      option-description, option-description-setter,
       option-default-value, option-default-value-setter,
       option-might-have-parameters?, option-might-have-parameters?-setter,
       option-value-setter,
@@ -69,6 +70,7 @@
 define module getopt
   use common-dylan, exclude: { format-to-string };
   use option-parser-protocol;
+  use standard-io;
 
   export
     <argument-list-parser>,
@@ -79,6 +81,7 @@
     option-parser-by-long-name,
     option-present?-by-long-name,
     option-value-by-long-name,
+    print-synopsis,
 
     <option-parser>,
       option-present?,

Modified: trunk/libraries/utilities/getopt/getopt.dylan
==============================================================================
--- trunk/libraries/utilities/getopt/getopt.dylan	(original)
+++ trunk/libraries/utilities/getopt/getopt.dylan	Thu Jan 11 08:15:14 2007
@@ -172,7 +172,9 @@
     init-keyword: short-options:,
     init-value: #();
   slot option-might-have-parameters? :: <boolean> = #t;
-
+  slot option-description :: <string>,
+    init-keyword: description:,
+    init-value: "";
   // Information generated by parsing arguments.
   slot option-present? :: <boolean>,
     init-value: #f;
@@ -415,6 +417,52 @@
 end function parse-arguments;
 
 
+// todo -- Generate the initial "Usage: ..." line as well.
+define method print-synopsis
+    (parser :: <argument-list-parser>,
+     #key stream :: <stream> = *standard-output*,
+          usage :: false-or(<string>),
+          description :: false-or(<string>))
+  if (usage) format(stream, "Usage: %s\n", usage); end;
+  if (description) format(stream, "%s\n", description); end;
+  if (usage | description) new-line(stream); end;
+  local method print-option (short, long, description);
+          let short = select (short by instance?)
+                        <list> => ~empty?(short) & first(short);
+                        <string> => short;
+                        otherwise => #f;
+                      end select;
+          let long = select (long by instance?)
+                       <pair> => ~empty?(long) & first(long);
+                       <string> => long;
+                       otherwise => #f;
+                     end select;
+          write(stream, "  ");
+          if (short)
+            format(stream, "-%s", short);
+            write(stream, if (long) ", " else "  " end);
+          else
+            write(stream, "    ");
+          end if;
+          if (long)
+            format(stream, "--%s", long);
+            for (i from 1 to 28 - 2 - size(long))
+              write-element(stream, ' ');
+            end for;
+          else
+            format(stream, "%28s", "");
+          end if;
+          write(stream, description);
+          new-line(stream);
+        end method print-option;
+
+  for (option in option-parsers(parser))
+    print-option(short-option-names(option),
+                 long-option-names(option),
+                 option-description(option));
+  end;
+end method print-synopsis;
+
 /*
   Semi-comprehensible design notes, here for historical interest:
 



More information about the chatter mailing list