Skip to content

Commit

Permalink
refactor(flags,command): rename requires option to depends
Browse files Browse the repository at this point in the history
BREAKING CHANGES:

To define depending options you have tu use the options `depends` instead of `requires` now.
  • Loading branch information
c4spar committed Apr 4, 2020
1 parent cf97a15 commit c937466
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion examples/command/depending-options.ts
Expand Up @@ -4,7 +4,7 @@ import { Command } from '../../packages/command/lib/command.ts';

const { options } = await new Command()
.option( '-a, --audio-codec <type:string>', 'description ...' )
.option( '-v, --video-codec <type:string>', 'description ...', { requires: [ 'audio-codec' ] } )
.option( '-v, --video-codec <type:string>', 'description ...', { depends: [ 'audio-codec' ] } )
.parse( Deno.args );

console.log( options );
4 changes: 2 additions & 2 deletions packages/command/README.md
Expand Up @@ -429,7 +429,7 @@ You ordered a pizza with sauce and parmesan cheese

### Options which depends on other options

Some options can not be call without other options. You can specify depending options with the `requires` option.
Some options can not be call without other options. You can specify depending options with the `depends` option.

```typescript
#!/usr/bin/env -S deno --allow-env
Expand All @@ -438,7 +438,7 @@ import { Command } from 'https://deno.land/x/cliffy/command.ts';

const { options } = await new Command()
.option( '-a, --audio-codec <type:string>', 'description ...' )
.option( '-v, --video-codec <type:string>', 'description ...', { requires: [ 'audio-codec' ] } )
.option( '-v, --video-codec <type:string>', 'description ...', { depends: [ 'audio-codec' ] } )
.parse( Deno.args );

console.log( options );
Expand Down
6 changes: 3 additions & 3 deletions packages/command/test/option/conflicts_test.ts
Expand Up @@ -10,17 +10,17 @@ const cmd = new Command()
} )
.option( '-v, --video-type [value:string]', 'description ...', {
required: true,
requires: [ 'audio-type', 'image-type' ],
depends: [ 'audio-type', 'image-type' ],
conflicts: [ 'type' ]
} )
.option( '-a, --audio-type [value:string]', 'description ...', {
required: true,
requires: [ 'video-type', 'image-type' ],
depends: [ 'video-type', 'image-type' ],
conflicts: [ 'type' ]
} )
.option( '-i, --image-type [value:string]', 'description ...', {
required: true,
requires: [ 'video-type', 'audio-type' ],
depends: [ 'video-type', 'audio-type' ],
conflicts: [ 'type' ]
} )
.action( () => {} );
Expand Down
6 changes: 3 additions & 3 deletions packages/command/test/option/requires_test.ts
Expand Up @@ -3,9 +3,9 @@ import { assertEquals, assertThrowsAsync } from '../lib/assert.ts';

const cmd = new Command()
.throwErrors()
.option( '-v, --video-type [value:string]', 'description ...', { requires: [ 'audio-type', 'image-type' ] } )
.option( '-a, --audio-type [value:string]', 'description ...', { requires: [ 'video-type', 'image-type' ] } )
.option( '-i, --image-type [value:string]', 'description ...', { requires: [ 'video-type', 'audio-type' ] } )
.option( '-v, --video-type [value:string]', 'description ...', { depends: [ 'audio-type', 'image-type' ] } )
.option( '-a, --audio-type [value:string]', 'description ...', { depends: [ 'video-type', 'image-type' ] } )
.option( '-i, --image-type [value:string]', 'description ...', { depends: [ 'video-type', 'audio-type' ] } )
.action( () => {} );

Deno.test( async function command_optionRequire_noArguments() {
Expand Down
2 changes: 1 addition & 1 deletion packages/flags/lib/flags.ts
Expand Up @@ -33,7 +33,7 @@ export function parseFlags( args: string[], opts: IParseOptions = {} ): IFlagsRe
const unknown: string[] = [];

opts.flags.forEach( opt => {
opt.requires?.forEach( flag => {
opt.depends?.forEach( flag => {
if ( !opts.flags || !getOption( opts.flags, flag ) ) {
throw new Error( `Unknown required option: ${ flag }` );
}
Expand Down
2 changes: 1 addition & 1 deletion packages/flags/lib/types.ts
Expand Up @@ -62,7 +62,7 @@ export interface IFlagOptions extends IFlagArgument {
standalone?: boolean;
default?: IDefaultValue;
required?: boolean;
requires?: string[];
depends?: string[];
conflicts?: string[];
value?: IFlagValueHandler;
collect?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion packages/flags/lib/validate-flags.ts
Expand Up @@ -50,7 +50,7 @@ export function validateFlags( flags: IFlagOptions[], values: IFlags, knownFlaks
}
} );

option.requires?.forEach( ( flag: string ) => {
option.depends?.forEach( ( flag: string ) => {
if ( !isset( flag ) ) {
throw new Error( `Option --${ option.name } depends on option: --${ flag }` );
}
Expand Down
6 changes: 3 additions & 3 deletions packages/flags/test/lib/test-command.ts
Expand Up @@ -29,21 +29,21 @@ export const flags = [ {
aliases: [ 'v' ],
type: OptionType.STRING,
required: true,
requires: [ 'audio-type', 'image-type' ],
depends: [ 'audio-type', 'image-type' ],
conflicts: [ 'type' ]
}, {
name: 'audio-type',
aliases: [ 'a' ],
type: OptionType.STRING,
required: true,
requires: [ 'video-type', 'image-type' ],
depends: [ 'video-type', 'image-type' ],
conflicts: [ 'type' ]
}, {
name: 'image-type',
aliases: [ 'a' ],
type: OptionType.STRING,
required: true,
requires: [ 'video-type', 'audio-type' ],
depends: [ 'video-type', 'audio-type' ],
conflicts: [ 'type' ]
}, {
name: 'amount',
Expand Down
6 changes: 3 additions & 3 deletions packages/flags/test/option/conflicts_test.ts
Expand Up @@ -16,21 +16,21 @@ const options = <IParseOptions>{
aliases: [ 'v' ],
type: OptionType.STRING,
required: true,
requires: [ 'audio-type', 'image-type' ],
depends: [ 'audio-type', 'image-type' ],
conflicts: [ 'type' ]
}, {
name: 'audio-type',
aliases: [ 'a' ],
type: OptionType.STRING,
required: true,
requires: [ 'video-type', 'image-type' ],
depends: [ 'video-type', 'image-type' ],
conflicts: [ 'type' ]
}, {
name: 'image-type',
aliases: [ 'i' ],
type: OptionType.STRING,
required: true,
requires: [ 'video-type', 'audio-type' ],
depends: [ 'video-type', 'audio-type' ],
conflicts: [ 'type' ]
} ]
};
Expand Down
6 changes: 3 additions & 3 deletions packages/flags/test/option/requires_test.ts
Expand Up @@ -9,17 +9,17 @@ const options = <IParseOptions>{
name: 'video-type',
aliases: [ 'v' ],
type: OptionType.STRING,
requires: [ 'audio-type', 'image-type' ]
depends: [ 'audio-type', 'image-type' ]
}, {
name: 'audio-type',
aliases: [ 'a' ],
type: OptionType.STRING,
requires: [ 'video-type', 'image-type' ]
depends: [ 'video-type', 'image-type' ]
}, {
name: 'image-type',
aliases: [ 'i' ],
type: OptionType.STRING,
requires: [ 'video-type', 'audio-type' ]
depends: [ 'video-type', 'audio-type' ]
} ]
};

Expand Down

0 comments on commit c937466

Please sign in to comment.