Command throws error when parsing number
halvardssm opened this issue · comments
Hi! I found a bug which happens when passing a number to a command.
How to reproduce:
import Denomander from "https://deno.land/x/denomander/mod.ts";
const program = new Denomander();
program.command("foo [bar]", "Foo")
program.globalOption("--baz", "Baz")
program.parse(Deno.args);
deno run mod.ts foo 1
The error:
error: Uncaught TypeError: text.substr is not a function
return text.substr(0, 2).replace(/-/g, "") +
^
at Function.stripDashes (https://deno.land/x/denomander@0.6.1/src/Helper.ts:5:17)
at https://deno.land/x/denomander@0.6.1/src/Util.ts:112:43
at Array.find (<anonymous>)
at Function.findCommandFromArgs (https://deno.land/x/denomander@0.6.1/src/Util.ts:111:18)
at https://deno.land/x/denomander@0.6.1/src/Executor.ts:28:30
at Array.forEach (<anonymous>)
at Executor.defaultCommands (https://deno.land/x/denomander@0.6.1/src/Executor.ts:27:26)
at Denomander.execute (https://deno.land/x/denomander@0.6.1/src/Kernel.ts:130:10)
at Denomander.run (https://deno.land/x/denomander@0.6.1/src/Kernel.ts:218:8)
at Denomander.parse (https://deno.land/x/denomander@0.6.1/src/Denomander.ts:15:10)
Also, when passing an option e.g. deno run mod.ts foo bar --baz buz
, both bar and baz will have the value buz
instead of foo=bar
and baz=buz
Hi. Thanks for pointing out. It is a bug. I am working on it!
Ok I fixed the first one. As for the second (option parsing) I think there is no bug there.
program.globalOption("--baz", "Baz");
program.command("foo [bar]", "Foo").action(({ bar }: any) => {
if(program.baz){
console.log(program.baz);
}
console.log(bar);
});
deno run examples/example.ts foo 1234 --baz buz
buz
1234
Great, thanks! I assume the second bug was fixed when the first one was solved 👍
Btw, any chance we can get a patch update with this fix? 🙏
Just updated it. v0.6.2
Thanks!
@siokas This might be a followup, let me know if you want me to open a new issue. When using 0
as a value e.g. deno run mod.ts foo 0
, it gets parsed as undefined.
I tried like deno run mod.ts foo 0
but for me it works fine. I also wrote a test about this and it passes.
test("command_argument_parse_number_0", function () {
const program = new Denomander();
const args = ["foo", "0"];
let result = 1;
program.command("foo [bar]", "Foo")
.action(({ bar }: any) => {
result = bar;
});
program.parse(args);
assertEquals(result, 0);
});
and I am sure that it parses it as number because I also tried with const args = ["foo", "000"];
and it again returns 0 as number.