Extract helper for module name CLI options parsing.

This commit is contained in:
Dillon Kearns 2023-02-06 12:07:41 -08:00
parent da132b13f3
commit 5aeade0daf
2 changed files with 21 additions and 10 deletions

View File

@ -5,7 +5,6 @@ import BackendTask
import Cli.Option as Option
import Cli.OptionsParser as OptionsParser
import Cli.Program as Program
import Cli.Validate
import Elm
import Elm.Annotation
import Elm.Case
@ -58,19 +57,11 @@ program =
Program.config
|> Program.add
(OptionsParser.build CliOptions
|> OptionsParser.with
(Option.requiredPositionalArg "module"
|> Option.validate (Cli.Validate.regex moduleNameRegex)
)
|> OptionsParser.with (Option.requiredPositionalArg "module" |> Pages.Generate.moduleNameCliArg)
|> OptionsParser.withRestArgs AddFormHelp.restArgsParser
)
moduleNameRegex : String
moduleNameRegex =
"^[A-Z][a-zA-Z0-9_]*(\\.([A-Z][a-zA-Z0-9_]*))*$"
createFile : List String -> List ( String, AddFormHelp.Kind ) -> Elm.File
createFile moduleName fields =
let

View File

@ -4,6 +4,7 @@ module Pages.Generate exposing
, serverRender
, preRender, single
, addDeclarations
, moduleNameCliArg
)
{-| This module provides some functions for scaffolding code for a new Route Module. It uses [`elm-codegen`'s API](https://package.elm-lang.org/packages/mdgriffith/elm-codegen/latest/) for generating code.
@ -33,14 +34,33 @@ Learn more about [the `elm-pages run` CLI command in its docs page](https://elm-
@docs addDeclarations
## CLI Options Parsing Helpers
@docs moduleNameCliArg
-}
import Cli.Option as Option
import Cli.Validate
import Elm
import Elm.Annotation
import Elm.Declare
import Pages.Internal.RoutePattern as RoutePattern
{-| A positional argument for elm-cli-options-parser that does a Regex validation to check that the module name is a valid Elm Route module name.
-}
moduleNameCliArg : Option.Option from String builderState -> Option.Option from String builderState
moduleNameCliArg =
Option.validate (Cli.Validate.regex moduleNameRegex)
moduleNameRegex : String
moduleNameRegex =
"^[A-Z][a-zA-Z0-9_]*(\\.([A-Z][a-zA-Z0-9_]*))*$"
{-| -}
type Type
= Alias Elm.Annotation.Annotation