Skip to content

Commit

Permalink
Fix CommandParser incorrectly handling multiple end-of-option delimit…
Browse files Browse the repository at this point in the history
…ers (#3541)

* Stop swallowing "--" after the first one is processed by command parser

Previously, the command parser would swallow all "--" after the first
one. For me, this manifested itself in not allowing corral to be used
with lldb "corral exec -- lldb ponyc -- $(ponyc) -V=0 -o ./build/
./utility" since the second "--" is required to be passed to lldb.

This change is dependency for a PR being submitted to corral to allow
use of corral with lldb (for debugging ponyc itself).

* adding test for multiple end-of-options issue

* adding test for multiple end-of-options issue (+1 squashed commit)
Squashed commits:
[9ab2aab] Stop swallowing "--" after the first one is processed by command parser

Previously, the command parser would swallow all "--" after the first
one. For me, this manifested itself in not allowing corral to be used
with lldb "corral exec -- lldb ponyc -- $(ponyc) -V=0 -o ./build/
./utility" since the second "--" is required to be passed to lldb.

This change is dependency for a PR being submitted to corral to allow
use of corral with lldb (for debugging ponyc itself).
  • Loading branch information
KittyMac authored May 7, 2020
1 parent 73468a7 commit 87b473d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
41 changes: 41 additions & 0 deletions packages/cli/_test.pony
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ actor Main is TestList
test(_TestHelp)
test(_TestHelpFalse)
test(_TestHelpMultipleArgs)
test(_TestMultipleEndOfOptions)

class iso _TestMinimal is UnitTest
fun name(): String => "ponycli/minimal"
Expand Down Expand Up @@ -548,6 +549,25 @@ class iso _TestHelpMultipleArgs is UnitTest
h.assert_true(
help.contains("simple <words> <argz>"))

class iso _TestMultipleEndOfOptions is UnitTest
fun name(): String => "ponycli/multiple-end-of-options"

fun apply(h: TestHelper) ? =>
let cs = _Fixtures.corral_spec()?

let args: Array[String] = ["ignored"; "exec"; "--"; "lldb"; "ponyc"; "--"; "-v" ]
let cmdErr = CommandParser(cs).parse(args)
h.log("Parsed: " + cmdErr.string())

let cmd = cmdErr as Command
h.assert_eq[String]("corral/exec", cmd.fullname())

let argss = cmd.arg("args").string_seq()
h.assert_eq[String]("lldb", argss(0)?)
h.assert_eq[String]("ponyc", argss(1)?)
h.assert_eq[String]("--", argss(2)?)
h.assert_eq[String]("-v", argss(3)?)

primitive _Fixtures
fun bools_cli_spec(): CommandSpec box ? =>
"""
Expand Down Expand Up @@ -606,3 +626,24 @@ primitive _Fixtures
])?
])?
])?


fun corral_spec(): CommandSpec box ? =>
"""
A snippet from Corral's command spec to demonstrate multiple end of option arguments
"""
CommandSpec.parent("corral", "", [
OptionSpec.u64(
"debug",
"Configure debug output: 0=off, 1=err, 2=warn, 3=info, 4=fine."
where short'='g',
default' = 0)
], [
CommandSpec.leaf(
"exec",
"For executing shell commands which require user interaction",
Array[OptionSpec](),
[
ArgSpec.string_seq("args", "Arguments to run.")
])?
])?
2 changes: 1 addition & 1 deletion packages/cli/command_parser.pony
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class CommandParser

while tokens.size() > 0 do
let token = try tokens.shift()? else "" end
if token == "--" then
if (token == "--") and (opt_stop == false) then
opt_stop = true

elseif not opt_stop and (token.compare_sub("--", 2, 0) == Equal) then
Expand Down

0 comments on commit 87b473d

Please sign in to comment.