Skip to content

Questions about -v variable=value & parsing .arguments() #318

Answered by c4spar
cascading-jox asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @cascading-jox,

sry for late reply, i somehow missed this discussion.

1.)

Is it possible to do have options like -v var=val like in awk? I tried this but to no avail:

There are different ways to achieve your goal:

Value handler

await new Command()
  .option(
    "-v, --variable <variable-name>",
    "pass variable(s) to script",
    {
      value: (val) => {
        const [key, ...parts] = val.split("=");
        const value = parts.join("=");
        if (!value) {
          throw new ValidationError(`missing value for variable: ${val}`);
        }
        return { [key]: value };
      },
    },
  )
  .action(({ variable }) => console.log("variable:", variable))
  .parse(Deno.args);

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@cascading-jox
Comment options

Answer selected by cascading-jox
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants