Skip to content

Commit

Permalink
docs(command): add generic types example and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed May 20, 2020
1 parent eaeb634 commit 0998f55
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
27 changes: 27 additions & 0 deletions examples/command/generic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env -S deno run

import { Command } from '../../packages/command/lib/command.ts';
import { IParseResult } from '../../packages/command/lib/types.ts';

// define your argument types
type Arguments = [ string, string ];

// define your option types
interface Options {
name: string;
age: number;
email?: string;
}

const result: IParseResult<Options, Arguments> = await new Command<Options, Arguments>()
.version( '0.1.0' )
.arguments( '<input:string> [output:string]' )
.option( '-n, --name <name:string>', 'description ...', { required: true } )
.option( '-a, --age <age:number>', 'description ...', { required: true } )
.option( '-e, --email <email:string>', 'description ...' )
.action( ( options: Options, input: string, output?: string ) => {} )
.parse( Deno.args );

const options: Options = result.options;
const input: string = result.args[ 0 ];
const output: string | undefined = result.args[ 1 ];
36 changes: 36 additions & 0 deletions packages/command/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
- [Override exit handling](#override-exit-handling)
- [Specify environment variables](#specify-the-argument-syntax)
- [Specify examples](#specify-examples)
- [Generic options and arguments](#generic-options-and-arguments)
- [Default options & commands](#default-options--commands)
- [Version option](#version-option)
- [Help option & command](#help-option--command)
Expand Down Expand Up @@ -833,6 +834,41 @@ $ ./examples/command/examples.ts help
```

## Generic options and arguments

You can define an interface for your command options and a tuple for the command arguments.

Here's how to do that:

```typescript
#!/usr/bin/env -S deno run

import { Command, IParseResult } from 'https://deno.land/x/cliffy/command.ts';

type Arguments = [ string, string ];

interface Options {
name: string;
age: number;
email?: string;
}

const result: IParseResult<Options, Arguments> = await new Command<Options, Arguments>()
.version( '0.1.0' )
.arguments( '<input:string> [output:string]' )
.option( '-n, --name <name:string>', 'description ...', { required: true } )
.option( '-a, --age <age:number>', 'description ...', { required: true } )
.option( '-e, --email <email:string>', 'description ...' )
.action( ( options: Options, input: string, output?: string ) => {} )
.parse( Deno.args );

const options: Options = result.options;
const input: string = result.args[ 0 ];
const output: string | undefined = result.args[ 1 ];
```

**Example**: `deno run https://deno.land/x/cliffy/examples/command/generic.ts`

## Default options & commands

Every instance of the `Command` class has some pre defenied options and sub-commands. If you don't need these pre defined option's and sub-command's you can use the `BaseCommand` class instedd of the `Command` class.
Expand Down

0 comments on commit 0998f55

Please sign in to comment.