A Cli.Program.Config is created with Cli.Program.config. Then OptionsParsers are added
to it with Cli.Program.add. Finally, you create a Cli.Program.StatelessProgram
using stateless or a Cli.Program.StatefulProgram using stateful.
import Cli.Option as Option
import Cli.OptionsParser as OptionsParser
import Cli.Program as Program
import Ports
programConfig : Program.Config GreetOptions
programConfig =
Program.config { version = "1.2.3" }
|> Program.add
(OptionsParser.build GreetOptions
|> OptionsParser.with (Option.requiredKeywordArg "name")
|> OptionsParser.with (Option.optionalKeywordArg "greeting")
)
type alias GreetOptions =
{ name : String
, maybeGreeting : Maybe String
}
init : GreetOptions -> Cmd Never
init { name, maybeGreeting } =
maybeGreeting
|> Maybe.withDefault "Hello"
|> (\greeting -> greeting ++ " " ++ name ++ "!")
|> Ports.print
main : Program.StatelessProgram Never
main =
Program.stateless
{ printAndExitFailure = Ports.printAndExitFailure
, printAndExitSuccess = Ports.printAndExitSuccess
, init = init
, config = programConfig
}See the examples for some end-to-end examples.
A Cli.Program.Config is used to build up a set of OptionsParsers for your
Command-Line Interface, as well as its meta-data such as version number.
Add an OptionsParser to your Cli.Program.Config.
ProgramsA stateful program can have a model that it creates and updates via init
and update. It also has subscriptions. See
the Curl.elm example.
Flags in Cli Programs can contain any data as long as it is a record
at the top-level which contains an argv field of type List String.
In other words, it must be a record of type FlagsIncludingArgv
(if you aren't familiar with them, you can read more about extensible records here).
You pass in the flags like this (see the examples folder for more):
#!/usr/bin/env node
let program = require('./elm.js').Main.worker({ argv: process.argv })
Create a
Configwith noOptionsParsers. UseCli.Program.addto addOptionsParsers.