Personal tools

Checking for correct invocation of a command line haskell program

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
Current revision (21:23, 15 June 2007) (edit) (undo)
m (Category)
 
(10 intermediate revisions not shown.)
Line 1: Line 1:
-
Running our example:
+
[[Category:How to]]
 +
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==
 +
 +
<code>
 +
runghc arghandling-nice.hs 0 1 2
 +
"0 1 2"
-
thartman@linodehaskell:~/learning/haskell/UnixTools/arghandling>./args.sh
+
runghc arghandling-ugly.hs 0 1 2
-
Checking for correct invocation of a command line / shell haskell program
+
"0 1 2"
-
We run a nice example,
 
-
alongside example of "ugly" argument checking that is simpler to write,
+
runghc arghandling-nice.hs 0 1
-
but gives a less user friendly error message when the program is invoked incorrectly
+
*** 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)
 +
</code>
-
**********
+
==The code==
-
nice arg handling, no errors:
+
-
"0 1 2"
+
 +
arghandling-nice.hs:
 +
<haskell>
 +
import System
-
ugly arg handling, no errors:
+
main = do args <- getArgs
-
"0 1 2"
+
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]
 +
</haskell>
-
nice arg handling, errors:
+
arghandling-ugly.hs
-
*** Exception: args length does not equal 3. args: : ["0","1"]
+
<haskell>
-
usage example: $ runghc arghandling-nice.hs firstarg secondarg thirdarg
+
import System
 +
main = do [first,second,third] <- getArgs
 +
process first second third
-
ugly arg handling, errors:
+
process a b c = print $ unwords [a,b,c]
-
*** Exception: user error (Pattern match failure in do expression at arghandling-ugly.hs:3:10-29)
+
</haskell>
-
Source code...
+
==See also:==
-
args.sh:
+
* [[Simple unix tools]]
-
 
+
* [[Error reporting strategies]]
-
 
+
* [http://leiffrenzel.de/papers/commandline-options-in-haskell.html Command line options in haskell]
-
thartman@linodehaskell:~/learning/haskell/UnixTools/arghandling>cat args.sh
+
* [http://groups.google.de/group/fa.haskell/browse_thread/thread/6359767a8e64bc5d/e5286a2783c22bbe?lnk=st&q=haskell+string+fill+ins&rnum=1&hl=en#e5286a2783c22bbe string fill-ins (discussion at haskell cafe)]
-
cat <<EOF
+
-
Checking for correct invocation of a command line / shell haskell program
+
-
 
+
-
We run a nice example,
+
-
 
+
-
alongside example of "ugly" argument checking that is simpler to write,
+
-
but gives a less user friendly error message when the program is invoked incorrectly
+
-
 
+
-
 
+
-
**********
+
-
EOF
+
-
 
+
-
echo nice arg handling, no errors:
+
-
runghc arghandling-nice.hs 0 1 2
+
-
echo
+
-
echo
+
-
 
+
-
echo ugly arg handling, no errors:
+
-
runghc arghandling-ugly.hs 0 1 2
+
-
echo
+
-
echo
+
-
 
+
-
 
+
-
echo nice arg handling, errors:
+
-
runghc arghandling-nice.hs 0 1
+
-
echo
+
-
echo
+
-
 
+
-
echo ugly arg handling, errors:
+
-
runghc arghandling-ugly.hs 0 1
+
-
 
+
-
 
+
-
 
+
-
thartman@linodehaskell:~/learning/haskell/UnixTools/arghandling>cat 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]
+
-
 
+
-
 
+
-
 
+
-
thartman@linodehaskell:~/learning/haskell/UnixTools/arghandling>cat arghandling-ugly.hs
+
-
import System
+
-
 
+
-
main = do [first,second,third] <- getArgs
+
-
process first second third
+
-
 
+
-
process a b c = print $ unwords [a,b,c]
+

Current revision

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

1 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)

2 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]

3 See also: