How to include a boolean option in front of an argument #576
-
Hi Cliffy team, great work! I have really enjoyed using cliffy so far. I have a question about boolean options. In my experience, it is a common CLI pattern to be able to have a boolean option flag that:
For example: apt-get install -y tree
apt-get install tree -y The command works either way. I am having trouble doing the same with cliffy. Here is a reprex: // reprex.ts
import { Command } from "https://deno.land/x/cliffy@v0.25.7/command/mod.ts";
await new Command()
.name("hello")
.version("0.1.0")
.description("hello world")
.option("-y, --yes [yes:boolean]", "Would you like to?",)
.arguments("<name:string>")
.action(({ yes }, name) => {
console.log("--yes:", yes)
console.log("argument:", name)
})
.parse(Deno.args); ❯ deno run reprex.ts "sam"
--yes: undefined
argument: sam
❯ deno run reprex.ts "sam" -y
--yes: true
argument: sam
❯ deno run reprex.ts -y "sam"
Usage: hello <name>
Version: 0.1.0
Description:
hello world
Options:
-h, --help - Show this help.
-V, --version - Show the version number for this program.
-y, --yes [yes] - Would you like to?
error: Option "--yes" must be of type "boolean", but got "sam". Expected values: "true", "false", "1", "0" It is possible in cliffy to achieve the same behaviour is you get with |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @SamEdwardes, yes you can achieve the same with cliffy. If you want that your option takes no value, then simple remove the argument from the option: new Command()
.option("-y, --yes", "Would you like to?") If you want your option to take an optional value, you can add an equal sign between the flag and the value. This requires adding an equal sign when passing a value to the flag. new Command()
.option("-y, --yes=[yes:boolean]", "Would you like to?") Than you can call it like this: $ deno run reprex.ts -y "sam"
--yes: true
argument: sam
$ deno run reprex.ts -y=true "sam"
--yes: true
argument: sam
$ deno run reprex.ts -y=0 "sam"
--yes: false
argument: sam |
Beta Was this translation helpful? Give feedback.
Hi @SamEdwardes,
yes you can achieve the same with cliffy.
If you want that your option takes no value, then simple remove the argument from the option:
If you want your option to take an optional value, you can add an equal sign between the flag and the value. This requires adding an equal sign when passing a value to the flag.
Than you can call it like this: