FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Merge pull request #227 from smores56/weaver-0.3 · codegod100/basic-cli@d9ffc22 · GitHub

Commit d9ffc22

Browse files
Merge pull request roc-lang#227 from smores56/weaver-0.3
Update Arg module to new record builder syntax
2 parents 5abb142 + 114858f commit d9ffc22

10 files changed

Lines changed: 588 additions & 711 deletions

File tree

‎examples/args.roc‎

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import pf.Stdout
66
import pf.Stderr
77
import pf.Task exposing [Task]
88
import pf.Arg.Cli as Cli
9-
import pf.Arg.Subcommand as Subcommand
9+
import pf.Arg.SubCmd as SubCmd
1010
import pf.Arg.Opt as Opt
1111
import pf.Arg.Param as Param
1212
import pf.Arg
@@ -15,7 +15,7 @@ main =
1515
args = Arg.list! {}
1616

1717
when Cli.parseOrDisplayMessage cli args is
18-
Ok { command: subcommand } ->
18+
Ok subcommand ->
1919
mathOutcome =
2020
when subcommand is
2121
Max { first, rest } ->
@@ -34,9 +34,7 @@ main =
3434
Task.err (Exit 1 "")
3535

3636
cli =
37-
Cli.build {
38-
command: <- Subcommand.required [maxSubcommand, divideSubcommand],
39-
}
37+
SubCmd.required [maxSubcommand, divideSubcommand]
4038
|> Cli.finish {
4139
name: "args-example",
4240
description: "A calculator example of the CLI platform argument parser.",
@@ -45,33 +43,31 @@ cli =
4543
|> Cli.assertValid
4644

4745
maxSubcommand =
48-
Cli.build {
46+
{ Cli.combine <-
4947
# 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." },
5250
}
53-
|> Subcommand.finish {
51+
|> SubCmd.finish {
5452
name: "max",
5553
description: "Find the largest of multiple numbers.",
5654
mapper: Max,
5755
}
5856

5957
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+
},
7369
}
74-
|> Subcommand.finish {
70+
|> SubCmd.finish {
7571
name: "div",
7672
description: "Divide two numbers.",
7773
mapper: Div,

‎examples/record-builder.roc‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import pf.Task exposing [Task]
77

88
main =
99
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,
1313
}
1414

1515
{ apples, oranges } = myrecord!
@@ -26,3 +26,10 @@ getFruit = \request ->
2626
when request is
2727
Apples -> Task.ok ["Granny Smith", "Pink Lady", "Golden Delicious"]
2828
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)

‎platform/Arg.roc‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ list = \_ ->
3232
##
3333
## ```roc
3434
## 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." },
3738
## }
3839
## |> Cli.finish {
3940
## name: "example",
@@ -56,6 +57,7 @@ list = \_ ->
5657
##
5758
## Options:
5859
## -v How verbose our logs should be.
60+
## -a Set the alpha level.
5961
## -h, --help Show this help page.
6062
## -V, --version Show the version.
6163
## """
@@ -90,7 +92,6 @@ parse = \parser ->
9092
ShowHelp { subcommandPath } ->
9193
helpMessage =
9294
helpText parser.config subcommandPath parser.textStyle
93-
9495
Stdout.line! helpMessage
9596
Task.err (Exit 0 "")
9697

@@ -105,6 +106,5 @@ parse = \parser ->
105106
106107
$(usageHelp parser.config subcommandPath parser.textStyle)
107108
"""
108-
109109
Stdout.line! incorrectUsageMessage
110110
Task.err (Exit 1 "")

‎platform/Arg/Builder.roc‎

Lines changed: 80 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@ module [
33
GetParamsAction,
44
StopCollectingAction,
55
CliBuilder,
6-
fromState,
7-
addOptions,
8-
addParameters,
6+
fromArgParser,
7+
fromFullParser,
8+
addOption,
9+
addParameter,
910
addSubcommands,
1011
updateParser,
11-
bindParser,
12+
map,
13+
combine,
1214
intoParts,
1315
checkForHelpAndVersion,
1416
]
1517

1618
import Arg.Base exposing [
1719
ArgParser,
18-
ArgParserState,
19-
ArgParserResult,
2020
onSuccessfulArgParse,
21+
mapSuccessfullyParsed,
2122
ArgExtractErr,
2223
OptionConfig,
2324
helpOption,
@@ -31,35 +32,49 @@ GetOptionsAction : { getOptions : {} }
3132
GetParamsAction : { getParams : {} }
3233
StopCollectingAction : []
3334

34-
CliBuilder state action := {
35-
parser : ArgParser state,
35+
CliBuilder data fromAction toAction := {
36+
parser : ArgParser data,
3637
options : List OptionConfig,
3738
parameters : List ParameterConfig,
3839
subcommands : Dict Str SubcommandConfig,
3940
}
4041

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 ->
4358
@CliBuilder {
44-
parser: \{ args, subcommandPath } -> SuccessfullyParsed { data: base, remainingArgs: args, subcommandPath },
59+
parser,
4560
options: [],
4661
parameters: [],
4762
subcommands: Dict.empty {},
4863
}
4964

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 }
5368

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 }
5772

58-
addSubcommands : CliBuilder state action, Dict Str SubcommandConfig -> CliBuilder state action
73+
addSubcommands : CliBuilder state fromAction toAction, Dict Str SubcommandConfig -> CliBuilder state fromAction toAction
5974
addSubcommands = \@CliBuilder builder, newSubcommands ->
6075
@CliBuilder { builder & subcommands: Dict.insertAll builder.subcommands newSubcommands }
6176

62-
setParser : CliBuilder state action, ArgParser nextState -> CliBuilder nextState nextAction
77+
setParser : CliBuilder state fromAction toAction, ArgParser nextState -> CliBuilder nextState fromAction toAction
6378
setParser = \@CliBuilder builder, parser ->
6479
@CliBuilder {
6580
options: builder.options,
@@ -68,28 +83,19 @@ setParser = \@CliBuilder builder, parser ->
6883
parser,
6984
}
7085

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
7287
updateParser = \@CliBuilder builder, updater ->
7388
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 }
8894

8995
setParser (@CliBuilder builder) newParser
9096

9197
intoParts :
92-
CliBuilder state action
98+
CliBuilder state fromAction toAction
9399
-> {
94100
parser : ArgParser state,
95101
options : List OptionConfig,
@@ -98,6 +104,41 @@ intoParts :
98104
}
99105
intoParts = \@CliBuilder builder -> builder
100106

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+
101142
flagWasPassed : OptionConfig, List Arg -> Bool
102143
flagWasPassed = \option, args ->
103144
List.any args \arg ->
@@ -107,7 +148,7 @@ flagWasPassed = \option, args ->
107148
Long long -> long.name == option.long
108149
Parameter _p -> Bool.false
109150

110-
checkForHelpAndVersion : CliBuilder state action -> CliBuilder state action
151+
checkForHelpAndVersion : CliBuilder state fromAction toAction -> CliBuilder state fromAction toAction
111152
checkForHelpAndVersion = \@CliBuilder builder ->
112153
newParser = \{ args, subcommandPath } ->
113154
when builder.parser { args, subcommandPath } is
@@ -130,15 +171,15 @@ checkForHelpAndVersion = \@CliBuilder builder ->
130171

131172
expect
132173
{ 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
135176
|> intoParts
136177

137178
out = parser { args: [Parameter "123"], subcommandPath: [] }
138179

139180
out
140181
== SuccessfullyParsed {
141-
data: { x: "[(Parameter \"123\")]" },
182+
data: Inspected "[(Parameter \"123\")]",
142183
remainingArgs: [],
143184
subcommandPath: [],
144185
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL