Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(command): add custom type examples
- Loading branch information
Showing
4 changed files
with
54 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env -S deno --allow-env | ||
|
||
import { Command, StringType } from '../../command.ts'; | ||
|
||
class EmailType extends StringType { | ||
|
||
complete(): string[] { | ||
return [ 'aaa@example.com', 'bbb@example.com', 'ccc@example.com' ]; | ||
} | ||
} | ||
|
||
await new Command() | ||
.option( '-e, --email <value:email>', 'Your email address.' ) | ||
.type( 'email', new EmailType() ) | ||
.parse( Deno.args ); |
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,27 @@ | ||
#!/usr/bin/env -S deno --allow-env | ||
|
||
import { Command, Type } from '../../command.ts'; | ||
import { IFlagArgument, IFlagOptions } from '../../flags.ts'; | ||
|
||
class EmailType extends Type<string> { | ||
|
||
protected emailRegex: RegExp = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | ||
|
||
parse( option: IFlagOptions, arg: IFlagArgument, value: string | false ): string | undefined { | ||
|
||
if ( value ) { | ||
if ( !this.emailRegex.test( value.toLowerCase() ) ) { | ||
throw new Error( `Option --${ option.name } must be a valid email but got: ${ value }` ); | ||
} | ||
} | ||
|
||
return value || undefined; | ||
} | ||
} | ||
|
||
const { options } = await new Command() | ||
.option( '-e, --email <value:email>', 'Your email address.' ) | ||
.type( 'email', new EmailType() ) | ||
.parse( Deno.args ); | ||
|
||
console.log( options ); |
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,8 @@ | ||
#!/usr/bin/env -S deno --allow-env | ||
|
||
import { Command } from '../../packages/command/lib/command.ts'; | ||
|
||
await new Command() | ||
.option( '-e, --email <value:string:email>', 'Your email address.' ) | ||
.complete( 'email', () => [ 'aaa@example.com', 'bbb@example.com', 'ccc@example.com' ] ) | ||
.parse( Deno.args ); |
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