Skip to content

Bug: quoted string is treated as a number #165

Description

@peetzweg

This is my basic setup:

import { cac } from "cac";

const cli = cac("cac");

cli
  .command("", "")
  .option("-c, --chars <chars>", "Characters")
  .action(async (options) => {
    console.log(options);
  });

cli.help();
cli.version("1.0.0");

try {
  cli.parse();
} catch (error) {
  console.error(error);
  process.exit(1);
}

When I pass -c 1234:

{
  "--": [],
  c: 1234,
  chars: 1234, // number
}

When I pass -c "1234" (quoted):

{
  "--": [],
  c: 1234,
  chars: 1234, // number
}

When I pass -c "abcdef":

{
  "--": [],
  c: "abcdef",
  chars: "abcdef", // string
}

But when i pass "01234" again quoted, it treats it as a number again but also dropping the leading zero as it's a number now. This is odd to me.

"-c "01234":

{
  "--": [],
  c: 1234,
  chars: 1234, // number
}

I would expect cac to treat my option always as a string when it is quoted and don't even try to parse it as a number if it is quoted.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions