Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(command,flags): add integer type (#190)
- Loading branch information
Showing
16 changed files
with
257 additions
and
71 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
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,62 @@ | ||
import { assertEquals, assertThrowsAsync } from "../../../dev_deps.ts"; | ||
import { Command } from "../../command.ts"; | ||
|
||
const cmd = new Command() | ||
.throwErrors() | ||
.option("-f, --flag [value:integer]", "description ...") | ||
.option("-F, --flag2 <value:integer>", "description ...") | ||
.action(() => {}); | ||
|
||
Deno.test("command - type - integer - with no value", async () => { | ||
const { options, args } = await cmd.parse(["-f"]); | ||
|
||
assertEquals(options, { flag: true }); | ||
assertEquals(args, []); | ||
}); | ||
|
||
Deno.test("command - type - integer - with valid value", async () => { | ||
const { options, args } = await cmd.parse(["--flag", "123"]); | ||
|
||
assertEquals(options, { flag: 123 }); | ||
assertEquals(args, []); | ||
}); | ||
|
||
Deno.test("command - type - integer - with argument", async () => { | ||
await assertThrowsAsync( | ||
async () => { | ||
await cmd.parse(["-f", "123", "unknown"]); | ||
}, | ||
Error, | ||
`No arguments allowed for command "COMMAND".`, | ||
); | ||
}); | ||
|
||
Deno.test("command - type - integer - with missing value", async () => { | ||
await assertThrowsAsync( | ||
async () => { | ||
await cmd.parse(["-F"]); | ||
}, | ||
Error, | ||
`Missing value for option "--flag2".`, | ||
); | ||
}); | ||
|
||
Deno.test("command - type - integer - with invalid string value", async () => { | ||
await assertThrowsAsync( | ||
async () => { | ||
await cmd.parse(["-f", "abc"]); | ||
}, | ||
Error, | ||
`Option "--flag" must be of type "integer", but got "abc".`, | ||
); | ||
}); | ||
|
||
Deno.test("command - type - integer - with invalid float value", async () => { | ||
await assertThrowsAsync( | ||
async () => { | ||
await cmd.parse(["-f", "1.23"]); | ||
}, | ||
Error, | ||
`Option "--flag" must be of type "integer", but got "1.23".`, | ||
); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Type } from "../type.ts"; | ||
import type { ITypeInfo } from "../types.ts"; | ||
import { integer } from "../../flags/types/integer.ts"; | ||
|
||
/** Integer type. */ | ||
export class IntegerType extends Type<number> { | ||
/** Parse integer type. */ | ||
public parse(type: ITypeInfo): number { | ||
return integer(type); | ||
} | ||
} |
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.