Skip to content

Commit

Permalink
Add u64 to Options, Command and CommandSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
winksaville authored and SeanTAllen committed Jan 17, 2018
1 parent 35254ac commit 6cc6d49
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 15 deletions.
32 changes: 22 additions & 10 deletions packages/cli/_test.pony
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ class iso _TestDefaults is UnitTest
fun apply(h: TestHelper) ? =>
let cs = _Fixtures.simple_cli_spec()?

let args: Array[String] = ["ignored"; "-B"; "-S--"; "-I42"; "-F42.0"]
let args: Array[String] =
["ignored"; "-B"; "-S--"; "-I42"; "-U47"; "-F42.0"]
let cmdErr = CommandParser(cs).parse(args)
h.log("Parsed: " + cmdErr.string())

Expand All @@ -199,6 +200,7 @@ class iso _TestDefaults is UnitTest
h.assert_eq[Bool](true, cmd.option("boolo").bool())
h.assert_eq[String]("astring", cmd.option("stringo").string())
h.assert_eq[I64](42, cmd.option("into").i64())
h.assert_eq[U64](47, cmd.option("uinto").u64())
h.assert_eq[F64](42.0, cmd.option("floato").f64())
h.assert_eq[USize](0, cmd.option("stringso").string_seq().size())

Expand All @@ -211,7 +213,7 @@ class iso _TestShortsAdj is UnitTest

let args: Array[String] = [
"ignored"
"-BS--"; "-I42"; "-F42.0"; "-zaaa"; "-zbbb"
"-BS--"; "-I42"; "-U47"; "-F42.0"; "-zaaa"; "-zbbb"
]
let cmdErr = CommandParser(cs).parse(args)
h.log("Parsed: " + cmdErr.string())
Expand All @@ -221,6 +223,7 @@ class iso _TestShortsAdj is UnitTest
h.assert_eq[Bool](true, cmd.option("boolr").bool())
h.assert_eq[String]("--", cmd.option("stringr").string())
h.assert_eq[I64](42, cmd.option("intr").i64())
h.assert_eq[U64](47, cmd.option("uintr").u64())
h.assert_eq[F64](42.0, cmd.option("floatr").f64())
let stringso = cmd.option("stringso")
h.assert_eq[USize](2, stringso.string_seq().size())
Expand All @@ -236,7 +239,7 @@ class iso _TestShortsEq is UnitTest

let args: Array[String] = [
"ignored"
"-BS=astring"; "-I=42"; "-F=42.0"; "-z=aaa"; "-z=bbb"
"-BS=astring"; "-I=42"; "-U=47"; "-F=42.0"; "-z=aaa"; "-z=bbb"
]
let cmdErr = CommandParser(cs).parse(args)
h.log("Parsed: " + cmdErr.string())
Expand All @@ -246,6 +249,7 @@ class iso _TestShortsEq is UnitTest
h.assert_eq[Bool](true, cmd.option("boolr").bool())
h.assert_eq[String]("astring", cmd.option("stringr").string())
h.assert_eq[I64](42, cmd.option("intr").i64())
h.assert_eq[U64](47, cmd.option("uintr").u64())
h.assert_eq[F64](42.0, cmd.option("floatr").f64())
let stringso = cmd.option("stringso")
h.assert_eq[USize](2, stringso.string_seq().size())
Expand All @@ -261,7 +265,8 @@ class iso _TestShortsNext is UnitTest

let args: Array[String] = [
"ignored"
"-BS"; "--"; "-I"; "42"; "-F"; "42.0"; "-z"; "aaa"; "-z"; "bbb"
"-BS"; "--"; "-I"; "42"; "-U"; "47"
"-F"; "42.0"; "-z"; "aaa"; "-z"; "bbb"
]
let cmdErr = CommandParser(cs).parse(args)
h.log("Parsed: " + cmdErr.string())
Expand All @@ -271,6 +276,7 @@ class iso _TestShortsNext is UnitTest
h.assert_eq[Bool](true, cmd.option("boolr").bool())
h.assert_eq[String]("--", cmd.option("stringr").string())
h.assert_eq[I64](42, cmd.option("intr").i64())
h.assert_eq[U64](47, cmd.option("uintr").u64())
h.assert_eq[F64](42.0, cmd.option("floatr").f64())
let stringso = cmd.option("stringso")
h.assert_eq[USize](2, stringso.string_seq().size())
Expand All @@ -286,8 +292,8 @@ class iso _TestLongsEq is UnitTest

let args: Array[String] = [
"ignored"
"--boolr=true"; "--stringr=astring"; "--intr=42"; "--floatr=42.0"
"--stringso=aaa"; "--stringso=bbb"
"--boolr=true"; "--stringr=astring"; "--intr=42"; "--uintr=47"
"--floatr=42.0"; "--stringso=aaa"; "--stringso=bbb"
]
let cmdErr = CommandParser(cs).parse(args)
h.log("Parsed: " + cmdErr.string())
Expand All @@ -297,6 +303,7 @@ class iso _TestLongsEq is UnitTest
h.assert_eq[Bool](true, cmd.option("boolr").bool())
h.assert_eq[String]("astring", cmd.option("stringr").string())
h.assert_eq[I64](42, cmd.option("intr").i64())
h.assert_eq[U64](47, cmd.option("uintr").u64())
h.assert_eq[F64](42.0, cmd.option("floatr").f64())
let stringso = cmd.option("stringso")
h.assert_eq[USize](2, stringso.string_seq().size())
Expand All @@ -312,8 +319,8 @@ class iso _TestLongsNext is UnitTest

let args: Array[String] = [
"ignored"
"--boolr"; "--stringr"; "--"; "--intr"; "42"; "--floatr"; "42.0"
"--stringso"; "aaa"; "--stringso"; "bbb"
"--boolr"; "--stringr"; "--"; "--intr"; "42"; "--uintr"; "47"
"--floatr"; "42.0"; "--stringso"; "aaa"; "--stringso"; "bbb"
]
let cmdErr = CommandParser(cs).parse(args)
h.log("Parsed: " + cmdErr.string())
Expand All @@ -322,6 +329,7 @@ class iso _TestLongsNext is UnitTest

h.assert_eq[String]("--", cmd.option("stringr").string())
h.assert_eq[I64](42, cmd.option("intr").i64())
h.assert_eq[U64](47, cmd.option("uintr").u64())
h.assert_eq[F64](42.0, cmd.option("floatr").f64())
let stringso = cmd.option("stringso")
h.assert_eq[USize](2, stringso.string_seq().size())
Expand All @@ -342,6 +350,7 @@ class iso _TestEnvs is UnitTest
"SIMPLE_BOOLR=true"
"SIMPLE_STRINGR=astring"
"SIMPLE_INTR=42"
"SIMPLE_UINTR=47"
"SIMPLE_FLOATR=42.0"
]
let cmdErr = CommandParser(cs).parse(args, envs)
Expand All @@ -352,6 +361,7 @@ class iso _TestEnvs is UnitTest
h.assert_eq[Bool](true, cmd.option("boolr").bool())
h.assert_eq[String]("astring", cmd.option("stringr").string())
h.assert_eq[I64](42, cmd.option("intr").i64())
h.assert_eq[U64](47, cmd.option("uintr").u64())
h.assert_eq[F64](42.0, cmd.option("floatr").f64())

class iso _TestOptionStop is UnitTest
Expand Down Expand Up @@ -383,8 +393,8 @@ class iso _TestDuplicate is UnitTest

let args: Array[String] = [
"ignored"
"--boolr=true"; "--stringr=astring"; "--intr=42"; "--floatr=42.0"
"--stringr=newstring"
"--boolr=true"; "--stringr=astring"; "--intr=42"; "--uintr=47"
"--floatr=42.0"; "--stringr=newstring"
]
let cmdErr = CommandParser(cs).parse(args)
h.log("Parsed: " + cmdErr.string())
Expand Down Expand Up @@ -491,6 +501,8 @@ primitive _Fixtures
OptionSpec.string("stringo" where short' = 's', default' = "astring")
OptionSpec.i64("intr" where short' = 'I')
OptionSpec.i64("into" where short' = 'i', default' = I64(42))
OptionSpec.u64("uintr" where short' = 'U')
OptionSpec.u64("uinto" where short' = 'u', default' = U64(47))
OptionSpec.f64("floatr" where short' = 'F')
OptionSpec.f64("floato" where short' = 'f', default' = F64(42.0))
OptionSpec.string_seq("stringso" where short' = 'z')
Expand Down
12 changes: 12 additions & 0 deletions packages/cli/command.pony
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ class val Option
"""
try _value as I64 else I64(0) end

fun u64(): U64 =>
"""
Returns the option value as an U64, defaulting to 0.
"""
try _value as U64 else U64(0) end

fun f64(): F64 =>
"""
Returns the option value as an F64, defaulting to 0.0.
Expand Down Expand Up @@ -147,6 +153,12 @@ class val Arg
"""
try _value as I64 else I64(0) end

fun u64(): U64 =>
"""
Returns the arg value as an U64, defaulting to 0.
"""
try _value as U64 else U64(0) end

fun f64(): F64 =>
"""
Returns the arg value as an F64, defaulting to 0.0.
Expand Down
38 changes: 36 additions & 2 deletions packages/cli/command_spec.pony
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,29 @@ class val OptionSpec
Creates an Option with an I64 typed value that can be used like
`--count=42 -C=42`
to yield an option value like
`cmd.option("count").i64() == I64(64)`.
`cmd.option("count").i64() == I64(42)`.
"""
_name = name'
_descr = descr'
_short = short'
(_typ, _default, _required) = _init(_I64Type, default')

new val u64(name': String,
descr': String = "",
short': (U8 | None) = None,
default': (U64 | None) = None)
=>
"""
Creates an Option with an U64 typed value that can be used like
`--count=47 -C=47`
to yield an option value like
`cmd.option("count").u64() == U64(47)`.
"""
_name = name'
_descr = descr'
_short = short'
(_typ, _default, _required) = _init(_U64Type, default')

new val f64(name': String,
descr': String = "",
short': (U8 | None) = None,
Expand Down Expand Up @@ -375,6 +391,20 @@ class val ArgSpec
_descr = descr'
(_typ, _default, _required) = _init(_I64Type, default')

new val u64(name': String,
descr': String = "",
default': (U64 | None) = None)
=>
"""
Creates an Arg with an U64 typed value that can be used like
`<cmd> 47`
to yield an arg value like
`cmd.arg("count").u64() == U64(47)`.
"""
_name = name'
_descr = descr'
(_typ, _default, _required) = _init(_U64Type, default')

new val f64(name': String,
descr': String = "",
default': (F64 | None) = None)
Expand Down Expand Up @@ -466,7 +496,7 @@ class _StringSeq is ReadSeq[String]
fun apply(i: USize): this->String ? => strings(i)?
fun values(): Iterator[this->String]^ => strings.values()

type _Value is (Bool | String | I64 | F64 | _StringSeq val)
type _Value is (Bool | String | I64 | U64 | F64 | _StringSeq val)

trait val _ValueType
fun string(): String
Expand All @@ -486,6 +516,10 @@ primitive _I64Type is _ValueType
fun string(): String => "I64"
fun value_of(s: String): _Value ? => s.i64()?

primitive _U64Type is _ValueType
fun string(): String => "U64"
fun value_of(s: String): _Value ? => s.u64()?

primitive _F64Type is _ValueType
fun string(): String => "F64"
fun value_of(s: String): _Value => s.f64()
Expand Down
13 changes: 11 additions & 2 deletions packages/options/_test.pony
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,29 @@ class iso _TestLongOptions is UnitTest
fun name(): String => "options/Options.longOptions"

fun apply(h: TestHelper) =>
let options = Options(["--none"; "--i64"; "12345"; "--f64=67.890"])
let options = Options(
["--none"; "--i64"; "12345"; "--u64=54321"; "--f64=67.890"])
var none: Bool = false
var i64: I64 = -1
var u64: U64 = 1
var f64: F64 = -1
options
.add("none", "n", None, Optional)
.add("i64", "i", I64Argument, Optional)
.add("u64", "u", U64Argument, Optional)
.add("f64", "f", F64Argument, Optional)
for option in options do
match option
| ("none", let arg: None) => none = true
| ("i64", let arg: I64) => i64 = arg
| ("u64", let arg: U64) => u64 = arg
| ("f64", let arg: F64) => f64 = arg
end
end

h.assert_eq[Bool](true, none)
h.assert_eq[I64](12345, i64)
h.assert_eq[U64](54321, u64)
h.assert_eq[F64](67.890, f64)

class iso _TestShortOptions is UnitTest
Expand All @@ -47,18 +52,21 @@ class iso _TestShortOptions is UnitTest
fun name(): String => "options/Options.shortOptions"

fun apply(h: TestHelper) =>
let options = Options(["-n"; "-i"; "12345"; "-f67.890"])
let options = Options(["-n"; "-i"; "12345"; "-u54321"; "-f67.890"])
var none: Bool = false
var i64: I64 = -1
var u64: U64 = 1
var f64: F64 = -1
options
.add("none", "n", None, Optional)
.add("i64", "i", I64Argument, Optional)
.add("u64", "u", U64Argument, Optional)
.add("f64", "f", F64Argument, Optional)
for option in options do
match option
| ("none", let arg: None) => none = true
| ("i64", let arg: I64) => i64 = arg
| ("u64", let arg: U64) => u64 = arg
| ("f64", let arg: F64) => f64 = arg
else
h.fail("Invalid option reported")
Expand All @@ -67,6 +75,7 @@ class iso _TestShortOptions is UnitTest

h.assert_eq[Bool](true, none)
h.assert_eq[I64](12345, i64)
h.assert_eq[U64](54321, u64)
h.assert_eq[F64](67.890, f64)

class iso _TestCombineShortOptions is UnitTest
Expand Down
10 changes: 9 additions & 1 deletion packages/options/options.pony
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ actor Main
// Some values we can set via command line options
var _a_string: String = "default"
var _a_number: USize = 0
var _a_unumber: USize = 0
var _a_float: Float = F64(0.0)
new create(env: Env) =>
Expand All @@ -22,6 +23,7 @@ actor Main
_env.out.print("The String is " + _a_string)
_env.out.print("The Number is " + _a_number.string())
_env.out.print("The UNumber is " + _a_unumber.string())
_env.out.print("The Float is " + _a_float.string())
fun ref arguments() ? =>
Expand All @@ -30,12 +32,14 @@ actor Main
options
.add("string", "t", StringArgument)
.add("number", "i", I64Argument)
.add("unumber", "u", U64Argument)
.add("float", "c", F64Argument)
for option in options do
match option
| ("string", let arg: String) => _a_string = arg
| ("number", let arg: I64) => _a_number = arg.usize()
| ("unumber", let arg: U64) => _a_unumber = arg.usize()
| ("float", let arg: F64) => _a_float = arg
| let err: ParseError => err.report(_env.out) ; usage() ; error
end
Expand All @@ -52,13 +56,15 @@ actor Main
"program [OPTIONS]\n" +
" --string N a string argument. Defaults to 'default'.\n" +
" --number N a number argument. Defaults to 0.\n" +
" --unumber N a unsigned number argument. Defaults to 0.\n" +
" --float N a floating point argument. Defaults to 0.0.\n"
)
```
"""

primitive StringArgument
primitive I64Argument
primitive U64Argument
primitive F64Argument
primitive Required
primitive Optional
Expand All @@ -72,6 +78,7 @@ type ArgumentType is
( None
| StringArgument
| I64Argument
| U64Argument
| F64Argument
)

Expand All @@ -82,7 +89,7 @@ type ErrorReason is
| AmbiguousMatch
)

type ParsedOption is (String, (None | String | I64 | F64))
type ParsedOption is (String, (None | String | I64 | U64 | F64))

interface ParseError
fun reason(): ErrorReason
Expand Down Expand Up @@ -224,6 +231,7 @@ class Options is Iterator[(ParsedOption | ParseError | None)]
match opt.arg
| StringArgument => return (opt.long, argument.clone())
| I64Argument => return (opt.long, argument.i64()?)
| U64Argument => return (opt.long, argument.u64()?)
| F64Argument => return (opt.long, argument.f64())
end
else
Expand Down

0 comments on commit 6cc6d49

Please sign in to comment.