all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Tim X <timx@nospam.dev.null>
To: help-gnu-emacs@gnu.org
Subject: Re: Checking parameters
Date: Sun, 17 Jun 2007 23:18:54 +1000	[thread overview]
Message-ID: <87sl8qsoqp.fsf@lion.rapttech.com.au> (raw)
In-Reply-To: 46750a24$0$338$e4fe514c@news.xs4all.nl

Cecil Westerhof <dummy@dummy.nl> writes:

> I am new to Emacs and lisp.
> What is the best way to check parameters and do error handling when they are
> not correct?
>
> I have the following function:
> (defun getHours(time)
>   (interactive "sHH:MM: ")
>   (let ((timelist (split-string time ":")))
>     (+
>       (string-to-number (car timelist))
>       (/
>         (string-to-number (cadr timelist))
>         60.0
>       )
>     )
>   )
> )
>
> How do I check that there is exactly one parameter? And how do I check the
> format and give an error that works in interactive and normal mode?
> For example when I give
>     (getHours "0:120")
> I get
>     2.0
> How should I generate an error/exception?
>

Emacs will throw an error if the function is called with the wrong number of
arguments. 

There is already a reply that shows a simple way to check for the correct
string format using a regexp. However, if you want to get a bit more
adventurous and you would like a function that can tell the user the arguement
is invalid and give them another opportunity to enter a correctly formatted
argument, you might want to use a function instead of the prompt sring. This
function can then prompt the user for input, check that it is valid and if it
is, just returns the values as a list (which may only have one element if thats
all that is required). If the value the user entered was not a valid format,
the function can print an error message, possibly explaining why the users
input was no good and then prompt again to give the user another go. 

While this may take more work, it can be nicer for the end user. It can
sometimes also make your code look a bit cleaner as you can put the arguement
error checking off in its own function, rather than have it cluttering up the
main body of your function. 

for example (untested)

(defun time-prompt ()
  "Prompt for a valid time string HH:MM."
  (let (prompt-str)
    (setq prompt-str (read-from-minibuffer "Time HH:MM: "))
    (while (not (string-match "^[0-9]+:[0-5][0-9]$" prompt-str))
      (message "Bad time format. Must be HH:MM")
      (setq prompt-str (read-from-minibuffer "Time HH:MM: ")))
    (list prompt-str)))

(defun getHours(time)
  (interactive time-prompt)
  ....

tim

-- 
tcross (at) rapttech dot com dot au

  parent reply	other threads:[~2007-06-17 13:18 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-06-17 10:17 Checking parameters Cecil Westerhof
2007-06-17 10:55 ` Lennart Borgman (gmail)
2007-06-17 11:18   ` Lennart Borgman (gmail)
2007-06-17 15:28   ` Nikolaj Schumacher
     [not found]   ` <mailman.2283.1182079099.32220.help-gnu-emacs@gnu.org>
2007-06-17 18:01     ` Cecil Westerhof
2007-06-18 17:27     ` don provan
2007-06-17 13:18 ` Tim X [this message]
2007-06-17 18:08   ` Cecil Westerhof

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87sl8qsoqp.fsf@lion.rapttech.com.au \
    --to=timx@nospam.dev.null \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.