* Checking parameters
@ 2007-06-17 10:17 Cecil Westerhof
2007-06-17 10:55 ` Lennart Borgman (gmail)
2007-06-17 13:18 ` Tim X
0 siblings, 2 replies; 8+ messages in thread
From: Cecil Westerhof @ 2007-06-17 10:17 UTC (permalink / raw)
To: help-gnu-emacs
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?
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Checking parameters
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)
` (2 more replies)
2007-06-17 13:18 ` Tim X
1 sibling, 3 replies; 8+ messages in thread
From: Lennart Borgman (gmail) @ 2007-06-17 10:55 UTC (permalink / raw)
Cc: help-gnu-emacs
Cecil Westerhof wrote:
> 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?
You probably want to use the most simple way, something like
(defun get-hours(time)
(interactive "sHH:MM: ")
(let ((timelist (split-string time ":"))
(errmsg "Time format error"))
(unless (= (length (nth 1 timelist)) 2)
(error errmsg))
(+
(string-to-number (nth 0 timelist))
(/
(string-to-number (nth 1 timelist))
60.0))))
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Checking parameters
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>
2 siblings, 0 replies; 8+ messages in thread
From: Lennart Borgman (gmail) @ 2007-06-17 11:18 UTC (permalink / raw)
To: help-gnu-emacs
Lennart Borgman (gmail) wrote:
> You probably want to use the most simple way, something like
>
> (defun get-hours(time)
> (interactive "sHH:MM: ")
> (let ((timelist (split-string time ":"))
> (errmsg "Time format error"))
> (unless (= (length (nth 1 timelist)) 2)
> (error errmsg))
> (+
> (string-to-number (nth 0 timelist))
> (/
> (string-to-number (nth 1 timelist))
> 60.0))))
And this is better:
(defun get-hours(time)
(interactive "sHH:MM: ")
(let ((timelist (split-string time ":"))
(errmsg "Time format error"))
(save-match-data
(unless (string-match "\\`[0-9]+:[0-9]\\{2\\}\\'" time)
(error errmsg)))
(+
(string-to-number (nth 0 timelist))
(/
(string-to-number (nth 1 timelist))
60.0))))
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Checking parameters
2007-06-17 10:17 Checking parameters Cecil Westerhof
2007-06-17 10:55 ` Lennart Borgman (gmail)
@ 2007-06-17 13:18 ` Tim X
2007-06-17 18:08 ` Cecil Westerhof
1 sibling, 1 reply; 8+ messages in thread
From: Tim X @ 2007-06-17 13:18 UTC (permalink / raw)
To: help-gnu-emacs
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
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Checking parameters
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>
2 siblings, 0 replies; 8+ messages in thread
From: Nikolaj Schumacher @ 2007-06-17 15:28 UTC (permalink / raw)
To: help-gnu-emacs
"Lennart Borgman (gmail)" <lennart.borgman@gmail.com> wrote:
> Cecil Westerhof wrote:
>> I am new to Emacs and lisp.
>> What is the best way to check parameters and do error handling when they are
>> not correct?
>
> You probably want to use the most simple way, something like
> (error errmsg))
You should also add the error message to `debug-ignored-errors' if you
use the debugger. Otherwise you start debugging every time you input
malformed data.
regards,
Nikolaj Schumacher
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Checking parameters
[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
1 sibling, 0 replies; 8+ messages in thread
From: Cecil Westerhof @ 2007-06-17 18:01 UTC (permalink / raw)
To: help-gnu-emacs
Lennart Borgman (gmail) wrote:
> (defunregulars(time)
> (interactive "sHH:MM: ")
> (let ((timelist (split-string time ":"))
> (errmsg "Time format error"))
> (save-match-data
> (unless (string-match "\\`[0-9]+:[0-9]\\{2\\}\\'" time)
> (error errmsg)))
> (+
> (string-to-number (nth 0 timelist))
> (/
> (string-to-number (nth 1 timelist))
> 60.0))))
Works like a charm. I only changed the regular expression to
"\\`[0-9]\\{1,2\\}:[0-9]\\{2\\}\\'"
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Checking parameters
2007-06-17 13:18 ` Tim X
@ 2007-06-17 18:08 ` Cecil Westerhof
0 siblings, 0 replies; 8+ messages in thread
From: Cecil Westerhof @ 2007-06-17 18:08 UTC (permalink / raw)
To: help-gnu-emacs
Tim X wrote:
>> I am new to Emacs and lisp.
>> What is the best way to check parameters and do error handling when they
>> are not correct?
>
> Emacs will throw an error if the function is called with the wrong number
> of arguments.
Okay, I do not have to take care of that. ;-}
> 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.
Not at the moment, I am just starting. Furthermore, the function shall
mostly be used non-interactively.
But thanks for the thought.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Checking parameters
[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
1 sibling, 0 replies; 8+ messages in thread
From: don provan @ 2007-06-18 17:27 UTC (permalink / raw)
To: help-gnu-emacs
"Lennart Borgman (gmail)" <lennart.borgman@gmail.com> writes:
> And this is better:
>
> (defun get-hours(time)
> (interactive "sHH:MM: ")
> (let ((timelist (split-string time ":"))
> (errmsg "Time format error"))
> (save-match-data
> (unless (string-match "\\`[0-9]+:[0-9]\\{2\\}\\'" time)
> (error errmsg)))
> (+
> (string-to-number (nth 0 timelist))
> (/
> (string-to-number (nth 1 timelist))
> 60.0))))
Go all the way:
(defun get-hours(time)
(interactive "sHH:MM: ")
(save-match-data
(unless (string-match "\\`\\([0-9]+\\):\\([0-9]\\{2\\}\\)\\'" time)
(error "Time format error"))
(+
(string-to-number (match-string 1 time))
(/
(string-to-number (match-string 2 time))
60.0))))
-don
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2007-06-18 17:27 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2007-06-17 18:08 ` Cecil Westerhof
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).