I don't think you're missing anything that's obvious.
In essence, this is a tricky little detail and it's difficult to
get all the small points right. I've seen code a bit like this in
many programs and several programmers, including myself on
occasion, have tried to do what you are doing, often afresh.
It could well be that we would be better off with handles in
-syntax-, but that's already a fairly complicated command.
I suspect your code would be broken by quote characters within
filenames, for example. There are also, I think, other
assumptions built in about the use of various characters within
filenames, so I wouldn't be sure that the code will not generate
error exits for the wrong reasons, especially if tested on
different platforms.
I applaud the principle of checking early for fatal errors, and my
name is on a document urging precisely that. In practice, I tend
to let users specify -saving()- as one of many options passed to a
graph command as a wildcard, so that only when the graph command
is encountered will that be tested. That's gross laziness if you
like, but then a user could make many errors in principle within
such a set, and it's quite impracticable to try to catch them all.
It's no accident that -saving()- is given as a "passthru" example
for -syntax-.
Also, in practice, I suspect that inexperienced users more likely
to make a mistake in specifying a filename would often in practice
be doing this by a dialog, so that getting the -saving()- option
exactly right is not in that case the issue.
There is a fine line between neurotic programming which worries a
fair amount about the most obvious errors users might make, and
paranoid programming which, if carried to excess, can make a
program a long series of traps for possible errors followed by a
small segment that does the real work. One rule of thumb is to
trap the errors you yourself make most commonly when developing a
program....
Nick
[email protected]
Allan Reese
>
> Am I missing some feature of the syntax command, or is this function
> provided by a general routine I should call? I'm writing a
> program that
> should provide the standard option to save a .gph graph
> file. The program
> should not run to completion then fail on a technicality,
> so I use the
> following code to validate the option - but can it be more
> specific than
> a "string". This code is either not needed, or may be of
> use to other
> program writers:
>
> syntax ... [ saving(string) ...];
> ...
> * Check saving details
> if "`saving'" != "" {
> tokenize "`saving'", parse(",")
> loc saving "`1'"
> if index("`saving'", ".")==0 loc saving "`saving'.gph"
> capture confirm file `saving'
> if _rc==0 { /* existing file */
> if "`3'" != "replace" {
> di "Invalid save file - did you mean to replace?"
> exit
> }
> loc saving "`saving',replace"
> }
> else { /* invalid file - may be new */
> capture confirm new file `1'
> if _rc!=0 {
> di "Invalid save filename"
> exit
> }
> }
> }
>
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/