1 {-# LANGUAGE DeriveDataTypeable #-}
3 module CommandLine (Args(..), apply_args)
6 -- Get the version from Cabal.
7 import Paths_lwn_epub (version)
8 import Data.Version (showVersion)
10 import Data.String.Utils (startswith)
11 import System.Console.CmdArgs (
26 import System.Console.CmdArgs.Explicit (process)
27 import System.Environment (getArgs, withArgs)
28 import System.Exit (ExitCode(..), exitWith)
29 import System.IO (hPutStrLn, stderr)
36 Args { article :: String }
37 deriving (Show, Data, Typeable)
41 description = "Convert LWN articles to EPUB format."
43 program_name :: String
44 program_name = "lwn_epub"
46 lwn_epub_summary :: String
48 program_name ++ "-" ++ (showVersion version)
50 arg_spec :: Mode (CmdArgs Args)
51 arg_spec = cmdArgsMode $
52 Args { article = "" &= argPos 0 &= typ "ARTICLE" }
53 &= program program_name
54 &= summary lwn_epub_summary
55 &= details [description]
58 -- Infix notation won't work, the arguments are backwards!
59 is_missing_arg_error :: String -> Bool
60 is_missing_arg_error s =
61 startswith "Requires at least" s
63 parse_args :: IO (CmdArgs Args)
66 let y = process arg_spec x
68 Right result -> return result
70 if (is_missing_arg_error err) then
71 withArgs ["--help"] parse_args
74 exitWith (ExitFailure exit_args_parse_failed)
76 -- Disregard the error message, show help instead.