| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
10 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,7 +6,7 @@ import pf.Stdout | |||
| 6 | 6 | import pf.Stderr | |
| 7 | 7 | import pf.Task exposing [Task] | |
| 8 | 8 | import pf.Arg.Cli as Cli | |
| 9 | - import pf.Arg.Subcommand as Subcommand | ||
| 9 | + import pf.Arg.SubCmd as SubCmd | ||
| 10 | 10 | import pf.Arg.Opt as Opt | |
| 11 | 11 | import pf.Arg.Param as Param | |
| 12 | 12 | import pf.Arg | |
@@ -15,7 +15,7 @@ main = | |||
| 15 | 15 | args = Arg.list! {} | |
| 16 | 16 | ||
| 17 | 17 | when Cli.parseOrDisplayMessage cli args is | |
| 18 | - Ok { command: subcommand } -> | ||
| 18 | + Ok subcommand -> | ||
| 19 | 19 | mathOutcome = | |
| 20 | 20 | when subcommand is | |
| 21 | 21 | Max { first, rest } -> | |
@@ -34,9 +34,7 @@ main = | |||
| 34 | 34 | Task.err (Exit 1 "") | |
| 35 | 35 | ||
| 36 | 36 | cli = | |
| 37 | - Cli.build { | ||
| 38 | - command: <- Subcommand.required [maxSubcommand, divideSubcommand], | ||
| 39 | - } | ||
| 37 | + SubCmd.required [maxSubcommand, divideSubcommand] | ||
| 40 | 38 | |> Cli.finish { | |
| 41 | 39 | name: "args-example", | |
| 42 | 40 | description: "A calculator example of the CLI platform argument parser.", | |
@@ -45,33 +43,31 @@ cli = | |||
| 45 | 43 | |> Cli.assertValid | |
| 46 | 44 | ||
| 47 | 45 | maxSubcommand = | |
| 48 | - Cli.build { | ||
| 46 | + { Cli.combine <- | ||
| 49 | 47 | # ensure there's at least one parameter provided | |
| 50 | - first: <- Param.dec { name: "first", help: "the first number to compare." }, | ||
| 51 | - rest: <- Param.decList { name: "rest", help: "the other numbers to compare." }, | ||
| 48 | + first: Param.dec { name: "first", help: "the first number to compare." }, | ||
| 49 | + rest: Param.decList { name: "rest", help: "the other numbers to compare." }, | ||
| 52 | 50 | } | |
| 53 | - |> Subcommand.finish { | ||
| 51 | + |> SubCmd.finish { | ||
| 54 | 52 | name: "max", | |
| 55 | 53 | description: "Find the largest of multiple numbers.", | |
| 56 | 54 | mapper: Max, | |
| 57 | 55 | } | |
| 58 | 56 | ||
| 59 | 57 | divideSubcommand = | |
| 60 | - Cli.build { | ||
| 61 | - dividend: <- | ||
| 62 | - Opt.dec { | ||
| 63 | - short: "n", | ||
| 64 | - long: "dividend", | ||
| 65 | - help: "the number to divide; corresponds to a numerator.", | ||
| 66 | - }, | ||
| 67 | - divisor: <- | ||
| 68 | - Opt.dec { | ||
| 69 | - short: "d", | ||
| 70 | - long: "divisor", | ||
| 71 | - help: "the number to divide by; corresponds to a denominator.", | ||
| 72 | - }, | ||
| 58 | + { Cli.combine <- | ||
| 59 | + dividend: Opt.dec { | ||
| 60 | + short: "n", | ||
| 61 | + long: "dividend", | ||
| 62 | + help: "the number to divide; corresponds to a numerator.", | ||
| 63 | + }, | ||
| 64 | + divisor: Opt.dec { | ||
| 65 | + short: "d", | ||
| 66 | + long: "divisor", | ||
| 67 | + help: "the number to divide by; corresponds to a denominator.", | ||
| 68 | + }, | ||
| 73 | 69 | } | |
| 74 | - |> Subcommand.finish { | ||
| 70 | + |> SubCmd.finish { | ||
| 75 | 71 | name: "div", | |
| 76 | 72 | description: "Divide two numbers.", | |
| 77 | 73 | mapper: Div, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,9 +7,9 @@ import pf.Task exposing [Task] | |||
| 7 | 7 | ||
| 8 | 8 | main = | |
| 9 | 9 | myrecord : Task { apples : List Str, oranges : List Str } []_ | |
| 10 | - myrecord = Task.ok { | ||
| 11 | - apples: <- getFruit Apples |> Task.batch, | ||
| 12 | - oranges: <- getFruit Oranges |> Task.batch, | ||
| 10 | + myrecord = { sequenceTasks <- | ||
| 11 | + apples: getFruit Apples, | ||
| 12 | + oranges: getFruit Oranges, | ||
| 13 | 13 | } | |
| 14 | 14 | ||
| 15 | 15 | { apples, oranges } = myrecord! | |
@@ -26,3 +26,10 @@ getFruit = \request -> | |||
| 26 | 26 | when request is | |
| 27 | 27 | Apples -> Task.ok ["Granny Smith", "Pink Lady", "Golden Delicious"] | |
| 28 | 28 | Oranges -> Task.ok ["Navel", "Blood Orange", "Clementine"] | |
| 29 | + | ||
| 30 | + sequenceTasks : Task a err, Task b err, (a, b -> c) -> Task c err | ||
| 31 | + sequenceTasks = \firstTask, secondTask, mapper -> | ||
| 32 | + first = firstTask! | ||
| 33 | + second = secondTask! | ||
| 34 | + | ||
| 35 | + Task.ok (mapper first second) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -32,8 +32,9 @@ list = \_ -> | |||
| 32 | 32 | ## | |
| 33 | 33 | ## ```roc | |
| 34 | 34 | ## exampleCli = | |
| 35 | - ## Cli.build { | ||
| 36 | - ## verbosity: <- Opt.count { short: "v", help: "How verbose our logs should be." }, | ||
| 35 | + ## { Cli.combine <- | ||
| 36 | + ## verbosity: Opt.count { short: "v", help: "How verbose our logs should be." }, | ||
| 37 | + ## alpha: Opt.mapbeU64 { short: "a", help: "Set the alpha level." }, | ||
| 37 | 38 | ## } | |
| 38 | 39 | ## |> Cli.finish { | |
| 39 | 40 | ## name: "example", | |
@@ -56,6 +57,7 @@ list = \_ -> | |||
| 56 | 57 | ## | |
| 57 | 58 | ## Options: | |
| 58 | 59 | ## -v How verbose our logs should be. | |
| 60 | + ## -a Set the alpha level. | ||
| 59 | 61 | ## -h, --help Show this help page. | |
| 60 | 62 | ## -V, --version Show the version. | |
| 61 | 63 | ## """ | |
@@ -90,7 +92,6 @@ parse = \parser -> | |||
| 90 | 92 | ShowHelp { subcommandPath } -> | |
| 91 | 93 | helpMessage = | |
| 92 | 94 | helpText parser.config subcommandPath parser.textStyle | |
| 93 | - | ||
| 94 | 95 | Stdout.line! helpMessage | |
| 95 | 96 | Task.err (Exit 0 "") | |
| 96 | 97 | ||
@@ -105,6 +106,5 @@ parse = \parser -> | |||
| 105 | 106 | ||
| 106 | 107 | $(usageHelp parser.config subcommandPath parser.textStyle) | |
| 107 | 108 | """ | |
| 108 | - | ||
| 109 | 109 | Stdout.line! incorrectUsageMessage | |
| 110 | 110 | Task.err (Exit 1 "") | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,21 +3,22 @@ module [ | |||
| 3 | 3 | GetParamsAction, | |
| 4 | 4 | StopCollectingAction, | |
| 5 | 5 | CliBuilder, | |
| 6 | - fromState, | ||
| 7 | - addOptions, | ||
| 8 | - addParameters, | ||
| 6 | + fromArgParser, | ||
| 7 | + fromFullParser, | ||
| 8 | + addOption, | ||
| 9 | + addParameter, | ||
| 9 | 10 | addSubcommands, | |
| 10 | 11 | updateParser, | |
| 11 | - bindParser, | ||
| 12 | + map, | ||
| 13 | + combine, | ||
| 12 | 14 | intoParts, | |
| 13 | 15 | checkForHelpAndVersion, | |
| 14 | 16 | ] | |
| 15 | 17 | ||
| 16 | 18 | import Arg.Base exposing [ | |
| 17 | 19 | ArgParser, | |
| 18 | - ArgParserState, | ||
| 19 | - ArgParserResult, | ||
| 20 | 20 | onSuccessfulArgParse, | |
| 21 | + mapSuccessfullyParsed, | ||
| 21 | 22 | ArgExtractErr, | |
| 22 | 23 | OptionConfig, | |
| 23 | 24 | helpOption, | |
@@ -31,35 +32,49 @@ GetOptionsAction : { getOptions : {} } | |||
| 31 | 32 | GetParamsAction : { getParams : {} } | |
| 32 | 33 | StopCollectingAction : [] | |
| 33 | 34 | ||
| 34 | - CliBuilder state action := { | ||
| 35 | - parser : ArgParser state, | ||
| 35 | + CliBuilder data fromAction toAction := { | ||
| 36 | + parser : ArgParser data, | ||
| 36 | 37 | options : List OptionConfig, | |
| 37 | 38 | parameters : List ParameterConfig, | |
| 38 | 39 | subcommands : Dict Str SubcommandConfig, | |
| 39 | 40 | } | |
| 40 | 41 | ||
| 41 | - fromState : base -> CliBuilder base GetOptionsAction | ||
| 42 | - fromState = \base -> | ||
| 42 | + fromArgParser : (List Arg -> Result { data : data, remainingArgs : List Arg } ArgExtractErr) -> CliBuilder data fromAction toAction | ||
| 43 | + fromArgParser = \parser -> | ||
| 44 | + newParser = \{ args, subcommandPath } -> | ||
| 45 | + when parser args is | ||
| 46 | + Ok { data, remainingArgs } -> SuccessfullyParsed { data, remainingArgs, subcommandPath } | ||
| 47 | + Err err -> IncorrectUsage err { subcommandPath } | ||
| 48 | + | ||
| 49 | + @CliBuilder { | ||
| 50 | + parser: newParser, | ||
| 51 | + options: [], | ||
| 52 | + parameters: [], | ||
| 53 | + subcommands: Dict.empty {}, | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + fromFullParser : ArgParser data -> CliBuilder data fromAction toAction | ||
| 57 | + fromFullParser = \parser -> | ||
| 43 | 58 | @CliBuilder { | |
| 44 | - parser: \{ args, subcommandPath } -> SuccessfullyParsed { data: base, remainingArgs: args, subcommandPath }, | ||
| 59 | + parser, | ||
| 45 | 60 | options: [], | |
| 46 | 61 | parameters: [], | |
| 47 | 62 | subcommands: Dict.empty {}, | |
| 48 | 63 | } | |
| 49 | 64 | ||
| 50 | - addOptions : CliBuilder state action, List OptionConfig -> CliBuilder state action | ||
| 51 | - addOptions = \@CliBuilder builder, newOptions -> | ||
| 52 | - @CliBuilder { builder & options: List.concat builder.options newOptions } | ||
| 65 | + addOption : CliBuilder state fromAction toAction, OptionConfig -> CliBuilder state fromAction toAction | ||
| 66 | + addOption = \@CliBuilder builder, newOption -> | ||
| 67 | + @CliBuilder { builder & options: List.append builder.options newOption } | ||
| 53 | 68 | ||
| 54 | - addParameters : CliBuilder state action, List ParameterConfig -> CliBuilder state action | ||
| 55 | - addParameters = \@CliBuilder builder, newParameters -> | ||
| 56 | - @CliBuilder { builder & parameters: List.concat builder.parameters newParameters } | ||
| 69 | + addParameter : CliBuilder state fromAction toAction, ParameterConfig -> CliBuilder state fromAction toAction | ||
| 70 | + addParameter = \@CliBuilder builder, newParameter -> | ||
| 71 | + @CliBuilder { builder & parameters: List.append builder.parameters newParameter } | ||
| 57 | 72 | ||
| 58 | - addSubcommands : CliBuilder state action, Dict Str SubcommandConfig -> CliBuilder state action | ||
| 73 | + addSubcommands : CliBuilder state fromAction toAction, Dict Str SubcommandConfig -> CliBuilder state fromAction toAction | ||
| 59 | 74 | addSubcommands = \@CliBuilder builder, newSubcommands -> | |
| 60 | 75 | @CliBuilder { builder & subcommands: Dict.insertAll builder.subcommands newSubcommands } | |
| 61 | 76 | ||
| 62 | - setParser : CliBuilder state action, ArgParser nextState -> CliBuilder nextState nextAction | ||
| 77 | + setParser : CliBuilder state fromAction toAction, ArgParser nextState -> CliBuilder nextState fromAction toAction | ||
| 63 | 78 | setParser = \@CliBuilder builder, parser -> | |
| 64 | 79 | @CliBuilder { | |
| 65 | 80 | options: builder.options, | |
@@ -68,28 +83,19 @@ setParser = \@CliBuilder builder, parser -> | |||
| 68 | 83 | parser, | |
| 69 | 84 | } | |
| 70 | 85 | ||
| 71 | - updateParser : CliBuilder state action, ({ data : state, remainingArgs : List Arg } -> Result { data : nextState, remainingArgs : List Arg } ArgExtractErr) -> CliBuilder nextState nextAction | ||
| 86 | + updateParser : CliBuilder state fromAction toAction, ({ data : state, remainingArgs : List Arg } -> Result { data : nextState, remainingArgs : List Arg } ArgExtractErr) -> CliBuilder nextState fromAction toAction | ||
| 72 | 87 | updateParser = \@CliBuilder builder, updater -> | |
| 73 | 88 | newParser = | |
| 74 | - { data, remainingArgs, subcommandPath } <- onSuccessfulArgParse builder.parser | ||
| 75 | - when updater { data, remainingArgs } is | ||
| 76 | - Err err -> IncorrectUsage err { subcommandPath } | ||
| 77 | - Ok { data: updatedData, remainingArgs: restOfArgs } -> | ||
| 78 | - SuccessfullyParsed { data: updatedData, remainingArgs: restOfArgs, subcommandPath } | ||
| 79 | - | ||
| 80 | - setParser (@CliBuilder builder) newParser | ||
| 81 | - | ||
| 82 | - bindParser : CliBuilder state action, (ArgParserState state -> ArgParserResult (ArgParserState nextState)) -> CliBuilder nextState nextAction | ||
| 83 | - bindParser = \@CliBuilder builder, updater -> | ||
| 84 | - newParser : ArgParser nextState | ||
| 85 | - newParser = | ||
| 86 | - { data, remainingArgs, subcommandPath } <- onSuccessfulArgParse builder.parser | ||
| 87 | - updater { data, remainingArgs, subcommandPath } | ||
| 89 | + onSuccessfulArgParse builder.parser \{ data, remainingArgs, subcommandPath } -> | ||
| 90 | + when updater { data, remainingArgs } is | ||
| 91 | + Err err -> IncorrectUsage err { subcommandPath } | ||
| 92 | + Ok { data: updatedData, remainingArgs: restOfArgs } -> | ||
| 93 | + SuccessfullyParsed { data: updatedData, remainingArgs: restOfArgs, subcommandPath } | ||
| 88 | 94 | ||
| 89 | 95 | setParser (@CliBuilder builder) newParser | |
| 90 | 96 | ||
| 91 | 97 | intoParts : | |
| 92 | - CliBuilder state action | ||
| 98 | + CliBuilder state fromAction toAction | ||
| 93 | 99 | -> { | |
| 94 | 100 | parser : ArgParser state, | |
| 95 | 101 | options : List OptionConfig, | |
@@ -98,6 +104,41 @@ intoParts : | |||
| 98 | 104 | } | |
| 99 | 105 | intoParts = \@CliBuilder builder -> builder | |
| 100 | 106 | ||
| 107 | + map : CliBuilder a fromAction toAction, (a -> b) -> CliBuilder b fromAction toAction | ||
| 108 | + map = \@CliBuilder builder, mapper -> | ||
| 109 | + combinedParser = \input -> | ||
| 110 | + builder.parser input | ||
| 111 | + |> mapSuccessfullyParsed \{ data, remainingArgs, subcommandPath } -> | ||
| 112 | + { data: mapper data, remainingArgs, subcommandPath } | ||
| 113 | + | ||
| 114 | + @CliBuilder { | ||
| 115 | + parser: combinedParser, | ||
| 116 | + options: builder.options, | ||
| 117 | + parameters: builder.parameters, | ||
| 118 | + subcommands: builder.subcommands, | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + combine : CliBuilder a action1 action2, CliBuilder b action2 action3, (a, b -> c) -> CliBuilder c action1 action3 | ||
| 122 | + combine = \@CliBuilder left, @CliBuilder right, combiner -> | ||
| 123 | + combinedParser = | ||
| 124 | + onSuccessfulArgParse left.parser \firstResult -> | ||
| 125 | + innerParser = | ||
| 126 | + onSuccessfulArgParse right.parser \{ data: secondData, remainingArgs, subcommandPath } -> | ||
| 127 | + SuccessfullyParsed { | ||
| 128 | + data: combiner firstResult.data secondData, | ||
| 129 | + remainingArgs, | ||
| 130 | + subcommandPath, | ||
| 131 | + } | ||
| 132 | + | ||
| 133 | + innerParser { args: firstResult.remainingArgs, subcommandPath: firstResult.subcommandPath } | ||
| 134 | + | ||
| 135 | + @CliBuilder { | ||
| 136 | + parser: combinedParser, | ||
| 137 | + options: List.concat left.options right.options, | ||
| 138 | + parameters: List.concat left.parameters right.parameters, | ||
| 139 | + subcommands: Dict.insertAll left.subcommands right.subcommands, | ||
| 140 | + } | ||
| 141 | + | ||
| 101 | 142 | flagWasPassed : OptionConfig, List Arg -> Bool | |
| 102 | 143 | flagWasPassed = \option, args -> | |
| 103 | 144 | List.any args \arg -> | |
@@ -107,7 +148,7 @@ flagWasPassed = \option, args -> | |||
| 107 | 148 | Long long -> long.name == option.long | |
| 108 | 149 | Parameter _p -> Bool.false | |
| 109 | 150 | ||
| 110 | - checkForHelpAndVersion : CliBuilder state action -> CliBuilder state action | ||
| 151 | + checkForHelpAndVersion : CliBuilder state fromAction toAction -> CliBuilder state fromAction toAction | ||
| 111 | 152 | checkForHelpAndVersion = \@CliBuilder builder -> | |
| 112 | 153 | newParser = \{ args, subcommandPath } -> | |
| 113 | 154 | when builder.parser { args, subcommandPath } is | |
@@ -130,15 +171,15 @@ checkForHelpAndVersion = \@CliBuilder builder -> | |||
| 130 | 171 | ||
| 131 | 172 | expect | |
| 132 | 173 | { parser } = | |
| 133 | - fromState (\x -> { x }) | ||
| 134 | - |> updateParser \{ data, remainingArgs } -> Ok { data: data (Inspect.toStr remainingArgs), remainingArgs: [] } | ||
| 174 | + fromArgParser \args -> Ok { data: Inspect.toStr args, remainingArgs: [] } | ||
| 175 | + |> map Inspected | ||
| 135 | 176 | |> intoParts | |
| 136 | 177 | ||
| 137 | 178 | out = parser { args: [Parameter "123"], subcommandPath: [] } | |
| 138 | 179 | ||
| 139 | 180 | out | |
| 140 | 181 | == SuccessfullyParsed { | |
| 141 | - data: { x: "[(Parameter \"123\")]" }, | ||
| 182 | + data: Inspected "[(Parameter \"123\")]", | ||
| 142 | 183 | remainingArgs: [], | |
| 143 | 184 | subcommandPath: [], | |
| 144 | 185 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments