From: Richard Smith
Subject: reading/feeding cmd line args into prg
Date: 
Message-ID: <m2d5hrruyu.fsf@82-35-122-1.cable.ubr01.enfi.blueyonder.co.uk>
Hi all

Nervous first post!  I have used lisp before, building up functions in
the toploop and supplying data as lists.

Now trying to read in command-line arguments to a Lisp program/

Was trying to write this trivial prg to find roots of a quadratic
equation -- and got this far...


(when (not (= 3 (length EXT:*ARGS*)))
  (format t "usage: clisp quad_... <a> <b> <c>  (a,b,c numbers)")
  (exit 1))

(setf
 a (car EXT:*ARGS*)
 b (cadr EXT:*ARGS*)
 c (caddr EXT:*ARGS*))

(format t "a=~A, b=~A, c=~A~%" a b c)

(setf discrt (- (expt b 2) (* 4 a c)))

;* crashes above
; because a, b and c are strings.  Don't know how to type-convert


Yup I know there's a lot of setf's which you tend to avoid in lisp,
and the code isn't very lisp-like, but this is a quick and
beauty-not-essential number-crunch

Point is, what can I do about cmdline args coming in as strings?
Especially in this case where I want to read them as numbers?

Richard Smith

From: Zach Beane
Subject: Re: reading/feeding cmd line args into prg
Date: 
Message-ID: <m3fymn6rs6.fsf@unnamed.xach.com>
Richard Smith <······@weldsmith4.co.uk> writes:

> Point is, what can I do about cmdline args coming in as strings?
> Especially in this case where I want to read them as numbers?

PARSE-INTEGER converts a string to a number.

http://www.lispworks.com/documentation/HyperSpec/Body/f_parse_.htm

Zach
From: Edi Weitz
Subject: Re: reading/feeding cmd line args into prg
Date: 
Message-ID: <uzmkvm85d.fsf@agharta.de>
On Mon, 13 Feb 2006 18:15:14 GMT, Richard Smith <······@weldsmith4.co.uk> wrote:

> Nervous first post!

Keep cool - we very rarely eat polite newbies... :)

> I have used lisp before, building up functions in the toploop and
> supplying data as lists.
>
> Now trying to read in command-line arguments to a Lisp program/
>
> Was trying to write this trivial prg to find roots of a quadratic
> equation -- and got this far...
>
> (when (not (= 3 (length EXT:*ARGS*)))
>   (format t "usage: clisp quad_... <a> <b> <c>  (a,b,c numbers)")
>   (exit 1))
>
> (setf
>  a (car EXT:*ARGS*)
>  b (cadr EXT:*ARGS*)
>  c (caddr EXT:*ARGS*))
>
> (format t "a=~A, b=~A, c=~A~%" a b c)
>
> (setf discrt (- (expt b 2) (* 4 a c)))
>
> ;* crashes above
> ; because a, b and c are strings.  Don't know how to type-convert
>
> Yup I know there's a lot of setf's which you tend to avoid in lisp,
> and the code isn't very lisp-like, but this is a quick and
> beauty-not-essential number-crunch

Hmm, OK.

> Point is, what can I do about cmdline args coming in as strings?
> Especially in this case where I want to read them as numbers?

Check out these (but make sure you understand the potential security
consequences):

  <http://www.lispworks.com/documentation/HyperSpec/Body/f_rd_fro.htm>
  <http://www.lispworks.com/documentation/HyperSpec/Body/f_parse_.htm>
  <http://www.cliki.net/PARSE-NUMBER>
  <http://www-cgi.cs.cmu.edu/afs/cs/project/ai-repository/ai/lang/lisp/code/math/atof/0.html>

HTH,
Edi.

-- 

European Common Lisp Meeting 2006: <http://weitz.de/eclm2006/>

Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: Pascal Bourguignon
Subject: Re: reading/feeding cmd line args into prg
Date: 
Message-ID: <87k6bzyuo9.fsf@thalassa.informatimago.com>
Richard Smith <······@weldsmith4.co.uk> writes:

> Hi all
>
> Nervous first post!  I have used lisp before, building up functions in
> the toploop and supplying data as lists.
>
> Now trying to read in command-line arguments to a Lisp program/
>
> Was trying to write this trivial prg to find roots of a quadratic
> equation -- and got this far...
>
>
> (when (not (= 3 (length EXT:*ARGS*)))
>   (format t "usage: clisp quad_... <a> <b> <c>  (a,b,c numbers)")
>   (exit 1))
>
> (setf
>  a (car EXT:*ARGS*)
>  b (cadr EXT:*ARGS*)
>  c (caddr EXT:*ARGS*))

(defun read-a-number-from-string (str)
  (let* ((*read-eval* nil)
         (n (read-from-string str)))
    (assert (numberp n) (n) "Argument should parse as a number, not ~A" str)))

  (setf a (read-from-string (first  EXT:*ARGS*))
        b (read-from-string (second EXT:*ARGS*))
        c (read-from-string (third  EXT:*ARGS*)))

(assert (numberp a) (numberp b) (numberp c)) (a 

> (format t "a=~A, b=~A, c=~A~%" a b c)
>
> (setf discrt (- (expt b 2) (* 4 a c)))
>
> ;* crashes above
> ; because a, b and c are strings.  Don't know how to type-convert
>
>
> Yup I know there's a lot of setf's which you tend to avoid in lisp,
> and the code isn't very lisp-like, but this is a quick and
> beauty-not-essential number-crunch
>
> Point is, what can I do about cmdline args coming in as strings?
> Especially in this case where I want to read them as numbers?
>
> Richard Smith

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

WARNING: This product attracts every other piece of matter in the
universe, including the products of other manufacturers, with a
force proportional to the product of the masses and inversely
proportional to the distance between them.
From: Pascal Bourguignon
Subject: Re: reading/feeding cmd line args into prg
Date: 
Message-ID: <87bqxbyued.fsf@thalassa.informatimago.com>
Richard Smith <······@weldsmith4.co.uk> writes:
> (when (not (= 3 (length EXT:*ARGS*)))
>   (format t "usage: clisp quad_... <a> <b> <c>  (a,b,c numbers)")
>   (exit 1))
>
> (setf
>  a (car EXT:*ARGS*)
>  b (cadr EXT:*ARGS*)
>  c (caddr EXT:*ARGS*))
> [...]
> Point is, what can I do about cmdline args coming in as strings?
> Especially in this case where I want to read them as numbers?

(defun read-a-number-from-string (str)
  (let* ((*read-eval* nil)
         (n (read-from-string str)))
    (unless (numberp n)
        (format *error-output* "~&Argument should parse as a number, not ~A~%"
                                str)
        (ext:exit 1))
    n))

(setf a (read-a-number-from-string (first  EXT:*ARGS*)) 
      b (read-a-number-from-string (second EXT:*ARGS*))
      c (read-a-number-from-string (third  EXT:*ARGS*)))

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

WARNING: This product attracts every other piece of matter in the
universe, including the products of other manufacturers, with a
force proportional to the product of the masses and inversely
proportional to the distance between them.
From: sross
Subject: Re: reading/feeding cmd line args into prg
Date: 
Message-ID: <1139864992.490208.180960@o13g2000cwo.googlegroups.com>
or for a safer and more lenient option (regarding *read-eval* and
the ability to parse integers and floating point numbers) take a look
at parse-number <http://www.cliki.net/parse-number>
or cl-l10n <http://www.cliki.net/cl-l10n> (which uses the parse-number
code).