Difference between revisions of "Checking for correct invocation of a command line haskell program"

From HaskellWiki
Jump to navigation Jump to search
(Reformat)
Line 1: Line 1:
  +
This page shows checking for correct invocation of a command line / shell haskell program. The first example is a version which gives a "user friendly" error message when invoked incorrectly, the second is an example of "ugly" argument checking that is slightly simpler to write, but gives a less user friendly error message
thartman@linodehaskell:~/learning/haskell/UnixTools/arghandling>./args.sh
 
This demonstrates checking for correct invocation of a command line / shell haskell program
 
It runs a version which gives a "user friendly" error message when invoked incorrectly,
 
alongside an example of "ugly" argument checking that is slightly simpler to write,
 
but gives a less user friendly error message
 
   
  +
==Usage examples==
**********
 
  +
runghc arghandling-nice.hs 0 1 2
 
  +
<code>
"0 1 2"
 
 
runghc arghandling-nice.hs 0 1 2
 
"0 1 2"
   
 
runghc arghandling-ugly.hs 0 1 2
 
"0 1 2"
   
runghc arghandling-ugly.hs 0 1 2
 
"0 1 2"
 
   
  +
runghc arghandling-nice.hs 0 1
  +
*** Exception: args length does not equal 3. args: : ["0","1"]
 
usage example: $ runghc arghandling-nice.hs firstarg secondarg thirdarg
   
runghc arghandling-nice.hs 0 1
+
runghc arghandling-ugly.hs 0 1
*** Exception: args length does not equal 3. args: : ["0","1"]
+
*** Exception: user error (Pattern match failure in do expression at arghandling-ugly.hs:3:10-29)
  +
</code>
usage example: $ runghc arghandling-nice.hs firstarg secondarg thirdarg
 
   
  +
==The code==
   
runghc arghandling-ugly.hs 0 1
+
arghandling-nice.hs:
  +
<haskell>
*** Exception: user error (Pattern match failure in do expression at arghandling-ugly.hs:3:10-29)
 
 
import System
   
 
main = do args <- getArgs
thartman@linodehaskell:~/learning/haskell/UnixTools/arghandling>cat arghandling-nice.hs
 
 
let usagemsg = "usage example: $ runghc arghandling-nice.hs firstarg secondarg thirdarg"
import System
 
 
case args of
 
[first,second,third] -> process first second third
 
_ -> error $ "args length does not equal 3. args: : " ++ ( show args ) ++ "\n" ++ usagemsg
   
 
process a b c = print $ unwords [a,b,c]
main = do args <- getArgs
 
  +
</haskell>
let usagemsg = "usage example: $ runghc arghandling-nice.hs firstarg secondarg thirdarg"
 
case args of
 
[first,second,third] -> process first second third
 
_ -> error $ "args length does not equal 3. args: : " ++ ( show args ) ++ "\n" ++ usagemsg
 
   
  +
arghandling-ugly.hs
process a b c = print $ unwords [a,b,c]
 
  +
<haskell>
 
import System
   
 
main = do [first,second,third] <- getArgs
thartman@linodehaskell:~/learning/haskell/UnixTools/arghandling>cat arghandling-ugly.hs
 
 
process first second third
import System
 
   
 
process a b c = print $ unwords [a,b,c]
main = do [first,second,third] <- getArgs
 
  +
</haskell>
process first second third
 
   
 
==See also:==
process a b c = print $ unwords [a,b,c]
 
 
 
See also:
 
   
 
* [[Simple unix tools]]
 
* [[Simple unix tools]]

Revision as of 16:44, 11 April 2007

This page shows checking for correct invocation of a command line / shell haskell program. The first example is a version which gives a "user friendly" error message when invoked incorrectly, the second is an example of "ugly" argument checking that is slightly simpler to write, but gives a less user friendly error message

Usage examples

runghc arghandling-nice.hs 0 1 2 "0 1 2"

runghc arghandling-ugly.hs 0 1 2 "0 1 2"


runghc arghandling-nice.hs 0 1

*** Exception: args length does not equal 3. args: : ["0","1"]

usage example: $ runghc arghandling-nice.hs firstarg secondarg thirdarg

runghc arghandling-ugly.hs 0 1

*** Exception: user error (Pattern match failure in do expression at arghandling-ugly.hs:3:10-29)

The code

arghandling-nice.hs:

import System

main = do args <- getArgs
          let usagemsg = "usage example: $ runghc arghandling-nice.hs firstarg secondarg thirdarg"
          case args of
            [first,second,third] -> process first second third
            _                    -> error $ "args length does not equal 3. args: : " ++ ( show args ) ++ "\n" ++ usagemsg

process a b c = print $ unwords [a,b,c]

arghandling-ugly.hs

import System

main = do [first,second,third] <- getArgs
          process first second third

process a b c = print $ unwords [a,b,c]

See also: