Skip to content

Commit

Permalink
docs(command): add custom type examples
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed Apr 2, 2020
1 parent 34fcddd commit 290d24d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
15 changes: 15 additions & 0 deletions examples/command/custom-option-type-class-completion.ts
@@ -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 );
27 changes: 27 additions & 0 deletions examples/command/custom-option-type-class.ts
@@ -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 );
8 changes: 8 additions & 0 deletions examples/command/custom-option-type-completion.ts
@@ -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 );
13 changes: 4 additions & 9 deletions examples/command/custom-option-type.ts
Expand Up @@ -13,21 +13,16 @@ const email = (): ITypeHandler<string> => {
if ( !emailRegex.test( value.toLowerCase() ) ) {
throw new Error( `Option --${ option.name } must be a valid email but got: ${ value }` );
}
return value;
}

return value || undefined;
};
};

// Register email as global type:
Command.type( 'email', email() );

const { options } = await new Command()
// Register email as command specific type:
.arguments( '[value:string:email]' )
.option( '-e, --email <value:email>', 'Your email address.' )
.type( 'email', email() )
.option( '-e, --email <value:email>', 'Your email address.', {
// Register email as option specific type:
// type: email()
} )
.parse( Deno.args );

console.log( options );

0 comments on commit 290d24d

Please sign in to comment.