Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command throws error when parsing number #22

Closed
halvardssm opened this issue May 29, 2020 · 8 comments
Closed

Command throws error when parsing number #22

halvardssm opened this issue May 29, 2020 · 8 comments
Labels
bug Something isn't working
Milestone

Comments

@halvardssm
Copy link
Contributor

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

@siokas siokas added the bug Something isn't working label May 29, 2020
@siokas siokas added this to the v1.0 (stable) milestone May 29, 2020
@siokas
Copy link
Owner

siokas commented May 29, 2020

Hi. Thanks for pointing out. It is a bug. I am working on it!

@siokas
Copy link
Owner

siokas commented May 29, 2020

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

@siokas siokas closed this as completed May 30, 2020
@halvardssm
Copy link
Contributor Author

Great, thanks! I assume the second bug was fixed when the first one was solved 👍

@halvardssm
Copy link
Contributor Author

Btw, any chance we can get a patch update with this fix? 🙏

@siokas
Copy link
Owner

siokas commented May 30, 2020

Just updated it. v0.6.2

@halvardssm
Copy link
Contributor Author

Thanks!

@halvardssm
Copy link
Contributor Author

@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.

@siokas
Copy link
Owner

siokas commented May 30, 2020

I tried like deno run mod.ts foo 0but 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants