Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(flags,command): improve support for negatable options (#103)
- Loading branch information
Showing
13 changed files
with
235 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { assertEquals, assertThrowsAsync } from "../../../dev_deps.ts"; | ||
import { Command } from "../../command.ts"; | ||
|
||
function command(): Command { | ||
return new Command() | ||
.throwErrors() | ||
.allowEmpty() | ||
.option("--no-check", "No check.") | ||
.option("--color <color:string>", "Color name.", { default: "yellow" }) | ||
.option("--no-color", "No color.") | ||
.option("--remote <url:string>", "Remote url.", { depends: ["color"] }) | ||
.option("--no-remote", "No remote."); | ||
} | ||
|
||
Deno.test("negatable options with no arguments", async () => { | ||
const { options, args, literal } = await command().parse([]); | ||
|
||
assertEquals(options, { | ||
check: true, | ||
color: "yellow", | ||
}); | ||
assertEquals(args, []); | ||
assertEquals(literal, []); | ||
}); | ||
|
||
Deno.test("negatable options with arguments", async () => { | ||
const { options, args, literal } = await command().parse( | ||
["--color", "blue", "--remote", "foo"], | ||
); | ||
|
||
assertEquals(options, { | ||
check: true, | ||
color: "blue", | ||
remote: "foo", | ||
}); | ||
assertEquals(args, []); | ||
assertEquals(literal, []); | ||
}); | ||
|
||
Deno.test("negatable flag --no-remote should not depend on --color", async () => { | ||
const { options, args, literal } = await command().parse(["--no-remote"]); | ||
|
||
assertEquals(options, { | ||
check: true, | ||
color: "yellow", | ||
remote: false, | ||
}); | ||
assertEquals(args, []); | ||
assertEquals(literal, []); | ||
}); | ||
|
||
Deno.test("negatable flags should negate value", async () => { | ||
const { options, args, literal } = await command().parse( | ||
["--no-check", "--no-color", "--no-remote"], | ||
); | ||
|
||
assertEquals(options, { | ||
color: false, | ||
check: false, | ||
remote: false, | ||
}); | ||
assertEquals(args, []); | ||
assertEquals(literal, []); | ||
}); | ||
|
||
Deno.test("negatable options should not be combinable with positive options", async () => { | ||
await assertThrowsAsync( | ||
async () => { | ||
await command().parse(["--color", "--no-color", "--no-check"]); | ||
}, | ||
Error, | ||
"Duplicate option: --no-color", | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.