From: Jeff
Subject: Example using getopts module?
Date: 
Message-ID: <pan.2005.01.11.06.57.36.379460@cunningham.net>
Hi; I'm new to lisp and have been struggling with what I would think are
fairly simple concepts here for the past couple days. As an exercise I
have been trying to duplicate the functionality of a perl script I wrote
sometime ago. I needed getopt functionality, so I downloaded the package
on CLiki and installed it using asdf (at least I think I did). It shows up
in ~/.asdf-install-dir/site and /systems. But I can't figure out how to
invoke it. The following code seems to load it:

(asdf:operate 'asdf:load-op :getopt)
(defpackage getopt-temp
  (:use #:getopt #:cl))
(in-package getopt-temp)

; loading system definition from /usr/share/common-lisp/systems/getopt.asd
into #<PACKAGE ;   ASDF10087>
; registering #<SYSTEM GETOPT #x203FB536> as GETOPT ; registering #<SYSTEM
GETOPT-TESTS #x204228FE> as GETOPT-TESTS 0 errors, 0 warnings

Would someone help me out here? How do I use it to parse a command line
for options, option values and any bare arguments? I assume I have to
build a list of some kind of option structures with the short and long
option codes, default values, etc.

I've looked in Graham's books and in Winston and Horn, and I've googled
all over the place without turning up anymore than I have above. Does
someone have a simple piece of code that runs a simple test case. I think
it would help a great deal.

Thanks

-Jeff Cunningham

From: Fred Gilham
Subject: Re: Example using getopts module?
Date: 
Message-ID: <u7y8f0xa4l.fsf@snapdragon.csl.sri.com>
Jeff wrote:
> Would someone help me out here? How do I use it to parse a command
> line for options, option values and any bare arguments? I assume I
> have to build a list of some kind of option structures with the
> short and long option codes, default values, etc.

This isn't portable, so it isn't part of the Common Lisp standard.
You need to say what implementation of Lisp you are using.

-- 
Fred Gilham                                       ······@csl.sri.com
	  Comprehensive Computer Language Preference Survey
Do you like Lisp?  (Check one)
     [ ] Yes     [ ] Sure     [ ] You bet     [ ] Yep     [ ] Da
From: Pascal Bourguignon
Subject: Re: Example using getopts module?
Date: 
Message-ID: <87k6qkf0zu.fsf@thalassa.informatimago.com>
Jeff <·······@cunningham.net> writes:

> Hi; I'm new to lisp and have been struggling with what I would think are
> fairly simple concepts here for the past couple days. As an exercise I
> have been trying to duplicate the functionality of a perl script I wrote
> sometime ago. I needed getopt functionality, so I downloaded the package
> on CLiki and installed it using asdf (at least I think I did). It shows up
> in ~/.asdf-install-dir/site and /systems. But I can't figure out how to
> invoke it. The following code seems to load it:
> 
> (asdf:operate 'asdf:load-op :getopt)
> (defpackage getopt-temp
>   (:use #:getopt #:cl))
> (in-package getopt-temp)
> 
> ; loading system definition from /usr/share/common-lisp/systems/getopt.asd
> into #<PACKAGE ;   ASDF10087>
> ; registering #<SYSTEM GETOPT #x203FB536> as GETOPT ; registering #<SYSTEM
> GETOPT-TESTS #x204228FE> as GETOPT-TESTS 0 errors, 0 warnings
> 
> Would someone help me out here? How do I use it to parse a command line
> for options, option values and any bare arguments? I assume I have to
> build a list of some kind of option structures with the short and long
> option codes, default values, etc.
> 
> I've looked in Graham's books and in Winston and Horn, and I've googled
> all over the place without turning up anymore than I have above. Does
> someone have a simple piece of code that runs a simple test case. I think
> it would help a great deal.


                        Use the source, Luke!


My bet is that the source of the GETOPT-TESTS would give a couple of
examples of how to use GETOPT...

   (ed "/usr/local/asdf-install/site/getopt-1.1.0/tests.lisp")


With a sane and correclty configured implementation, you should be
able to see the source of a function named GETOPT with: 

   (ED 'GETOPT)

Otherwise use:

   (ed "/usr/local/asdf-install/site/getopt-1.1.0/main.lisp") ; the source!


Also, you can get the documentation with:

   (documentation 'getopt 'function)


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Cats meow out of angst
"Thumbs! If only we had thumbs!
We could break so much!"
From: Jeff
Subject: Re: Example using getopts module?
Date: 
Message-ID: <pan.2005.01.12.02.41.13.990778@cunningham.net>
On Tue, 11 Jan 2005 16:18:13 +0100, Pascal Bourguignon wrote:

> My bet is that the source of the GETOPT-TESTS would give a couple of
> examples of how to use GETOPT...
> 
>    (ed "/usr/local/asdf-install/site/getopt-1.1.0/tests.lisp")

That was one of the first places I looked. Unfortunately, I don't know
enough lisp to figure out from the tests.lisp file:

(in-package cl)
(defpackage getopt-tests
  (:use #:getopt #:cl #:ptester))
(in-package getopt-tests)

(defmacro test-mv (values form)
  `(test ,values ,form :multiple-values t :test #'equal))

(defun do-tests ()
  (with-tests (:name "GETOPT")
    (let ((*break-on-test-failures* nil))
      
      ;; match-unique-abbreviation
      (test nil (match-unique-abbreviation "abc" nil)) (test nil
      (match-unique-abbreviation "abc" '("ab"))) (test 0
      (match-unique-abbreviation "ab" '("ab"))) (test 0
      (match-unique-abbreviation "a" '("ab"))) (test nil
      (match-unique-abbreviation "b" '("ab"))) (test nil
      (match-unique-abbreviation "ab" '("ab" "abc"))) (test 1
      (match-unique-abbreviation "ac" '("ab" "ac"))) (test 1
      (match-unique-abbreviation "ac" '("ab" "acb")))
      
      ;; getopt
      (test-mv '(("argv") nil nil) (getopt '("argv") nil)) (test-mv
      '(("argv" "2") nil nil) (getopt '("argv" "2") nil))
      
      (test-mv '(("argv") (("c")) nil) (getopt '("argv" "-c") '(("c"
      :none))))
      
      (test-mv '(("argv") (("c" . "val")) nil)
	       (getopt '("argv" "-c" "val") '(("c" :optional))))
      (test-mv '(("argv" "v1") (("c" . "val")) nil)
	       (getopt '("argv" "-c" "val" "v1") '(("c" :optional))))
      (test-mv '(( "v1") (("colon" . "val")) nil)
	       (getopt '("--colon" "val" "v1") '(("colon" :optional))))
      (test-mv '(("ab" "-c") (("colon" . "val")) nil)
	       (getopt '("ab" "--colon" "val" "--" "-c")
		       '(("colon" :optional) ("-c" :none))))
      (test-mv '(("argv") (("c" . "cd")) nil)
	       (getopt '("argv" "-c" "cd") '(("c" :required))))
      (test-mv '(("argv") nil ("c"))
	       (getopt '("argv" "-c") '(("c" :required))))
      (test-mv '(("argv") (("c" . "10")) nil)
	       (getopt '("argv" "-c=10") '(("c" :required))))
      (test-mv '(("argv") nil ("c"))
	       (getopt '("argv" "-c=10") '(("c" :none))))
      (test-mv '(nil (("along" . "10")) nil)
	       (getopt '("--along=10") '(("along" :optional))))
      (test-mv '(nil nil ("along"))
	       (getopt '("--along=10") '(("along" :none))))
      (test-mv '(nil (("along" . "10")) nil)
	       (getopt '("--a=10") '(("along" :optional))))
      (test-mv '(nil nil ("a"))
	       (getopt '("--a=10") '(("along" :optional) ("aboot" :optional))))
      (test-mv '(("a") nil nil)
               (getopt '("a") '(("a" :none))))
      ))
  t)



> With a sane and correclty configured implementation, you should be able
> to see the source of a function named GETOPT with:
> 
>    (ED 'GETOPT)

There - I think - is a crux of the problem. I assume the implementation is
sane but that my installation of it is not. I am trying to test this with
clisp, but have also tried it with sbcl and cmucl as well. When I try to
refer to getopt as above (or any other way) it doesn't recognize the
symbol unless I'm in the source directory itself.


> Also, you can get the documentation with:
> 
>    (documentation 'getopt 'function)

That would be really cool, but again, it doesn't work as I have things. 

Thanks for any help.

-Jeff
From: Pascal Bourguignon
Subject: Re: Example using getopts module?
Date: 
Message-ID: <87mzvfe3x2.fsf@thalassa.informatimago.com>
Jeff <·······@cunningham.net> writes:

> On Tue, 11 Jan 2005 16:18:13 +0100, Pascal Bourguignon wrote:
> 
> > My bet is that the source of the GETOPT-TESTS would give a couple of
> > examples of how to use GETOPT...
> > 
> >    (ed "/usr/local/asdf-install/site/getopt-1.1.0/tests.lisp")
> > [...]
quite a number of examples:
>
>	       (getopt '("argv" "-c" "cd") '(("c" :required))))
>	       (getopt '("argv" "-c") '(("c" :required))))
>	       (getopt '("argv" "-c=10") '(("c" :required))))
>	       (getopt '("argv" "-c=10") '(("c" :none))))
>	       (getopt '("--along=10") '(("along" :optional))))
>	       (getopt '("--along=10") '(("along" :none))))
>	       (getopt '("--a=10") '(("along" :optional))))
>	       (getopt '("--a=10") '(("along" :optional) ("aboot" :optional))))
>               (getopt '("a") '(("a" :none))))
> [...]
> > Also, you can get the documentation with:
> > 
> >    (documentation 'getopt 'function)
> 
> That would be really cool, but again, it doesn't work as I have things. 

Well then, if the examples above are not enough, check the
documentation string in the source of getopt:

      (ed "/usr/local/asdf-install/site/getopt-1.1.0/main.lisp")

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
From: Jeff
Subject: Re: Example using getopts module?
Date: 
Message-ID: <pan.2005.01.12.04.04.42.426621@cunningham.net>
> Well then, if the examples above are not enough, check the
> documentation string in the source of getopt:
> 
>       (ed "/usr/local/asdf-install/site/getopt-1.1.0/main.lisp")

Okay, I'll chew on this awhile and see how far I get. I'm much obliged for
the help.

-Jeff