Skip to content

Commit

Permalink
Support default values for args (#4)
Browse files Browse the repository at this point in the history
* Support default values for args

* Fix CI tests
  • Loading branch information
smores56 authored Jul 30, 2024
1 parent c1a5215 commit bcf678d
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 108 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ jobs:
- run: ./roc_nightly/roc version

# run all tests
- run: for f in **/*.roc; do ./roc_nightly/roc test $f; done
- run: ./roc_nightly/roc test package/main.roc
19 changes: 16 additions & 3 deletions examples/default-values.roc
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,22 @@ main =

cliParser =
{ Cli.weave <-
alpha: Opt.maybeU64 { short: "a", long: "alpha", help: "Set the alpha level. [default: 123]" }
|> Cli.map \a -> Result.withDefault a 123,,
file: Param.maybeStr { name: "file", help: "The file to process. [default: NONE]" }
alpha: Opt.u64 {
short: "a",
long: "alpha",
help: "Set the alpha level. [default: 123]",
default: Value 123,
},
beta: Opt.dec {
short: "b",
long: "beta",
help: "Set the beta level. [default: PI]",
default: Generate (\{} -> Num.pi),
},
file: Param.maybeStr {
name: "file",
help: "The file to process. [default: NONE]",
}
|> Cli.map \f -> Result.withDefault f "NONE",
}
|> Cli.finish {
Expand Down
14 changes: 7 additions & 7 deletions examples/subcommands.roc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import pf.Task exposing [Task]
import weaver.Opt
import weaver.Cli
import weaver.Param
import weaver.Subcommand
import weaver.SubCmd

main =
args = Arg.list!
Expand All @@ -28,7 +28,7 @@ main =
cliParser =
{ Cli.weave <-
force: Opt.flag { short: "f", help: "Force the task to complete." },
sc: Subcommand.optional [subcommandParser1, subcommandParser2],
sc: SubCmd.optional [subcommandParser1, subcommandParser2],
file: Param.maybeStr { name: "file", help: "The file to process." },
files: Param.strList { name: "files", help: "The rest of the files." },
}
Expand All @@ -44,14 +44,14 @@ subcommandParser1 =
{ Cli.weave <-
d: Opt.maybeU64 { short: "d", help: "A non-overlapping subcommand flag with s2." },
volume: Opt.maybeU64 { short: "v", long: "volume", help: "How loud to grind the gears." },
sc: Subcommand.optional [subSubcommandParser1, subSubcommandParser2],
sc: SubCmd.optional [subSubcommandParser1, subSubcommandParser2],
}
|> Subcommand.finish { name: "s1", description: "A first subcommand.", mapper: S1 }
|> SubCmd.finish { name: "s1", description: "A first subcommand.", mapper: S1 }

subcommandParser2 =
Opt.maybeU64 { short: "d", help: "This doesn't overlap with s1's -d flag." }
|> Cli.map DFlag
|> Subcommand.finish {
|> SubCmd.finish {
name: "s2",
description: "Another subcommand.",
mapper: S2,
Expand All @@ -62,12 +62,12 @@ subSubcommandParser1 =
a: Opt.u64 { short: "a", help: "An example short flag for a sub-subcommand." },
b: Opt.u64 { short: "b", help: "Another example short flag for a sub-subcommand." },
}
|> Subcommand.finish { name: "ss1", description: "A sub-subcommand.", mapper: SS1 }
|> SubCmd.finish { name: "ss1", description: "A sub-subcommand.", mapper: SS1 }

subSubcommandParser2 =
{ Cli.weave <-
a: Opt.u64 { short: "a", help: "Set the alpha level." },
c: Opt.u64 { short: "c", long: "create", help: "Create a doohickey." },
data: Param.str { name: "data", help: "Data to manipulate." },
}
|> Subcommand.finish { name: "ss2", description: "Another sub-subcommand.", mapper: SS2 }
|> SubCmd.finish { name: "ss2", description: "Another sub-subcommand.", mapper: SS2 }
39 changes: 39 additions & 0 deletions package/Base.roc
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@ module [
Plurality,
SpecialFlags,
InvalidValue,
DefaultValue,
ValueParser,
OptionConfigBaseParams,
DefaultableOptionConfigBaseParams,
OptionConfigParams,
DefaultableOptionConfigParams,
OptionConfig,
helpOption,
versionOption,
ParameterConfigBaseParams,
DefaultableParameterConfigBaseParams,
ParameterConfigParams,
DefaultableParameterConfigParams,
ParameterConfig,
CliConfigParams,
CliConfig,
Expand Down Expand Up @@ -110,6 +115,8 @@ SpecialFlags : { help : Bool, version : Bool }

InvalidValue : [InvalidNumStr, InvalidValue Str]

DefaultValue a : [NoDefault, Value a, Generate ({} -> a)]

## A parser that extracts an argument value from a string.
ValueParser a : Str -> Result a InvalidValue

Expand All @@ -119,6 +126,13 @@ OptionConfigBaseParams : {
help ? Str,
}

DefaultableOptionConfigBaseParams a : {
short ? Str,
long ? Str,
help ? Str,
default ? DefaultValue a,
}

## Default-value options for creating an option.
OptionConfigParams a : {
short ? Str,
Expand All @@ -128,6 +142,16 @@ OptionConfigParams a : {
parser : ValueParser a,
}

## Default-value options for creating an option.
DefaultableOptionConfigParams a : {
short ? Str,
long ? Str,
help ? Str,
type : Str,
parser : ValueParser a,
default ? DefaultValue a,
}

## Metadata for options in our CLI building system.
OptionConfig : {
expectedValue : ExpectedValue,
Expand Down Expand Up @@ -162,6 +186,12 @@ ParameterConfigBaseParams : {
help ? Str,
}

DefaultableParameterConfigBaseParams a : {
name : Str,
help ? Str,
default ? DefaultValue a,
}

## Default-value options for creating an parameter.
ParameterConfigParams a : {
name : Str,
Expand All @@ -170,6 +200,15 @@ ParameterConfigParams a : {
parser : ValueParser a,
}

## Default-value options for creating an parameter.
DefaultableParameterConfigParams a : {
name : Str,
help ? Str,
type : Str,
parser : ValueParser a,
default ? DefaultValue a,
}

## Metadata for parameters in our CLI building system.
ParameterConfig : {
name : Str,
Expand Down
14 changes: 7 additions & 7 deletions package/Builder.roc
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,20 @@ setParser = \@CliBuilder builder, parser ->
updateParser : CliBuilder state fromAction toAction, ({ data : state, remainingArgs : List Arg } -> Result { data : nextState, remainingArgs : List Arg } ArgExtractErr) -> CliBuilder nextState fromAction toAction
updateParser = \@CliBuilder builder, updater ->
newParser =
{ data, remainingArgs, subcommandPath } <- onSuccessfulArgParse builder.parser
when updater { data, remainingArgs } is
Err err -> IncorrectUsage err { subcommandPath }
Ok { data: updatedData, remainingArgs: restOfArgs } ->
SuccessfullyParsed { data: updatedData, remainingArgs: restOfArgs, subcommandPath }
onSuccessfulArgParse builder.parser \{ data, remainingArgs, subcommandPath } ->
when updater { data, remainingArgs } is
Err err -> IncorrectUsage err { subcommandPath }
Ok { data: updatedData, remainingArgs: restOfArgs } ->
SuccessfullyParsed { data: updatedData, remainingArgs: restOfArgs, subcommandPath }

setParser (@CliBuilder builder) newParser

bindParser : CliBuilder state fromAction toAction, (ArgParserState state -> ArgParserResult (ArgParserState nextState)) -> CliBuilder nextState fromAction toAction
bindParser = \@CliBuilder builder, updater ->
newParser : ArgParser nextState
newParser =
{ data, remainingArgs, subcommandPath } <- onSuccessfulArgParse builder.parser
updater { data, remainingArgs, subcommandPath }
onSuccessfulArgParse builder.parser \{ data, remainingArgs, subcommandPath } ->
updater { data, remainingArgs, subcommandPath }

setParser (@CliBuilder builder) newParser

Expand Down
16 changes: 8 additions & 8 deletions package/Cli.roc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
## Weave together a CLI parser using the `<- ` builder notation!
## Weave together a CLI parser using the `<-` builder notation!
##
## This module is the entry point for creating CLIs using Weaver.
## To get started, call the [weave] method and pass a
## [record builder](https://www.roc-lang.org/examples/RecordBuilder/README.html)
## to it. You can pass `Opt`s, `Param`s, or `Subcommand`s as fields,
## to it. You can pass `Opt`s, `Param`s, or `SubCmd`s as fields,
## and Weaver will automatically register them in its config as
## well as build a parser with the inferred types of the fields
## you set.
Expand All @@ -28,7 +28,7 @@
## ```roc
## fooSubcommand =
## Opt.u64 { short: "a", help: "Set the alpha level" }
## |> Subcommand.finish {
## |> SubCmd.finish {
## name: "foo",
## description: "Foo some stuff."
## mapper: Foo,
Expand All @@ -38,15 +38,15 @@
## # We allow two subcommands of the same parent to have overlapping
## # fields since only one can ever be parsed at a time.
## Opt.u64 { short: "a", help: "Set the alpha level" }
## |> Subcommand.finish {
## |> SubCmd.finish {
## name: "bar",
## description: "Bar some stuff."
## mapper: Bar,
## }
##
## { Cli.weave <-
## verbosity: Opt.count { short: "v", long: "verbose" },
## sc: Subcommand.optional [fooSubcommand, barSubcommand],
## sc: SubCmd.optional [fooSubcommand, barSubcommand],
## }
## ```
##
Expand Down Expand Up @@ -85,8 +85,8 @@
## If you want to see more examples, check the [examples](https://github.com/smores56/weaver/tree/main/examples)
## folder in the [repository](https://github.com/smores56/weaver).
##
## _note: `Opt`s must be set before an optional `Subcommand` field is given,_
## _and the `Subcommand` field needs to be set before `Param`s are set._
## _note: `Opt`s must be set before an optional `SubCmd` field is given,_
## _and the `SubCmd` field needs to be set before `Param`s are set._
## _`Param` lists also cannot be followed by anything else including_
## _themselves. These requirements ensure we parse arguments in the_
## _right order. Luckily, all of this is ensured at the type level._
Expand Down Expand Up @@ -149,7 +149,7 @@ map : CliBuilder a fromAction toAction, (a -> b) -> CliBuilder b fromAction toAc
map = \builder, mapper ->
Builder.map builder mapper

## Begin weaving together a CLI builder using the `<- ` builder notation.
## Begin weaving together a CLI builder using the `<-` builder notation.
##
## Check the module-level documentation for general usage instructions.
##
Expand Down
Loading

0 comments on commit bcf678d

Please sign in to comment.