-options :: [OptDescr (Options -> IO Options)]
-options =
- [ Option "h" ["help"] (NoArg set_help) "Prints this help message.",
- Option "i" ["input"] (ReqArg set_input "FILE") "Read FILE instead of stdin." ]
-
--- | Takes an Options as an argument, and sets its opt_help member to
--- True.
-set_help :: Options -> IO Options
-set_help opts =
- return opts { opt_help = True }
-
-
--- | If the input file option is set, this function will update the
--- passed Options record with a new function for opt_input. The
--- default opt_input is to read from stdin, but if this option is
--- set, we replace that with readFile.
-set_input :: String -> Options -> IO Options
-set_input arg opts =
- return opts { opt_input = readFile arg }
-
-
--- | The usage header.
-usage :: String
-usage =
- "Usage: hath " ++
- "[regexed|reduced|duped|diffed|listed|reversed] " ++
- "[-h] " ++
- "[-i FILE] " ++
- "<input>"
-
-
--- | The usage header, and all available flags (as generated by GetOpt).
-help_text :: String
-help_text = usageInfo usage options
-
-
--- | Return a list of options.
-parse_options :: IO Options
-parse_options = do
- argv <- getArgs
- let (actions, _, _) = getOpt Permute options argv
-
- -- This will execute each of the functions contained in our options
- -- list, one after another, on a default_options record. The end
- -- result should be an Options instance with all of its members set
- -- correctly.
- foldl (>>=) (return default_options) actions
-
-
-
--- | Return the mode if one was given.
-parse_mode :: IO Mode
-parse_mode = do
- argv <- getArgs
- let (_, non_options, _) = getOpt Permute options argv
- return $ case non_options of
- -- Default
- [] -> Regex
- -- Some non-option was given, but were any of them modes?
- (x:_) ->
- case (lowercase x) of
- "regex" -> Regex
- "regexed" -> Regex
- "reduce" -> Reduce
- "reduced" -> Reduce
- "dupe" -> Dupe
- "duped" -> Dupe
- "diff" -> Diff
- "diffed" -> Diff
- "list" -> List
- "listed" -> List
- "reverse" -> Reverse
- "reversed" -> Reverse
- _ -> Regex
-
-
-
-
--- | Return a list of errors.
-parse_errors :: IO [String]
-parse_errors = do
- argv <- getArgs
- let (_, _, errors) = getOpt Permute options argv
- return errors
-
-
-
--- | Is the help option set?
-help_set :: IO Bool
-help_set = do
- opts <- parse_options
- return (opt_help opts)
-
-
--- | Return our input function, getContents by default, or readFile if
--- the input file option was set.
-input_function :: IO (IO String)
-input_function = do
- opts <- parse_options
- return (opt_input opts)
+arg_spec :: Annotate Ann
+arg_spec =
+ modes_ [regexed += auto, reduced, duped, diffed, listed, reversed]
+ += program program_name
+ += summary my_summary
+ += helpArg [explicit,
+ name "help",
+ name "h",
+ groupname "Common flags"]
+ += versionArg [explicit,
+ name "version",
+ name "v",
+ groupname "Common flags"]
+ where
+ make_mode :: (Bool -> Args) -> String -> (Annotate Ann)
+ make_mode ctor desc =
+ record (ctor def) [ barriers := def
+ += groupname "Common flags"
+ += help barriers_help ]
+ += details [" " ++ desc]
+
+ regexed = make_mode Regexed regexed_description
+ reduced = make_mode Reduced reduced_description
+ duped = make_mode Duped duped_description
+ diffed = make_mode Diffed diffed_description
+ listed = make_mode Listed listed_description
+ reversed = make_mode Reversed reversed_description
+
+-- | This is the public interface; i.e. what main() should use to get
+-- the command-line arguments.
+get_args :: IO Args
+get_args = cmdArgs_ arg_spec