I think Fergus's efficiency argument may be a red herring.
Here is an excerpt from a compiler I wrote recently:
> data JvlArgs = JvlArgs {optNoLink :: Bool,
> optVerbose :: Bool,
> jvlClassNames :: [String]}
> deriving Show
>
> jvlArgs :: JvlArgs
> jvlArgs = getJvlArgs (unsafePerformIO getArgs)
> JvlArgs {optNoLink = False,
> optVerbose = False}
>
> getJvlArgs :: [String] -> JvlArgs -> JvlArgs
> getJvlArgs ("-c":ss) args = getJvlArgs ss (args {optNoLink = True})
> getJvlArgs ("-v":ss) args = getJvlArgs ss (args {optVerbose = True})
> getJvlArgs (s@('-':_):_) _ = error ("bad option: " ++ s)
> getJvlArgs ss args = args {jvlClassNames = map internalClassName ss}
Note that argv (= unsafePerformIO getArgs) is a constant (as is
jvlArgs), defaults are dealt with systematically, error handling
(not very extensive in this case) is done, and the arguments are
scanned only once (lazily, in fact).
Cheers,
--Joe
Fergus Henderson wrote:
| Simon L Peyton Jones wrote:
| >
| > I agree with Sigbjorn about argv, rather strongly, though apparently nobody
| > else does.
|
| No, I agree Sigbjorn's proposal is probably a good idea, although I don't
| feel strongly either way. (I was just disagreeing with the reasoning that
| he used to motivate it.)
|
| > module CmdLineOpts where
| >
| > argv = unsafePerformIO getArgs
| >
| > unfoldSize :: Int
| > unfoldSize = lookupInt "-funfold-size" argv
| >
| > useCleverFiniteMap :: Bool
| > useCleverFiniteMap = lookup "-fclever" argv
|
| I have a comment, and couple of questions.
|
| First, this will involve scanning argv once for each possible option;
| I guess option handling is not likely to be a bottleneck, but still...
| this offends some aesthetic sense of mine.
|
| Second, how do you handle syntax errors in the command line arguments?
| What does lookupInt do if the integer overflows, or if the argument
| is not valid syntax for an integer? Do you check for misspelt or
| invalid option names?
|
| > PS. I'm less steamed up about the stdin issue; but I think you missed
| > Sigbjorn's point. Yes stdin is a constant now, but he'd like stdin *not* to
| > be a constant, so that he could take a value of type IO () that used stdin,
| > and reconnect its stdin to (say) a file.
|
| Even if stdin remains a constant, you could still do that, because even
| if the handle is a constant, the connection between handle and file can
| still vary, just as the file contents can vary.
|
| --
| Fergus Henderson <[EMAIL PROTECTED]> | "I have always known that the pursuit
| WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
| PGP: finger [EMAIL PROTECTED] | -- the last words of T. S. Garp.
|
Joseph H. Fasel, Ph.D. email: [EMAIL PROTECTED]
Technology Modeling and Analysis phone: +1 505 667 7158
University of California fax: +1 505 667 2960
Los Alamos National Laboratory postal: TSA-7 MS F609
Los Alamos, NM 87545