Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Maddash is a Mealy finite state machine that processes options. Ordinarily you will not need this module; instead, see Multiarg for most uses or Multiarg.Mode for commands that have more than one mode.
The machine consists of the following parts:
- The set of states, in
State
- The start state, which is
Ready
- The input alphabet, which is all
Word
s. AWord
is an input word from the command line. - The output alphabet, which is
Pallet
. APallet
indicates whether its input is not an option at all withNotAnOption
. This indicates that the inputWord
was not a short option and was not a long option; that is, it was not a single dash followed by a non-dash character and it was not a double dash followed by another character. (Neither a single dash alone nor a double dash alone is an option.) Anything else is an option and will returnFull
, which is a list ofOutput
. EachOutput
indicates either an error or a good result. - The transition function and the output function are combined into
a single function,
processWord
.
- newtype OptName = OptName (Either ShortName LongName)
- optSpec :: [Char] -> [String] -> ArgSpec a -> OptSpec a
- data ArgSpec a
- data ShortName
- data LongName
- shortName :: Char -> Maybe ShortName
- longName :: String -> Maybe LongName
- shortNameToChar :: ShortName -> Char
- longNameToString :: LongName -> String
- data Output a
- = Good a
- | OptionError OptionError
- data Pallet a
- = NotAnOption
- | Full [Output a]
- data State a
- isReady :: State a -> Bool
- isPending :: State a -> Bool
- processWord :: [(ShortName, ArgSpec a)] -> [(LongName, ArgSpec a)] -> State a -> Word -> (Pallet a, State a)
- processWords :: [(ShortName, ArgSpec a)] -> [(LongName, ArgSpec a)] -> [Word] -> ([[Output a]], Either (OptName, Word -> ([Output a], State a)) [Word])
- newtype OptArg = OptArg {}
- data OptionError
Options and option arguments
The name of an option (either a short option name or a long option name).
:: [Char] | There is one character for each desired short option name.
Each of these characters may not be a hyphen; otherwise,
|
-> [String] | There is one string for each desired long option name. Each string:
|
-> ArgSpec a | How many option arguments this option takes. This also specifies what is returned when the option is parsed on the command line. |
-> OptSpec a |
Creates an OptSpec
.
Specifies how many option arguments an option takes.
shortName :: Char -> Maybe ShortName Source
Creates a short option name. Any character other than a single hyphen will succeed.
longName :: String -> Maybe LongName Source
Creates a long option name. The string may not be empty, and the first character may not be a hyphen. In addition, no character may be an equal sign.
shortNameToChar :: ShortName -> Char Source
longNameToString :: LongName -> String Source
Machine components
NotAnOption | |
Full [Output a] |
processWord :: [(ShortName, ArgSpec a)] -> [(LongName, ArgSpec a)] -> State a -> Word -> (Pallet a, State a) Source
Process a single word in the machine.
Multi-word processor
processWords :: [(ShortName, ArgSpec a)] -> [(LongName, ArgSpec a)] -> [Word] -> ([[Output a]], Either (OptName, Word -> ([Output a], State a)) [Word]) Source
Processes multiple words in the machine. Processing ends with
the first word that is NotAnOption
. This first word that is
NotAnOption
, and all remaining words, are returned in the
result. A list of all lists of Output
are also returned, with
one list for each input Word
that was processed. Each of these
lists may be of any length. For instance, if the input word is
the flag for a long option that takes two option arguments,
the corresponding list will be empty. If the input word is a
flag for a short option, this list may have more than one
element.
Errors
An option argument.
data OptionError Source
BadOption OptName | |
LongArgumentForZeroArgumentOption LongName OptArg | The user gave an argument for a long option that does not take an argument. |