Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I simplify the following command line options parsing code?
    primarykey
    data
    text
    <p>I am working on a homework manager for personal use, developing it with Haskell. I have, as a start, made the following code for parsing the commandline arguments, code which i should be easily extandable later through adding to <code>dispatch</code>. The functions themselves are dummies for testing purposes. I do, however, feel that this code can be made simpler, and more failsafe, though i cannot see how. The code is this:</p> <pre><code>module Main where import System.Environment main = getArgs &gt;&gt;= parse parse :: [String] -&gt; IO () parse [] = usage parse (file:mode:[]) = if mode == "list" then list file else case lookup mode dispatch of Just _ -&gt; putStrLn "That mode requires arguments" &gt;&gt; usage Nothing -&gt; putStrLn "Please supply a valid mode" &gt;&gt; usage parse (file:mode:args) = case lookup mode dispatch of Nothing -&gt; do case lookup file dispatch of Just _ -&gt; putStrLn "Please supply a data file" Nothing -&gt; putStrLn "Please supply a valid mode" usage Just fun -&gt; fun file args parse (file:[]) = case lookup file dispatch of Just _ -&gt; putStrLn "Please supply a datafile" &gt;&gt; usage Nothing -&gt; list file usage :: IO () usage = mapM_ putStrLn helptext helptext :: [String] helptext = ["Homework manager by Marcus Medom Ryding &lt;mrok4a@gmail.com&gt;" ,"Copyright 2013, licensed under BSD3" ,"Usage: homework filepath [mode] [arguments]"] dispatch :: [(String, String -&gt; [String] -&gt; IO ())] dispatch = [("new",new)] new :: String -&gt; [String] -&gt; IO () new file args = putStrLn ("NEW: " ++ file) &gt;&gt; mapM_ putStrLn args list :: String -&gt; IO () list file = putStrLn ("LIST: " ++ file) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload