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

bug(flags): boolean flag doesn't equal assigned value #2341

Closed
timreichen opened this issue Jun 13, 2022 · 4 comments
Closed

bug(flags): boolean flag doesn't equal assigned value #2341

timreichen opened this issue Jun 13, 2022 · 4 comments
Labels
bug Something isn't working needs triage

Comments

@timreichen
Copy link
Contributor

Describe the bug
A flag which is set as a boolean shouldn't be parsed as a boolean if it has a value assigned with the equals sign as described:

options.boolean - a boolean, string or array of strings to always treat as booleans. if true will treat all double hyphenated arguments without equal signs as boolean (e.g. affects --foo, not -f or --foo=bar).

Steps to Reproduce

import { parse } from "https://deno.land/std@0.143.0/flags/mod.ts";
const args = parse(["--reload=value"], { boolean: ["reload"] });

parse returns { _:[], reload: true }

Expected behavior
parse should return { _:[], reload: "value" }

Environment

  • OS: MacOS 12.4
  • deno version: 1.22.0
  • std version: 0.143.0
@timreichen timreichen added bug Something isn't working needs triage labels Jun 13, 2022
@sigmaSd
Copy link
Contributor

sigmaSd commented Jun 13, 2022

Maybe this check https://github.com/denoland/deno_std/blob/main/flags/mod.ts#L310 should just be removed and always run setArg(key, value, arg) instead?

@c4spar
Copy link
Contributor

c4spar commented Jun 14, 2022

I think the returned result is correct here because --reload=value is only parsed as string if boolean is set to true, but in your example it is set to ["reload"], which explicitly marks reload as a boolean argument.

a boolean, string or array of strings to always treat as booleans.

if true will treat all double hyphenated arguments without equal signs as boolean (e.g. affects --foo, not -f or --foo=bar).

But i noticed the values for short flags are wrong:

import { parse } from "https://deno.land/std@0.143.0/flags/mod.ts";

const args1 = parse(["-r=value"], { boolean: ["r"] });

const args2 = parse(["-r=value"], {
  boolean: ["reload"],
  alias: { reload: ["r"] },
});

args1 returns { _:[], reload: "value" }
args2 returns { _: [], r: "value", reload: "value" }

Expected behavior
args1 should return { _:[], reload: true }
args2 should return { _: [], r: true, reload: true }

@timreichen
Copy link
Contributor Author

timreichen commented Jun 14, 2022

I think the returned result is correct here because --reload=value is only parsed as string if boolean is set to true, but in your example it is set to ["reload"], which explicitly marks reload as a boolean argument.

Oh, that is kinda strange. What if you want some flags to behave like you set { boolean: true } but not others?
I think this that boolean property is not very intuitive.

What if we redesigned the parse options to something more intuitive and with a better overview with objects that represent flags? For example:

parse(args, {
  flags: [
    {
      name: "reload",
      alias: "r",
      converter(value: string) { return value == null || value }
    },
    {
      name: "some-list",
      default: "a,b,c",
      args: [{ name: "list" }],
      converter(value: string) { return value.split(",") }
    },
    {
      name: "log-level",
      description: `Set log level [possible values: debug, info]`,
      alias: "L",
      args: [{ name: "log-level" }],
    }
  ]
})

@timreichen
Copy link
Contributor Author

Stale

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

No branches or pull requests

3 participants