mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-27 22:09:09 +00:00
Update Args.roc
This commit is contained in:
parent
b72ee8a68e
commit
140c33aa50
1 changed files with 14 additions and 14 deletions
|
@ -127,7 +127,7 @@ findOneArg : Str, Str, List Str -> Result Str [NotFound]*
|
||||||
findOneArg = \long, short, args ->
|
findOneArg = \long, short, args ->
|
||||||
argMatches = \arg ->
|
argMatches = \arg ->
|
||||||
if arg == "--\(long)" then
|
if arg == "--\(long)" then
|
||||||
True
|
Bool.true
|
||||||
else
|
else
|
||||||
Bool.not (Str.isEmpty short) && arg == "-\(short)"
|
Bool.not (Str.isEmpty short) && arg == "-\(short)"
|
||||||
|
|
||||||
|
@ -314,15 +314,15 @@ parseHelp = \@Parser parser, args ->
|
||||||
parseHelp parser2 args
|
parseHelp parser2 args
|
||||||
|
|
||||||
## Creates a parser for a boolean flag argument.
|
## Creates a parser for a boolean flag argument.
|
||||||
## Flags of value "true" and "false" will be parsed as [True] and [False], respectively.
|
## Flags of value "true" and "false" will be parsed as [Bool.true] and [Bool.false], respectively.
|
||||||
## All other values will result in a `WrongType` error.
|
## All other values will result in a `WrongType` error.
|
||||||
bool : _ -> Parser Bool # TODO: panics if parameter annotation given
|
bool : _ -> Parser Bool # TODO: panics if parameter annotation given
|
||||||
bool = \{ long, short ? "", help ? "" } ->
|
bool = \{ long, short ? "", help ? "" } ->
|
||||||
fn = \args ->
|
fn = \args ->
|
||||||
when findOneArg long short args is
|
when findOneArg long short args is
|
||||||
Err NotFound -> Err NotFound
|
Err NotFound -> Err NotFound
|
||||||
Ok "true" -> Ok True
|
Ok "true" -> Ok Bool.true
|
||||||
Ok "false" -> Ok False
|
Ok "false" -> Ok Bool.false
|
||||||
Ok _ -> Err WrongType
|
Ok _ -> Err WrongType
|
||||||
|
|
||||||
@Parser (Arg { long, short, help, type: Bool } fn)
|
@Parser (Arg { long, short, help, type: Bool } fn)
|
||||||
|
@ -523,13 +523,13 @@ expect
|
||||||
expect
|
expect
|
||||||
parser = bool { long: "foo" }
|
parser = bool { long: "foo" }
|
||||||
|
|
||||||
parseHelp parser ["--foo", "true"] == Ok True
|
parseHelp parser ["--foo", "true"] == Ok Bool.true
|
||||||
|
|
||||||
# bool dashed long argument with value is determined false
|
# bool dashed long argument with value is determined false
|
||||||
expect
|
expect
|
||||||
parser = bool { long: "foo" }
|
parser = bool { long: "foo" }
|
||||||
|
|
||||||
parseHelp parser ["--foo", "false"] == Ok False
|
parseHelp parser ["--foo", "false"] == Ok Bool.false
|
||||||
|
|
||||||
# bool dashed long argument with value is determined wrong type
|
# bool dashed long argument with value is determined wrong type
|
||||||
expect
|
expect
|
||||||
|
@ -541,13 +541,13 @@ expect
|
||||||
expect
|
expect
|
||||||
parser = bool { long: "foo", short: "F" }
|
parser = bool { long: "foo", short: "F" }
|
||||||
|
|
||||||
parseHelp parser ["-F", "true"] == Ok True
|
parseHelp parser ["-F", "true"] == Ok Bool.true
|
||||||
|
|
||||||
# bool dashed short argument with value is determined false
|
# bool dashed short argument with value is determined false
|
||||||
expect
|
expect
|
||||||
parser = bool { long: "foo", short: "F" }
|
parser = bool { long: "foo", short: "F" }
|
||||||
|
|
||||||
parseHelp parser ["-F", "false"] == Ok False
|
parseHelp parser ["-F", "false"] == Ok Bool.false
|
||||||
|
|
||||||
# bool dashed short argument with value is determined wrong type
|
# bool dashed short argument with value is determined wrong type
|
||||||
expect
|
expect
|
||||||
|
@ -644,7 +644,7 @@ expect
|
||||||
parser = bool { long: "foo" }
|
parser = bool { long: "foo" }
|
||||||
|
|
||||||
when parseHelp parser ["foo"] is
|
when parseHelp parser ["foo"] is
|
||||||
Ok _ -> False
|
Ok _ -> Bool.false
|
||||||
Err e ->
|
Err e ->
|
||||||
err = formatError e
|
err = formatError e
|
||||||
|
|
||||||
|
@ -655,7 +655,7 @@ expect
|
||||||
parser = bool { long: "foo" }
|
parser = bool { long: "foo" }
|
||||||
|
|
||||||
when parseHelp parser ["--foo", "12"] is
|
when parseHelp parser ["--foo", "12"] is
|
||||||
Ok _ -> False
|
Ok _ -> Bool.false
|
||||||
Err e ->
|
Err e ->
|
||||||
err = formatError e
|
err = formatError e
|
||||||
|
|
||||||
|
@ -747,7 +747,7 @@ expect
|
||||||
|
|
||||||
when parse parser ["test", "login", "--pw", "123", "--user", "abc"] is
|
when parse parser ["test", "login", "--pw", "123", "--user", "abc"] is
|
||||||
Ok result -> result == "logging in abc with 123"
|
Ok result -> result == "logging in abc with 123"
|
||||||
Err _ -> False
|
Err _ -> Bool.false
|
||||||
|
|
||||||
# subcommand of subcommand parser
|
# subcommand of subcommand parser
|
||||||
expect
|
expect
|
||||||
|
@ -765,7 +765,7 @@ expect
|
||||||
|
|
||||||
when parse parser ["test", "auth", "login", "--pw", "123", "--user", "abc"] is
|
when parse parser ["test", "auth", "login", "--pw", "123", "--user", "abc"] is
|
||||||
Ok result -> result == "logging in abc with 123"
|
Ok result -> result == "logging in abc with 123"
|
||||||
Err _ -> False
|
Err _ -> Bool.false
|
||||||
|
|
||||||
# subcommand not provided
|
# subcommand not provided
|
||||||
expect
|
expect
|
||||||
|
@ -773,7 +773,7 @@ expect
|
||||||
choice [subCommand (succeed "") "auth", subCommand (succeed "") "publish"]
|
choice [subCommand (succeed "") "auth", subCommand (succeed "") "publish"]
|
||||||
|
|
||||||
when parseHelp parser [] is
|
when parseHelp parser [] is
|
||||||
Ok _ -> True
|
Ok _ -> Bool.true
|
||||||
Err e ->
|
Err e ->
|
||||||
err = formatError e
|
err = formatError e
|
||||||
|
|
||||||
|
@ -791,7 +791,7 @@ expect
|
||||||
choice [subCommand (succeed "") "auth", subCommand (succeed "") "publish"]
|
choice [subCommand (succeed "") "auth", subCommand (succeed "") "publish"]
|
||||||
|
|
||||||
when parseHelp parser ["logs"] is
|
when parseHelp parser ["logs"] is
|
||||||
Ok _ -> True
|
Ok _ -> Bool.true
|
||||||
Err e ->
|
Err e ->
|
||||||
err = formatError e
|
err = formatError e
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue