@@ -88,35 +88,36 @@ sample = Sample
8888 ( long "quiet"
8989 <> short 'q'
9090 <> help "Whether to be quiet" )
91- <*> option auto
91+ <*> option int
9292 ( long "enthusiasm"
9393 <> help "How enthusiastically to greet"
9494 <> showDefault
9595 <> value 1
9696 <> metavar "INT" )
9797```
9898
99- The parser is built using an [ applicative] style starting from a
100- set of basic combinators. In this example, hello is defined as an
99+ The parser is built using an applicative style starting from a
100+ set of basic combinators (you can also check [ Applicative Do] ( #applicative-do )
101+ section for definition of ` sample ` using ` ado ` ). In this example, hello is defined as an
101102option with a ` String ` argument, while quiet is a boolean flag
102103(called a switch) and enthusiasm gets parsed as an ` Int ` with help
103- of the ` Read ` type class .
104+ of the ` int :: ReadM Int ` .
104105
105106
106107The parser can be used like this:
107108
108109``` purs
109- main :: IO ()
110+ main :: Effect Unit
110111main = greet =<< execParser opts
111112 where
112113 opts = info (sample <**> helper)
113114 ( fullDesc
114115 <> progDesc "Print a greeting for TARGET"
115116 <> header "hello - a test for purescript-optparse" )
116117
117- greet :: Sample -> IO ()
118+ greet :: Sample -> Effect Unit
118119greet (Sample h false n) = putStrLn $ "Hello, " ++ h ++ replicate n '!'
119- greet _ = return ()
120+ greet _ = return unit
120121```
121122
122123The ` greet ` function is the entry point of the program, while ` opts `
@@ -241,9 +242,7 @@ parallel, and can not depend on the output of other options.
241242
242243Note, however, that the order of sequencing is still somewhat
243244significant, in that it affects the generated help text. Customisation
244- can be achieved easily through a lambda abstraction, with [ Arrow
245- notation] ( #arrow-interface ) , or by taking advantage of GHC 8's
246- [ ApplicativeDo] ( #applicative-do ) extension.
245+ can be achieved easily by using [ ApplicativeDo] ( #applicative-do ) .
247246
248247### Alternative
249248
@@ -259,7 +258,7 @@ data Input
259258 = FileInput FilePath
260259 | StdInput
261260
262- run :: Input -> IO ()
261+ run :: Input -> Effect Unit
263262run = ...
264263```
265264
@@ -320,14 +319,14 @@ opts = info (sample <**> helper)
320319```
321320
322321The ` helper ` parser that we added after ` opts ` just creates a dummy
323- ` --help ` option that displays the help text. Besides that, we just
322+ ` --help ` option that displays the help text. Besides that, we just
324323set some of the fields of the ` ParserInfo ` structure with meaningful
325- values. Now that we have a ` ParserInfo ` , we can finally run the
326- parser. The simplest way to do so is to simply call the ` execParser `
324+ values. Now that we have a ` ParserInfo ` , we can finally run the
325+ parser. The simplest way to do so is to simply call the ` execParser `
327326function in your ` main ` :
328327
329328``` purs
330- main :: IO ()
329+ main :: Effect Unit
331330main = do
332331 options <- execParser opts
333332 ...
@@ -357,9 +356,6 @@ Builders work by building the option from scratch, and eventually
357356lifting it to a single-option parser, ready to be combined with
358357other parsers using normal ` Applicative ` and ` Alternative ` combinators.
359358
360- See the [ haddock documentation] [ hackage ] for ` Options.Applicative.Builder `
361- for a full list of builders and modifiers.
362-
363359There are four different kinds of options in ` purescript-optparse ` :
364360regular options, flags, arguments, and commands. In the following,
365361we will go over each one of these and describe the builders that
@@ -419,24 +415,17 @@ value "out.txt", a long name "output" and a short name "o".
419415
420416A regular ` option ` can return an object of any type, and takes a
421417* reader* parameter which specifies how the argument should be parsed.
422- A common reader is ` auto ` , which requires a ` Read ` instance for the
423- return type and uses it to parse its argument. For example:
418+ Built in readers are ` str ` , ` int ` , ` number ` and ` boolean ` for example:
424419
425420``` purs
426421lineCount :: Parser Int
427- lineCount = option auto
422+ lineCount = option int
428423 ( long "lines"
429424 <> short 'n'
430425 <> metavar "K"
431426 <> help "Output the last K lines" )
432427```
433428
434- specifies a regular option with an ` Int ` argument. We added an
435- explicit type annotation here, since without it the parser would
436- have been polymorphic in the output type. There's usually no need
437- to add type annotations, however, because the type will be normally
438- inferred from the context in which the parser is used.
439-
440429Further information on * readers* is available [ below] ( #option-readers ) .
441430
442431### Flags
@@ -477,7 +466,7 @@ settings could be specified on a scale; the following parser will
477466count the number of instances of ` -v ` on the command line.
478467
479468``` purs
480- length <$> many (flag' () (short 'v'))
469+ length <$> many (flag' unit (short 'v'))
481470```
482471
483472Flags can be used together after a single hyphen, so ` -vvv ` and
@@ -495,7 +484,7 @@ a command line argument for which the reader succeeds. For example
495484argument str (metavar "FILE")
496485```
497486
498- creates an argument accepting any string. To accept an arbitrary
487+ creates an argument accepting any string. To accept an arbitrary
499488number of arguments, combine the ` argument ` builder with either the
500489` many ` or ` some ` combinator:
501490
@@ -555,34 +544,34 @@ data Command
555544 ...
556545```
557546
558- Alternatively, you can directly return an ` IO ` action from a parser,
547+ Alternatively, you can directly return an ` Effect ` action from a parser,
559548and execute it using ` join ` from ` Control.Monad ` .
560549
561550``` purs
562- start :: String -> IO ()
563- stop :: IO ()
551+ start :: String -> Effect Unit
552+ stop :: Effect Unit
564553
565- opts :: Parser (IO () )
554+ opts :: Parser (Effect Unit )
566555opts = subparser
567556 ( command "start" (info (start <$> argument str idm) idm)
568557 <> command "stop" (info (pure stop) idm) )
569558
570- main :: IO ()
559+ main :: Effect Unit
571560main = join $ execParser (info opts idm)
572561```
573562
574563### Modifiers
575564
576565* Modifiers* are instances of the ` Semigroup ` and ` Monoid ` typeclasses,
577566so they can be combined using the composition function ` append `
578- (or simply ` (<>) ` ). Since different builders accept different sets
567+ (or simply ` (<>) ` ). Since different builders accept different sets
579568of modifiers, modifiers have a type parameter that specifies which
580569builders support it.
581570
582571For example,
583572
584573``` purs
585- command :: String -> ParserInfo a -> Mod CommandFields a
574+ command :: forall a. String -> ParserInfo a -> Mod CommandFields a
586575```
587576
588577can only be used with [ commands] ( #commands ) , as the ` CommandFields `
@@ -599,18 +588,18 @@ Parsers are run with the `execParser` family of functions — from
599588easiest to use to most flexible these are:
600589
601590``` purs
602- execParser :: ParserInfo a -> IO a
603- customExecParser :: ParserPrefs -> ParserInfo a -> IO a
604- execParserPure :: ParserPrefs -> ParserInfo a -> [ String] -> ParserResult a
591+ execParser :: forall a. ParserInfo a -> Effect a
592+ customExecParser :: forall a. ParserPrefs -> ParserInfo a -> Effect a
593+ execParserPure :: forall a. ParserPrefs -> ParserInfo a -> Array String -> ParserResult a
605594```
606595
607- When using the ` IO ` functions, retrieving command line arguments
596+ When using the ` Effect ` functions, retrieving command line arguments
608597and handling exit codes and failure will be done automatically.
609598When using ` execParserPure ` , the functions
610599
611600``` purs
612- handleParseResult :: ParserResult a -> IO a
613- overFailure :: (ParserHelp -> ParserHelp) -> ParserResult a -> ParserResult a
601+ handleParseResult :: forall a. ParserResult a -> Effect a
602+ overFailure :: forall a. (ParserHelp -> ParserHelp) -> ParserResult a -> ParserResult a
614603```
615604
616605can be used to correctly set exit codes and display the help message;
@@ -620,13 +609,12 @@ additional information for example).
620609### Option readers
621610
622611Options and Arguments require a way to interpret the string passed
623- on the command line to the type desired. The ` str ` and ` auto `
624- * readers* are the most common way, but one can also create a custom
625- reader that doesn't use the ` Read ` type class or return a ` String ` ,
626- and use it to parse the option. A custom reader is a value in the
612+ on the command line to the type desired. ` str ` , ` int ` , ` number ` and
613+ ` boolean ` are Built in * readers* , but one can also create a custom
614+ reader and use it to parse the option. A custom reader is a value in the
627615` ReadM ` monad.
628616
629- We provide the ` eitherReader :: (String -> Either String a) -> ReadM a `
617+ We provide the ` eitherReader :: forall a. (String -> Either String a) -> ReadM a `
630618convenience function to help create these values, where a ` Left ` will
631619hold the error message for a parse failure.
632620
@@ -643,15 +631,6 @@ One can also use `ReadM` directly, using `readerAsk` to obtain the
643631command line string, and ` readerAbort ` or ` readerError ` within the
644632` ReadM ` monad to exit with an error message.
645633
646- One nice property of ` eitherReader ` is how well it composes with
647- [ attoparsec] parsers with
648-
649- ``` purs
650- import qualified Data.Attoparsec.Text as A
651- attoReadM :: A.Parser a -> ReadM a
652- attoReadM p = eitherReader (A.parseOnly p <<< T.pack)
653- ```
654-
655634### Preferences
656635` PrefsMod ` s can be used to customise the look of the usage text and
657636control when it is displayed; turn off backtracking of subparsers;
@@ -685,12 +664,12 @@ Here is a minimal example:
685664``` purs
686665import Options.Applicative
687666
688- sample :: Parser ()
689- sample = () <$
667+ sample :: Parser Unit
668+ sample = unit <$
690669 switch (long "filename") <*
691670 switch (long "filler")
692671
693- main :: IO ()
672+ main :: Effect Unit
694673main = customExecParser p opts
695674 where
696675 opts = info (helper <*> sample) idm
@@ -724,10 +703,10 @@ message will be, indicating what's missing, or what was unable to
724703be parsed.
725704
726705``` purs
727- myParser :: Parser ()
706+ myParser :: Parser Unit
728707myParser = ...
729708
730- main :: IO ()
709+ main :: Effect Unit
731710main = customExecParser p opts
732711 where
733712 opts = info (myParser <**> helper) idm
@@ -741,7 +720,7 @@ subcommands is command group separation.
741720
742721``` purs
743722data Sample
744- = Hello [ String]
723+ = Hello (Array String)
745724 | Goodbye
746725 deriving (Eq, Show)
747726
@@ -840,7 +819,7 @@ on a regular option or argument:
840819 - ` action ` : specifies a completion "action". An action dynamically determines
841820 a list of possible completions. Common actions are "file" and "directory";
842821 the full list of actions can be found in the [ bash documentation] ;
843- - ` completer ` : a completer is a function ` String -> IO [ String] ` , returning
822+ - ` completer ` : a completer is a function ` String -> Effect (Array String) ` , returning
844823 all possible completions for a given string. You can use this modifier to
845824 specify a custom completion for an argument.
846825
@@ -869,30 +848,29 @@ standard output. They are then read by the completion script and put into the
869848
870849## Applicative do
871850
872- Some may find using purescript-optparse easier using do notation.
873- However, as ` Parser ` is not an instance of ` Monad ` , this can only
874- be done in recent versions of GHC using the * ApplicativeDo* extension.
875- For example, a parser specified in this manner might be
851+ Some may find using purescript-optparse easier using ` ado ` notation. For example,
852+ a parser specified in this ` Sample ` outlined in [ Quick Start] ( #quick-start )
853+ will look like this:
876854
877855``` purs
878- data Options = Options
879- { optArgs :: [String]
880- , optVerbose :: Boolean }
881-
882- opts :: Parser Options
883- opts = do
884- optVerbose <- switch (short 'v')
885- optArgs <- many (argument str idm)
886- pure Options {..}
887- ```
888-
889- Here we've also used the * RecordWildCards * extension to make the
890- parser specification cleaner. Compilation errors referring to ` Monad `
891- instances not being found are likely because the ` Parser ` specified
892- can not be implemented entirely with ` Applicative ` (Note however,
893- there were a few desugaring bugs regarding ApplicativeDo in GHC
894- 8.0.1, function application with ` ($) ` in particular may not work,
895- and the ` pure ` value should instead be wrapped parenthetically).
856+ sample :: Parser Sample
857+ sample = ado
858+ hello <- strOption
859+ ( long "hello"
860+ <> metavar "TARGET"
861+ <> help "Target for the greeting" )
862+ quiet <- switch
863+ ( long "quiet"
864+ <> short 'q'
865+ <> help "Whether to be quiet" )
866+ enthusiasm <- option auto
867+ ( long "enthusiasm"
868+ <> help "How enthusiastically to greet"
869+ <> showDefault
870+ <> value 1
871+ <> metavar "INT" )
872+ in Sample { hello, quiet, enthusiasm }
873+ ```
896874
897875## FAQ
898876
0 commit comments