Skip to content

Commit

Permalink
Move to snake_case and PNC
Browse files Browse the repository at this point in the history
  • Loading branch information
smores56 committed Jan 7, 2025
1 parent b91f309 commit 088a6fc
Show file tree
Hide file tree
Showing 19 changed files with 1,654 additions and 1,658 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,30 @@ import weaver.Param
main! = \args ->
data =
Cli.parse_or_display_message cli_parser args Arg.to_os_raw
|> try Result.onErr! \message ->
try Stdout.line! message
Err (Exit 1 "")
Cli.parse_or_display_message(cli_parser, args, Arg.to_os_raw)
|> try(Result.on_err!(\message ->
try(Stdout.line!(message))
Err(Exit(1, ""))))
try Stdout.line! "Successfully parsed! Here's what I got:"
try Stdout.line! ""
try Stdout.line! (Inspect.toStr data)
try(Stdout.line!("Successfully parsed! Here's what I got:"))
try(Stdout.line!(""))
try(Stdout.line!(Inspect.to_str(data)))
Ok {}
Ok({})
cli_parser =
{ Cli.weave <-
alpha: Opt.u64 { short: "a", help: "Set the alpha level." },
force: Opt.flag { short: "f", help: "Force the task to complete." },
file: Param.maybe_str { name: "file", help: "The file to process." },
files: Param.str_list { name: "files", help: "The rest of the files." },
alpha: Opt.u64({ short: "a", help: "Set the alpha level." }),
force: Opt.flag({ short: "f", help: "Force the task to complete." }),
file: Param.maybe_str({ name: "file", help: "The file to process." }),
files: Param.str_list({ name: "files", help: "The rest of the files." }),
}
|> Cli.finish {
|> Cli.finish({
name: "basic",
version: "v0.1.0",
authors: ["Some One <[email protected]>"],
description: "This is a basic example of what you can build with Weaver. You get safe parsing, useful error messages, and help pages all for free!",
}
})
|> Cli.assert_valid
```

Expand Down
28 changes: 14 additions & 14 deletions examples/basic.roc
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@ import weaver.Param

main! = \args ->
data =
Cli.parse_or_display_message cli_parser args Arg.to_os_raw
|> try Result.onErr! \message ->
try Stdout.line! message
Err (Exit 1 "")
Cli.parse_or_display_message(cli_parser, args, Arg.to_os_raw)
|> try(Result.on_err!, \message ->
try(Stdout.line!, message)
Err(Exit(1, "")))

try Stdout.line! "Successfully parsed! Here's what I got:"
try Stdout.line! ""
try Stdout.line! (Inspect.toStr data)
try(Stdout.line!, "Successfully parsed! Here's what I got:")
try(Stdout.line!, "")
try(Stdout.line!, Inspect.to_str(data))

Ok {}
Ok({})

cli_parser =
{ Cli.weave <-
alpha: Opt.u64 { short: "a", help: "Set the alpha level." },
force: Opt.flag { short: "f", help: "Force the task to complete." },
file: Param.maybe_str { name: "file", help: "The file to process." },
files: Param.str_list { name: "files", help: "The rest of the files." },
alpha: Opt.u64({ short: "a", help: "Set the alpha level." }),
force: Opt.flag({ short: "f", help: "Force the task to complete." }),
file: Param.maybe_str({ name: "file", help: "The file to process." }),
files: Param.str_list({ name: "files", help: "The rest of the files." }),
}
|> Cli.finish {
|> Cli.finish({
name: "basic",
version: "v0.0.1",
authors: ["Some One <[email protected]>"],
description: "This is a basic example of what you can build with Weaver. You get safe parsing, useful error messages, and help pages all for free!",
}
})
|> Cli.assert_valid
38 changes: 19 additions & 19 deletions examples/default-values.roc
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,39 @@ import weaver.Param

main! = \args ->
data =
Cli.parse_or_display_message cli_parser args Arg.to_os_raw
|> try Result.onErr! \message ->
try Stdout.line! message
Err (Exit 1 "")
Cli.parse_or_display_message(cli_parser, args, Arg.to_os_raw)
|> try(Result.on_err!, \message ->
try(Stdout.line!, message)
Err(Exit(1, "")))

try Stdout.line! "Successfully parsed! Here's what I got:"
try Stdout.line! ""
try Stdout.line! (Inspect.toStr data)
try(Stdout.line!, "Successfully parsed! Here's what I got:")
try(Stdout.line!, "")
try(Stdout.line!, Inspect.to_str(data))

Ok {}
Ok({})

cli_parser =
{ Cli.weave <-
alpha: Opt.u64 {
alpha: Opt.u64({
short: "a",
long: "alpha",
help: "Set the alpha level. [default: 123]",
default: Value 123,
},
beta: Opt.dec {
default: Value(123),
}),
beta: Opt.dec({
short: "b",
long: "beta",
help: "Set the beta level. [default: PI]",
default: Generate (\{} -> Num.pi),
},
file: Param.maybe_str {
default: Generate(\{} -> Num.pi),
}),
file: Param.maybe_str({
name: "file",
help: "The file to process. [default: NONE]",
}
|> Cli.map \f -> Result.with_default f "NONE",
})
|> Cli.map(\f -> Result.with_default(f, "NONE")),
}
|> Cli.finish {
|> Cli.finish({
name: "default-values",
version: "v0.0.1",
}
})
|> Cli.assert_valid
24 changes: 12 additions & 12 deletions examples/single-arg.roc
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ import weaver.Cli

main! = \args ->
data =
Cli.parse_or_display_message cli_parser args Arg.to_os_raw
|> try Result.onErr! \message ->
try Stdout.line! message
Err (Exit 1 "")
Cli.parse_or_display_message(cli_parser, args, Arg.to_os_raw)
|> try(Result.on_err!, \message ->
try(Stdout.line!, message)
Err(Exit(1, "")))

try Stdout.line! "Successfully parsed! Here's what I got:"
try Stdout.line! ""
try Stdout.line! (Inspect.toStr data)
try(Stdout.line!, "Successfully parsed! Here's what I got:")
try(Stdout.line!, "")
try(Stdout.line!, Inspect.to_str(data))

Ok {}
Ok({})

cli_parser =
Opt.u64 { short: "a", long: "alpha", help: "Set the alpha level." }
|> Cli.map Alpha
|> Cli.finish {
Opt.u64({ short: "a", long: "alpha", help: "Set the alpha level." })
|> Cli.map(Alpha)
|> Cli.finish({
name: "single-arg",
version: "v0.0.1",
}
})
|> Cli.assert_valid
58 changes: 29 additions & 29 deletions examples/subcommands.roc
Original file line number Diff line number Diff line change
Expand Up @@ -12,60 +12,60 @@ import weaver.SubCmd

main! = \args ->
data =
Cli.parse_or_display_message cli_parser args Arg.to_os_raw
|> try Result.onErr! \message ->
try Stdout.line! message
Err (Exit 1 "")
Cli.parse_or_display_message(cli_parser, args, Arg.to_os_raw)
|> try(Result.on_err!, \message ->
try(Stdout.line!, message)
Err(Exit(1, "")))

try Stdout.line! "Successfully parsed! Here's what I got:"
try Stdout.line! ""
try Stdout.line! (Inspect.toStr data)
try(Stdout.line!, "Successfully parsed! Here's what I got:")
try(Stdout.line!, "")
try(Stdout.line!, Inspect.to_str(data))

Ok {}
Ok({})

cli_parser =
{ Cli.weave <-
force: Opt.flag { short: "f", help: "Force the task to complete." },
sc: SubCmd.optional [subcommand_parser1, subcommand_parser2],
file: Param.maybe_str { name: "file", help: "The file to process." },
files: Param.str_list { name: "files", help: "The rest of the files." },
force: Opt.flag({ short: "f", help: "Force the task to complete." }),
sc: SubCmd.optional([subcommand_parser1, subcommand_parser2]),
file: Param.maybe_str({ name: "file", help: "The file to process." }),
files: Param.str_list({ name: "files", help: "The rest of the files." }),
}
|> Cli.finish {
|> Cli.finish({
name: "subcommands",
version: "v0.0.1",
authors: ["Some One <[email protected]>"],
description: "This is a basic example of what you can build with Weaver. You get safe parsing, useful error messages, and help pages all for free!",
}
})
|> Cli.assert_valid

subcommand_parser1 =
{ Cli.weave <-
d: Opt.maybe_u64 { short: "d", help: "A non-overlapping subcommand flag with s2." },
volume: Opt.maybe_u64 { short: "v", long: "volume", help: "How loud to grind the gears." },
sc: SubCmd.optional [sub_subcommand_parser1, sub_subcommand_parser2],
d: Opt.maybe_u64({ short: "d", help: "A non-overlapping subcommand flag with s2." }),
volume: Opt.maybe_u64({ short: "v", long: "volume", help: "How loud to grind the gears." }),
sc: SubCmd.optional([sub_subcommand_parser1, sub_subcommand_parser2]),
}
|> SubCmd.finish { name: "s1", description: "A first subcommand.", mapper: S1 }
|> SubCmd.finish({ name: "s1", description: "A first subcommand.", mapper: S1 })

subcommand_parser2 =
Opt.maybe_u64 { short: "d", help: "This doesn't overlap with s1's -d flag." }
|> Cli.map DFlag
|> SubCmd.finish {
Opt.maybe_u64({ short: "d", help: "This doesn't overlap with s1's -d flag." })
|> Cli.map(DFlag)
|> SubCmd.finish({
name: "s2",
description: "Another subcommand.",
mapper: S2,
}
})

sub_subcommand_parser1 =
{ Cli.weave <-
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." },
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." }),
}
|> SubCmd.finish { name: "ss1", description: "A sub-subcommand.", mapper: SS1 }
|> SubCmd.finish({ name: "ss1", description: "A sub-subcommand.", mapper: SS1 })

sub_subcommand_parser2 =
{ 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." },
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." }),
}
|> SubCmd.finish { name: "ss2", description: "Another sub-subcommand.", mapper: SS2 }
|> SubCmd.finish({ name: "ss2", description: "Another sub-subcommand.", mapper: SS2 })
58 changes: 29 additions & 29 deletions package/Arg.roc
Original file line number Diff line number Diff line change
Expand Up @@ -17,82 +17,82 @@ module [
## Most of the time, you will pass these to packages and they will handle the
## encoding for you, but for quick-and-dirty code you can use [display] to
## convert these to [Str] in a lossy way.
Arg := [Unix (List U8), Windows (List U16)] implements [Eq, Inspect { toInspector: arg_inspector }]
Arg := [Unix (List U8), Windows (List U16)] implements [Eq, Inspect { to_inspector: arg_inspector }]

arg_inspector : Arg -> Inspector f where f implements InspectFormatter
arg_inspector = \arg -> Inspect.str (display arg)
arg_inspector = \arg -> Inspect.str(display(arg))

## Wrap a raw, OS-aware numeric list into an [Arg].
from_raw_arg : [Unix (List U8), Windows (List U16)] -> Arg
from_raw_arg = \raw_arg -> @Arg raw_arg
from_raw_arg = \raw_arg -> @Arg(raw_arg)

## Unwrap an [Arg] into a raw, OS-aware numeric list.
##
## This is a good way to pass [Arg]s to Roc packages.
to_raw_arg : Arg -> [Unix (List U8), Windows (List U16)]
to_raw_arg = \@Arg raw_arg -> raw_arg
to_raw_arg = \@Arg(raw_arg) -> raw_arg

## Encode a UTF-8 [Str] to a Unix-flavored [Arg].
from_str : Str -> Arg
from_str = \str ->
@Arg (Unix (Str.toUtf8 str))
@Arg(Unix(Str.to_utf8(str)))

## Attempt to decode an [Arg] to a UTF-8 [Str].
to_str : Arg -> Result Str [InvalidUtf8]
to_str = \@Arg arg ->
to_str = \@Arg(arg) ->
# TODO: update when Unicode -> Str conversion is ready:
# https://github.com/roc-lang/roc/issues/7390
when arg is
Unix unix ->
Str.fromUtf8 unix
|> Result.mapErr \_err -> InvalidUtf8
Unix(unix) ->
Str.from_utf8(unix)
|> Result.map_err(\_err -> InvalidUtf8)

Windows _windows -> Err InvalidUtf8
Windows(_windows) -> Err(InvalidUtf8)

## Convert an [Arg] to a list of bytes.
to_bytes : Arg -> List U8
to_bytes = \@Arg arg ->
to_bytes = \@Arg(arg) ->
when arg is
Unix unix -> unix
Windows windows ->
Unix(unix) -> unix
Windows(windows) ->
# avoid intermediate list resizing allocations by
# appending to a list instead of using `List.join_map`
helper = \codepoints, bytes ->
when codepoints is
[] -> bytes
[codepoint, .. as rest] ->
lower = codepoint |> Num.toU8
lower = codepoint |> Num.to_u8
upper =
codepoint
|> Num.shiftRightBy 8
|> Num.toU8
|> Num.shift_right_by(8)
|> Num.to_u8

updatedBytes =
updated_bytes =
bytes
|> List.append upper
|> List.append lower
|> List.append(upper)
|> List.append(lower)

helper rest updatedBytes
helper(rest, updated_bytes)

bytesOut = List.withCapacity (2 * List.len windows)
bytes_out = List.with_capacity((2 * List.len(windows)))

helper windows bytesOut
helper(windows, bytes_out)

## Convert an Arg to a `Str` for display purposes.
##
## NB: This currently only supports valid UTF-8 Unix strings. Once Roc adds
## support for lossy conversion of Unicode to Str, this will replace invalid
## codepoints with the Unicode replacement character "\uFFFD".
display : Arg -> Str
display = \@Arg arg ->
display = \@Arg(arg) ->
# TODO: update when Unicode -> Str conversion is ready:
# https://github.com/roc-lang/roc/issues/7390
when arg is
Unix unix ->
when Str.fromUtf8 unix is
Ok str -> str
Err err -> crash "Invalid UTF-8 string: $(Inspect.toStr err)"
Unix(unix) ->
when Str.from_utf8(unix) is
Ok(str) -> str
Err(err) -> crash("Invalid UTF-8 string: $(Inspect.to_str(err))")
Windows _windows ->
crash "Windows args cannot currently be displayed"
Windows(_windows) ->
crash("Windows args cannot currently be displayed")
Loading

0 comments on commit 088a6fc

Please sign in to comment.