simpleargs: Provides a more flexible getArgs function with better error reporting.

[ library, system ] [ Propose Tags ]

The provided getArgs returns an arbitrary tuple of values instead of a list of Strings. This means that the number and type (i.e. parseability) of parameters are checked, and reported to the user. The module is not a substitute for proper error handling (use System.Console.GetOpt for that), but is useful for making quick and dirty command line tools a bit less dirty, without sacrificing the quick part.


[Skip to Readme]

Modules

[Index]

Downloads

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1, 0.2.1
Dependencies base (>=3 && <4.10) [details]
License LicenseRef-LGPL
Author Ketil Malde
Maintainer Ketil Malde <ketil@malde.org>
Revised Revision 1 made by AndreasAbel at 2021-10-23T08:40:01Z
Category System
Home page http://malde.org/~ketil/simpleargs
Uploaded by KetilMalde at 2010-09-10T10:25:47Z
Distributions
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 1945 total (11 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for simpleargs-0.2.1

[back to package description]
SimpleArgs - provide a more flexible and informative replacement for getArgs 

For "real" command line programs, you usually want to provide a
flexible command line with various options and settings, sensibly
named and with auto-generated help.  In that case, SimpleArgs is not
for you, stop reading this, and look up System.Console.GetOpt¹
instead. 

But sometimes, a quick hack is just what you need.  Previously, you
were wont to do:

    main = do
        [count',gender'] <- getArgs
	let count = read count
	    gender = case gender' of 
	       	      	     "M" -> 'M'
			     "F" -> 'F'
	main_real count gender

This is somewhat tedious, wastes precious sceen estate, users
supplying parameters of the wrong type will get obscure errors, and
while any programming errors you might introduce probably will be
trivial, it would be better to avoid them entirely.

The SimpleArgs module provides getArgs with an overloaded return type,
so that command line parameters are parsed as the types required by
the rest of the program.

Using SimpleArgs, the above could therefore look like this:

    main = do 
        (count,gender) <- getArgs
        main_real count gender

or even (I think):

    main = getArgs >>= return . uncurry main_real

If that was a bit contrieved, let's say you just want to read a file
name:

     main = do
        [filename] <- getArgs
        readFile filename >>= print . length 

I'm sure you could avoid the information-free name "filename" by some
esoteric tranformation to more point-free style, but I argue that
SimpleArgs makes this natural and easy:

    main = getArgs >>= readFile >>= print . length

I don't think 'wc -c' gets much easier than this.  Here's a custom
data type (which is parsed using its Read instance, and also needs
Typeable):

    import System.SimpleArgs
    import Data.Typeable

    data Gender = Male | Female deriving (Typeable,Read)

    main = do
        g <- getArgs
        case g of Male -> putStrLn "X and Y"
                  Female -> putStrLn "two Xs"

Instead of reporting errors rather anonymously as incomplete cases or
read failures, SimpleArgs will provide more sensible error
reporting. (To try this, build Example by executing 'ghc --make
Example.hs').  It will: 

      1) report incorrect number of parameters,
      	 also mentioning the expected parameters and types:

	  % ./Example foo
	  Example: Incorrect number of arguments, got 1,
	  expected 2 (Int,[Char])

	This also gives you a useful hint if you just run the program
	without any parameters.

      2) report parameters that fail to parse as the required type:

          % ./Example foo 10
 	  Example: Couldn't parse parameter "foo" as type Int

Nice, huh?  Please enjoy, and let me know how you fare at ketil@malde.org.

¹ http://www.haskell.org/haskellwiki/High-level_option_handling_with_GetOpt