Capstone A: the command line
A subcommand and a usage line
The program of the previous lesson becomes a small tool: the first argument is a subcommand, and anything else prints a usage line on standard error and exits with status 2. The reading is the same, so the only new code is the dispatch at the end.
args <- getArgs
case args of
["total"] -> print total
["count"] -> print (length rows)
_ -> do
hPutStrLn stderr "usage: sales total | sales count"
exitWith (ExitFailure 2)
The two subcommands are the two numbers the report has always printed, and the usage line names both. A script that runs this tool can check the status of the failure case, which is the convention the arguments lesson set.
The program
module Main (main) where
import Data.Maybe (mapMaybe)
import System.Environment (getArgs)
import System.Exit (ExitCode (ExitFailure), exitWith)
import System.IO (hPutStrLn, stderr)
csv :: String
csv = unlines ["tea,3,250", "cake,1,400", "tea,2,250"]
parseLine :: String -> Maybe (String, Int, Int)
parseLine line =
case break (== ',') line of
(item, ',' : rest) ->
case break (== ',') rest of
(qty, ',' : pence) ->
case (reads qty, reads pence) of
([(q, "")], [(p, "")]) -> Just (item, q, p)
_ -> Nothing
_ -> Nothing
_ -> Nothing
main :: IO ()
main = do
writeFile "/tmp/sales-cli.csv" csv
text <- readFile "/tmp/sales-cli.csv"
let rows = mapMaybe parseLine (lines text)
total = sum [qty * pence | (_, qty, pence) <- rows]
args <- getArgs
case args of
["total"] -> print total
["count"] -> print (length rows)
_ -> do
hPutStrLn stderr "usage: sales total | sales count"
exitWith (ExitFailure 2)
ghc -Wall -O1 -o sales cli.hs
./sales total
./sales count
./sales
echo "status: $?"
1650
3
usage: sales total | sales count
status: 2
The status is part of the interface
- 0 for a subcommand that ran, 2 for a wrong invocation, which is the convention of the arguments lesson.
- The usage goes to standard error, so a script that captures the total does not find it in the output.
- A third subcommand would be one more case; the parser and the reader stay shared.
The next lesson tests the parser with a table of good and bad lines and a runner that exits non-zero on the first failure.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does the program do with no arguments?
Answer
It prints a usage line on stderr and exits with status 2
The fall-through case is the usage and the non-zero status.
-
Why does the usage go to standard error?
Answer
So a script that captures the output does not see it
The standard output carries the data and stderr the diagnostics.
-
What is the status of the usage case?
Answer
2
The lesson follows the convention of 2 for a usage error.
-
What does this program print?
module Main (main) where import System.Environment (getArgs) main :: IO () main = do args <- getArgs print (length args)Run as./tool total.Answer
1
One argument was passed and the program name is not counted.
-
What does the count subcommand print?
Answer
The number of parsed rows
length rows counts the rows the parser accepted.
-
What does this program print?
module Main (main) where main :: IO () main = print (sum [1, 2, 3 :: Int])Answer
6
The three numbers add to 6.
-
Why does the total not change from the previous lesson?
Answer
The reading and the arithmetic are the same code
The lesson only adds a dispatch around the existing program.
-
What does this program print?
module Main (main) where main :: IO () main = putStrLn "usage"Answer
usage
putStrLn prints the string without quotes.
-
What would a third subcommand need?
Answer
One more case in the dispatch and a word in the usage line
The reader and the arithmetic are shared, so a subcommand is one case.
-
What does this program print?
module Main (main) where main :: IO () main = print (null [])Answer
True
The empty list is null.
The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev