1 {-# LANGUAGE DeriveDataTypeable #-}
10 -- Get the version from Cabal.
11 import Paths_lwn_epub (version)
12 import Data.Version (showVersion)
14 import Data.String.Utils (startswith)
15 import System.Console.CmdArgs (
33 import System.Console.CmdArgs.Explicit (process)
34 import System.Environment (getArgs, withArgs)
35 import System.Exit (ExitCode(..), exitWith)
36 import System.IO (hPutStrLn, stderr)
43 Args { output :: FilePath,
45 deriving (Show, Data, Typeable)
49 description = "Convert LWN articles to EPUB format."
51 program_name :: String
52 program_name = "lwn_epub"
54 lwn_epub_summary :: String
56 program_name ++ "-" ++ (showVersion version)
59 output_help = "Output file, defaults to stdout"
61 arg_spec :: Mode (CmdArgs Args)
62 arg_spec = cmdArgsMode $
64 output = def &= typFile &= help output_help,
65 article = def &= argPos 0 &= typ "ARTICLE"
67 &= program program_name
68 &= summary lwn_epub_summary
69 &= details [description]
72 -- Infix notation won't work, the arguments are backwards!
73 is_missing_arg_error :: String -> Bool
74 is_missing_arg_error s =
75 startswith "Requires at least" s
78 show_help :: IO (CmdArgs Args)
79 show_help = withArgs ["--help"] parse_args
81 parse_args :: IO (CmdArgs Args)
84 let y = process arg_spec x
86 Right result -> return result
88 if (is_missing_arg_error err) then
89 -- Disregard the error message, show help instead.
93 exitWith (ExitFailure exit_args_parse_failed)
96 -- | Really get the command-line arguments. This calls 'parse_args'
97 -- first to replace the default "wrong number of arguments" error,
98 -- and then runs 'cmdArgsApply' on the result to do what the
99 -- 'cmdArgs' function usually does.
100 apply_args :: IO Args
102 parse_args >>= cmdArgsApply