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 (
29 import System.Console.CmdArgs.Explicit (process)
30 import System.Environment (getArgs, withArgs)
31 import System.Exit (ExitCode(..), exitWith)
32 import System.IO (hPutStrLn, stderr)
39 Args { output :: FilePath,
41 deriving (Show, Data, Typeable)
45 description = "Convert LWN articles to EPUB format."
47 program_name :: String
48 program_name = "lwn_epub"
50 lwn_epub_summary :: String
52 program_name ++ "-" ++ (showVersion version)
55 output_help = "Output file, defaults to stdout"
57 arg_spec :: Mode (CmdArgs Args)
58 arg_spec = cmdArgsMode $
60 output = def &= typFile &= help output_help,
61 article = def &= argPos 0 &= typ "ARTICLE"
63 &= program program_name
64 &= summary lwn_epub_summary
65 &= details [description]
68 -- Infix notation won't work, the arguments are backwards!
69 is_missing_arg_error :: String -> Bool
70 is_missing_arg_error s =
71 startswith "Requires at least" s
74 parse_args :: IO (CmdArgs Args)
77 let y = process arg_spec x
79 Right result -> return result
81 if (is_missing_arg_error err) then
82 -- Disregard the error message, show help instead.
83 withArgs ["--help"] parse_args
86 exitWith (ExitFailure exit_args_parse_failed)
89 -- | Really get the command-line arguments. This calls 'parse_args'
90 -- first to replace the default "wrong number of arguments" error,
91 -- and then runs 'cmdArgsApply' on the result to do what the
92 -- 'cmdArgs' function usually does.
95 parse_args >>= cmdArgsApply