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.Option as Option
import Cli.OptionsParser as OptionsParser import Cli.OptionsParser as OptionsParser
import Cli.Program as Program import Cli.Program as Program
import Cli.Validate
import Elm import Elm
import Elm.Annotation import Elm.Annotation
import Elm.Case import Elm.Case
@ -58,19 +57,11 @@ program =
Program.config Program.config
|> Program.add |> Program.add
(OptionsParser.build CliOptions (OptionsParser.build CliOptions
|> OptionsParser.with |> OptionsParser.with (Option.requiredPositionalArg "module" |> Pages.Generate.moduleNameCliArg)
(Option.requiredPositionalArg "module"
|> Option.validate (Cli.Validate.regex moduleNameRegex)
)
|> OptionsParser.withRestArgs AddFormHelp.restArgsParser |> 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 : List String -> List ( String, AddFormHelp.Kind ) -> Elm.File
createFile moduleName fields = createFile moduleName fields =
let let

View File

@ -4,6 +4,7 @@ module Pages.Generate exposing
, serverRender , serverRender
, preRender, single , preRender, single
, addDeclarations , 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. {-| 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 @docs addDeclarations
## CLI Options Parsing Helpers
@docs moduleNameCliArg
-} -}
import Cli.Option as Option
import Cli.Validate
import Elm import Elm
import Elm.Annotation import Elm.Annotation
import Elm.Declare import Elm.Declare
import Pages.Internal.RoutePattern as RoutePattern 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 type Type
= Alias Elm.Annotation.Annotation = Alias Elm.Annotation.Annotation