dillonkearns / elm-cli-options-parser / Cli.Program

Config

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
        |> Program.add
            (OptionsParser.build GreetOptions
                |> OptionsParser.with (Option.requiredKeywordArg "name")
                |> OptionsParser.with (Option.optionalKeywordArg "greeting")
            )

type alias GreetOptions =
    { name : String
    , maybeGreeting : Maybe String
    }

init : Flags -> GreetOptions -> Cmd Never
init flags { name, maybeGreeting } =
    maybeGreeting
        |> Maybe.withDefault "Hello"
        |> (\greeting -> greeting ++ " " ++ name ++ "!")
        |> Ports.print

type alias Flags =
    Program.FlagsIncludingArgv {}

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.

config : Config decodesTo

Create a Config with no OptionsParsers. Use Cli.Program.add to add OptionsParsers.


type Config msg

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 : Cli.OptionsParser.OptionsParser msg anything -> Config msg -> Config msg

Add an OptionsParser to your Cli.Program.Config.

Programs

stateless : ProgramOptions msg options flags -> StatelessProgram msg flags

stateful : StatefulOptions msg model cliOptions flags -> Platform.Program (FlagsIncludingArgv flags) (StatefulProgramModel model cliOptions) msg

A stateful program can have a model that it creates and updates via init and update. It also has subscriptions. See the Curl.elm example.


type alias StatelessProgram msg flags =
Platform.Program (FlagsIncludingArgv flags) () msg


type alias StatefulProgram model msg cliOptions flags =
Platform.Program (FlagsIncludingArgv flags) (StatefulProgramModel model cliOptions) msg


type alias FlagsIncludingArgv flagsRecord =
{ flagsRecord | argv : List String
, versionMessage : String 
}

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").Elm.Main.init({
  flags: { argv: process.argv, versionMessage: "1.2.3" }
});

mapConfig : (a -> b) -> Config a -> Config b

Transform the return type for all of the registered OptionsParser's in the Config.