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 { full_stories :: Bool,
46 deriving (Show, Data, Typeable)
50 description = "Convert LWN articles to EPUB format."
52 program_name :: String
53 program_name = "lwn-epub"
55 lwn_epub_summary :: String
57 program_name ++ "-" ++ (showVersion version)
60 output_help = "Output file, defaults to stdout"
62 full_stories_help :: String
63 full_stories_help = "Replace \"Full Story\" links with their content"
65 arg_spec :: Mode (CmdArgs Args)
66 arg_spec = cmdArgsMode $
68 full_stories = def &= help full_stories_help,
69 output = def &= typFile &= help output_help,
70 article = def &= argPos 0 &= typ "ARTICLE"
72 &= program program_name
73 &= summary lwn_epub_summary
74 &= details [description]
77 -- Infix notation won't work, the arguments are backwards!
78 is_missing_arg_error :: String -> Bool
79 is_missing_arg_error s =
80 startswith "Requires at least" s
83 show_help :: IO (CmdArgs Args)
84 show_help = withArgs ["--help"] parse_args
86 parse_args :: IO (CmdArgs Args)
89 let y = process arg_spec x
91 Right result -> return result
93 if (is_missing_arg_error err) then
94 -- Disregard the error message, show help instead.
98 exitWith (ExitFailure exit_args_parse_failed)
101 -- | Really get the command-line arguments. This calls 'parse_args'
102 -- first to replace the default "wrong number of arguments" error,
103 -- and then runs 'cmdArgsApply' on the result to do what the
104 -- 'cmdArgs' function usually does.
105 apply_args :: IO Args
107 parse_args >>= cmdArgsApply