Safe Haskell | None |
---|---|
Language | Haskell2010 |
WithCli.Pure
Contents
Synopsis
- withCliPure :: WithCliPure function a => String -> [Modifier] -> [String] -> function -> Result a
- class WithCliPure function output
- data Result a
- = Success a
- | Errors String
- | OutputAndExit String
- handleResult :: Result a -> IO a
- class HasArguments a where
- argumentsParser :: Modifiers -> Maybe String -> Result (Parser Unnormalized a)
- atomicArgumentsParser :: forall a. Argument a => Modifiers -> Maybe String -> Result (Parser Unnormalized a)
- class Argument a where
- argumentType :: Proxy a -> String
- parseArgument :: String -> Maybe a
- data Modifier
- = AddShortOption String Char
- | RenameOption String String
- | RenameOptions (String -> Maybe String)
- | UseForPositionalArguments String String
- | AddOptionHelp String String
- | AddVersionFlag String
- class Generic a
- class Typeable (a :: k)
- data Proxy (t :: k) = Proxy
Documentation
Arguments
:: WithCliPure function a | |
=> String | |
-> [Modifier] | |
-> [String] | |
-> function | The |
-> Result a |
Pure variant of withCliModified
.
class WithCliPure function output Source #
Minimal complete definition
run
Instances
WithCliPure output output Source # | |
(HasArguments input, WithCliPure function output) => WithCliPure (input -> function) output Source # | |
Type to wrap results from withCliPure
.
Constructors
Success a | The CLI was used correctly and a value of type |
Errors String | The CLI was used incorrectly. The It can also happen that the data type you're trying to use isn't supported. See the README for details. |
OutputAndExit String | The CLI was used with |
handleResult :: Result a -> IO a Source #
Handles an input of type
:Result
a
- On
it returns the valueSuccess
aa
. - On
it writes the message toOutputAndExit
messagestdout
and throwsExitSuccess
. - On
it writes the error messages toErrors
errsstderr
and throws
.ExitFailure
1
This is used by withCli
to handle parse results.
class HasArguments a where Source #
Everything that can be used as an argument to your main
function
(see withCli
) needs to have a HasArguments
instance.
HasArguments
also allows to conjure up instances for record types
to create more complex command line interfaces. Here's an example:
{-# LANGUAGE DeriveAnyClass #-} {-# LANGUAGE DeriveGeneric #-} import WithCli data Options = Options { port :: Int, daemonize :: Bool, config :: Maybe FilePath } deriving (Show, Generic, HasArguments) main :: IO () main = withCli run run :: Options -> IO () run = print
In a shell this program behaves like this:
$ program --port 8080 --config some/path Options {port = 8080, daemonize = False, config = Just "some/path"} $ program --port 8080 --daemonize Options {port = 8080, daemonize = True, config = Nothing} $ program --port foo cannot parse as INTEGER: foo # exit-code 1 $ program missing option: --port=INTEGER # exit-code 1 $ program --help program [OPTIONS] --port=INTEGER --daemonize --config=STRING (optional) -h --help show help and exit
Minimal complete definition
Nothing
Methods
argumentsParser :: Modifiers -> Maybe String -> Result (Parser Unnormalized a) Source #
default argumentsParser :: (Generic a, GTo a, GDatatypeInfo a, All2 HasArguments (GCode a)) => Modifiers -> Maybe String -> Result (Parser Unnormalized a) Source #
Instances
HasArguments Bool Source # | |
Defined in WithCli.HasArguments Methods argumentsParser :: Modifiers -> Maybe String -> Result (Parser Unnormalized Bool) Source # | |
HasArguments Double Source # | |
Defined in WithCli.HasArguments Methods argumentsParser :: Modifiers -> Maybe String -> Result (Parser Unnormalized Double) Source # | |
HasArguments Float Source # | |
Defined in WithCli.HasArguments Methods argumentsParser :: Modifiers -> Maybe String -> Result (Parser Unnormalized Float) Source # | |
HasArguments Int Source # | |
Defined in WithCli.HasArguments Methods argumentsParser :: Modifiers -> Maybe String -> Result (Parser Unnormalized Int) Source # | |
HasArguments String Source # | |
Defined in WithCli.HasArguments | |
Argument a => HasArguments [a] Source # | |
Defined in WithCli.HasArguments Methods argumentsParser :: Modifiers -> Maybe String -> Result (Parser Unnormalized [a]) Source # | |
Argument a => HasArguments (Maybe a) Source # | |
Defined in WithCli.HasArguments Methods argumentsParser :: Modifiers -> Maybe String -> Result (Parser Unnormalized (Maybe a)) Source # | |
(HasArguments a, HasArguments b) => HasArguments (a, b) Source # | |
Defined in WithCli.HasArguments Methods argumentsParser :: Modifiers -> Maybe String -> Result (Parser Unnormalized (a, b)) Source # | |
(HasArguments a, HasArguments b, HasArguments c) => HasArguments (a, b, c) Source # | |
Defined in WithCli.HasArguments Methods argumentsParser :: Modifiers -> Maybe String -> Result (Parser Unnormalized (a, b, c)) Source # |
atomicArgumentsParser :: forall a. Argument a => Modifiers -> Maybe String -> Result (Parser Unnormalized a) Source #
Useful for implementing your own instances of HasArguments
on top
of a custom Argument
instance.
class Argument a where Source #
Argument
is a typeclass for things that can be parsed as atomic values from
single command line arguments, e.g. strings (and filenames) and numbers.
Occasionally you might want to declare your own instance for additional type safety and for providing a more informative command argument type. Here's an example:
{-# LANGUAGE DeriveDataTypeable #-} import WithCli data File = File FilePath deriving (Show, Typeable) instance Argument File where argumentType Proxy = "custom-file-type" parseArgument f = Just (File f) instance HasArguments File where argumentsParser = atomicArgumentsParser main :: IO () main = withCli run run :: File -> IO () run = print
And this is how the above program behaves:
$ program --help program [OPTIONS] custom-file-type -h --help show help and exit $ program some/file File "some/file"
Instances
Argument Double Source # | |
Defined in WithCli.Argument Methods argumentType :: Proxy Double -> String Source # parseArgument :: String -> Maybe Double Source # | |
Argument Float Source # | |
Defined in WithCli.Argument Methods argumentType :: Proxy Float -> String Source # parseArgument :: String -> Maybe Float Source # | |
Argument Int Source # | |
Defined in WithCli.Argument | |
Argument Integer Source # | |
Defined in WithCli.Argument Methods argumentType :: Proxy Integer -> String Source # parseArgument :: String -> Maybe Integer Source # | |
Argument String Source # | |
Defined in WithCli.Argument |
Modifiers
Modifier
s can be used to customize the command line parser.
Constructors
AddShortOption String Char |
|
RenameOption String String |
|
RenameOptions (String -> Maybe String) |
Can be used together with |
UseForPositionalArguments String String |
|
AddOptionHelp String String |
|
AddVersionFlag String |
|
Useful Re-exports
Minimal complete definition
from, to
Instances
Generic Bool | |
Defined in GHC.Generics Associated Types type Rep Bool :: Type -> Type | |
Generic Ordering | |
Defined in GHC.Generics Associated Types type Rep Ordering :: Type -> Type | |
Generic Exp | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep Exp :: Type -> Type | |
Generic Match | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep Match :: Type -> Type | |
Generic Clause | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep Clause :: Type -> Type | |
Generic Pat | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep Pat :: Type -> Type | |
Generic Type | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep Type :: Type -> Type | |
Generic Dec | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep Dec :: Type -> Type | |
Generic Name | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep Name :: Type -> Type | |
Generic FunDep | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep FunDep :: Type -> Type | |
Generic InjectivityAnn | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep InjectivityAnn :: Type -> Type | |
Generic Overlap | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep Overlap :: Type -> Type | |
Generic () | |
Defined in GHC.Generics Associated Types type Rep () :: Type -> Type | |
Generic ExitCode | |
Defined in GHC.IO.Exception Associated Types type Rep ExitCode :: Type -> Type | |
Generic Void | |
Generic Version | |
Defined in Data.Version Associated Types type Rep Version :: Type -> Type | |
Generic SourceUnpackedness | |
Defined in GHC.Generics Associated Types type Rep SourceUnpackedness :: Type -> Type Methods from :: SourceUnpackedness -> Rep SourceUnpackedness x to :: Rep SourceUnpackedness x -> SourceUnpackedness | |
Generic SourceStrictness | |
Defined in GHC.Generics Associated Types type Rep SourceStrictness :: Type -> Type Methods from :: SourceStrictness -> Rep SourceStrictness x to :: Rep SourceStrictness x -> SourceStrictness | |
Generic DecidedStrictness | |
Defined in GHC.Generics Associated Types type Rep DecidedStrictness :: Type -> Type Methods from :: DecidedStrictness -> Rep DecidedStrictness x to :: Rep DecidedStrictness x -> DecidedStrictness | |
Generic Associativity | |
Defined in GHC.Generics Associated Types type Rep Associativity :: Type -> Type | |
Generic Fixity | |
Defined in GHC.Generics Associated Types type Rep Fixity :: Type -> Type | |
Generic ForeignSrcLang | |
Defined in GHC.ForeignSrcLang.Type Associated Types type Rep ForeignSrcLang :: Type -> Type | |
Generic Extension | |
Defined in GHC.LanguageExtensions.Type Associated Types type Rep Extension :: Type -> Type | |
Generic Mode | |
Defined in Text.PrettyPrint.Annotated.HughesPJ Associated Types type Rep Mode :: Type -> Type | |
Generic Style | |
Defined in Text.PrettyPrint.Annotated.HughesPJ Associated Types type Rep Style :: Type -> Type | |
Generic TextDetails | |
Defined in Text.PrettyPrint.Annotated.HughesPJ Associated Types type Rep TextDetails :: Type -> Type | |
Generic Doc | |
Defined in Text.PrettyPrint.HughesPJ Associated Types type Rep Doc :: Type -> Type | |
Generic AnnLookup | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep AnnLookup :: Type -> Type | |
Generic AnnTarget | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep AnnTarget :: Type -> Type | |
Generic Bang | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep Bang :: Type -> Type | |
Generic Body | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep Body :: Type -> Type | |
Generic Callconv | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep Callconv :: Type -> Type | |
Generic Con | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep Con :: Type -> Type | |
Generic DecidedStrictness | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep DecidedStrictness :: Type -> Type | |
Generic DerivClause | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep DerivClause :: Type -> Type | |
Generic DerivStrategy | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep DerivStrategy :: Type -> Type | |
Generic FamilyResultSig | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep FamilyResultSig :: Type -> Type | |
Generic Fixity | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep Fixity :: Type -> Type | |
Generic FixityDirection | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep FixityDirection :: Type -> Type | |
Generic Foreign | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep Foreign :: Type -> Type | |
Generic Guard | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep Guard :: Type -> Type | |
Generic Info | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep Info :: Type -> Type | |
Generic Inline | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep Inline :: Type -> Type | |
Generic Lit | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep Lit :: Type -> Type | |
Generic Loc | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep Loc :: Type -> Type | |
Generic ModName | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep ModName :: Type -> Type | |
Generic Module | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep Module :: Type -> Type | |
Generic ModuleInfo | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep ModuleInfo :: Type -> Type | |
Generic NameFlavour | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep NameFlavour :: Type -> Type | |
Generic NameSpace | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep NameSpace :: Type -> Type | |
Generic OccName | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep OccName :: Type -> Type | |
Generic PatSynArgs | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep PatSynArgs :: Type -> Type | |
Generic PatSynDir | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep PatSynDir :: Type -> Type | |
Generic Phases | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep Phases :: Type -> Type | |
Generic PkgName | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep PkgName :: Type -> Type | |
Generic Pragma | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep Pragma :: Type -> Type | |
Generic Range | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep Range :: Type -> Type | |
Generic Role | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep Role :: Type -> Type | |
Generic RuleBndr | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep RuleBndr :: Type -> Type | |
Generic RuleMatch | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep RuleMatch :: Type -> Type | |
Generic Safety | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep Safety :: Type -> Type | |
Generic SourceStrictness | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep SourceStrictness :: Type -> Type | |
Generic SourceUnpackedness | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep SourceUnpackedness :: Type -> Type | |
Generic Stmt | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep Stmt :: Type -> Type | |
Generic TyLit | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep TyLit :: Type -> Type | |
Generic TySynEqn | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep TySynEqn :: Type -> Type | |
Generic TyVarBndr | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep TyVarBndr :: Type -> Type | |
Generic TypeFamilyHead | |
Defined in Language.Haskell.TH.Syntax Associated Types type Rep TypeFamilyHead :: Type -> Type | |
Generic ConstructorInfo | |
Defined in Language.Haskell.TH.Datatype Associated Types type Rep ConstructorInfo :: Type -> Type | |
Generic ConstructorVariant | |
Defined in Language.Haskell.TH.Datatype Associated Types type Rep ConstructorVariant :: Type -> Type | |
Generic DatatypeInfo | |
Defined in Language.Haskell.TH.Datatype Associated Types type Rep DatatypeInfo :: Type -> Type | |
Generic DatatypeVariant | |
Defined in Language.Haskell.TH.Datatype Associated Types type Rep DatatypeVariant :: Type -> Type | |
Generic FieldStrictness | |
Defined in Language.Haskell.TH.Datatype Associated Types type Rep FieldStrictness :: Type -> Type | |
Generic Strictness | |
Defined in Language.Haskell.TH.Datatype Associated Types type Rep Strictness :: Type -> Type | |
Generic Unpackedness | |
Defined in Language.Haskell.TH.Datatype Associated Types type Rep Unpackedness :: Type -> Type | |
Generic [a] | |
Defined in GHC.Generics Associated Types type Rep [a] :: Type -> Type | |
Generic (Maybe a) | |
Defined in GHC.Generics Associated Types type Rep (Maybe a) :: Type -> Type | |
Generic (Par1 p) | |
Defined in GHC.Generics Associated Types type Rep (Par1 p) :: Type -> Type | |
Generic (NonEmpty a) | |
Generic (Down a) | |
Defined in GHC.Generics Associated Types type Rep (Down a) :: Type -> Type | |
Generic (ZipList a) | |
Defined in Control.Applicative Associated Types type Rep (ZipList a) :: Type -> Type | |
Generic (I a) | |
Generic (Doc a) | |
Defined in Text.PrettyPrint.Annotated.HughesPJ Associated Types type Rep (Doc a) :: Type -> Type | |
Generic (Either a b) | |
Defined in GHC.Generics Associated Types type Rep (Either a b) :: Type -> Type | |
Generic (V1 p) | |
Defined in GHC.Generics Associated Types type Rep (V1 p) :: Type -> Type | |
Generic (U1 p) | |
Defined in GHC.Generics Associated Types type Rep (U1 p) :: Type -> Type | |
Generic (a, b) | |
Defined in GHC.Generics Associated Types type Rep (a, b) :: Type -> Type | |
Generic (Proxy t) | |
Generic (WrappedMonad m a) | |
Defined in Control.Applicative Associated Types type Rep (WrappedMonad m a) :: Type -> Type | |
Generic (Rec1 f p) | |
Defined in GHC.Generics Associated Types type Rep (Rec1 f p) :: Type -> Type | |
Generic (URec (Ptr ()) p) | |
Defined in GHC.Generics Associated Types type Rep (URec (Ptr ()) p) :: Type -> Type | |
Generic (URec Char p) | |
Defined in GHC.Generics Associated Types type Rep (URec Char p) :: Type -> Type | |
Generic (URec Double p) | |
Defined in GHC.Generics Associated Types type Rep (URec Double p) :: Type -> Type | |
Generic (URec Float p) | |
Defined in GHC.Generics Associated Types type Rep (URec Float p) :: Type -> Type | |
Generic (URec Int p) | |
Defined in GHC.Generics Associated Types type Rep (URec Int p) :: Type -> Type | |
Generic (URec Word p) | |
Defined in GHC.Generics Associated Types type Rep (URec Word p) :: Type -> Type | |
Generic (a, b, c) | |
Defined in GHC.Generics Associated Types type Rep (a, b, c) :: Type -> Type | |
Generic (WrappedArrow a b c) | |
Defined in Control.Applicative Associated Types type Rep (WrappedArrow a b c) :: Type -> Type | |
Generic (K a b) | |
Generic (K1 i c p) | |
Defined in GHC.Generics Associated Types type Rep (K1 i c p) :: Type -> Type | |
Generic ((f :+: g) p) | |
Defined in GHC.Generics Associated Types type Rep ((f :+: g) p) :: Type -> Type | |
Generic ((f :*: g) p) | |
Defined in GHC.Generics Associated Types type Rep ((f :*: g) p) :: Type -> Type | |
Generic (a, b, c, d) | |
Defined in GHC.Generics Associated Types type Rep (a, b, c, d) :: Type -> Type | |
Generic (Sum f g a) | |
Defined in Data.Functor.Sum Associated Types type Rep (Sum f g a) :: Type -> Type | |
Generic (Product f g a) | |
Defined in Data.Functor.Product Associated Types type Rep (Product f g a) :: Type -> Type | |
Generic (M1 i c f p) | |
Defined in GHC.Generics Associated Types type Rep (M1 i c f p) :: Type -> Type | |
Generic ((f :.: g) p) | |
Defined in GHC.Generics Associated Types type Rep ((f :.: g) p) :: Type -> Type | |
Generic (a, b, c, d, e) | |
Defined in GHC.Generics Associated Types type Rep (a, b, c, d, e) :: Type -> Type | |
Generic ((f :.: g) p) | |
Generic (a, b, c, d, e, f) | |
Defined in GHC.Generics Associated Types type Rep (a, b, c, d, e, f) :: Type -> Type | |
Generic (a, b, c, d, e, f, g) | |
Defined in GHC.Generics Associated Types type Rep (a, b, c, d, e, f, g) :: Type -> Type |
Constructors
Proxy |
Instances
Generic1 (Proxy :: k -> Type) | |
Monad (Proxy :: Type -> Type) | |
Functor (Proxy :: Type -> Type) | |
Applicative (Proxy :: Type -> Type) | |
Foldable (Proxy :: Type -> Type) | |
Defined in Data.Foldable Methods fold :: Monoid m => Proxy m -> m foldMap :: Monoid m => (a -> m) -> Proxy a -> m foldMap' :: Monoid m => (a -> m) -> Proxy a -> m foldr :: (a -> b -> b) -> b -> Proxy a -> b foldr' :: (a -> b -> b) -> b -> Proxy a -> b foldl :: (b -> a -> b) -> b -> Proxy a -> b foldl' :: (b -> a -> b) -> b -> Proxy a -> b foldr1 :: (a -> a -> a) -> Proxy a -> a foldl1 :: (a -> a -> a) -> Proxy a -> a elem :: Eq a => a -> Proxy a -> Bool maximum :: Ord a => Proxy a -> a | |
Traversable (Proxy :: Type -> Type) | |
MonadPlus (Proxy :: Type -> Type) | |
Alternative (Proxy :: Type -> Type) | |
Bounded (Proxy t) | |
Defined in Data.Proxy | |
Enum (Proxy s) | |
Defined in Data.Proxy | |
Eq (Proxy s) | |
Ord (Proxy s) | |
Read (Proxy t) | |
Show (Proxy s) | |
Ix (Proxy s) | |
Generic (Proxy t) | |
Semigroup (Proxy s) | |
Monoid (Proxy s) | |
type Rep1 (Proxy :: k -> Type) | |
Defined in GHC.Generics type Rep1 (Proxy :: k -> Type) = D1 ('MetaData "Proxy" "Data.Proxy" "base" 'False) (C1 ('MetaCons "Proxy" 'PrefixI 'False) (U1 :: k -> Type)) | |
type Rep (Proxy t) | |
Defined in GHC.Generics type Rep (Proxy t) = D1 ('MetaData "Proxy" "Data.Proxy" "base" 'False) (C1 ('MetaCons "Proxy" 'PrefixI 'False) (U1 :: Type -> Type)) | |
type Code (Proxy t) | |
Defined in Generics.SOP.Instances | |
type DatatypeInfoOf (Proxy t) | |
Defined in Generics.SOP.Instances type DatatypeInfoOf (Proxy t) = 'ADT "Data.Proxy" "Proxy" '['Constructor "Proxy"] '['[] :: [StrictnessInfo]] |