diff --git a/src/command.ts b/src/command.ts index 45c0c44a..7efd940d 100644 --- a/src/command.ts +++ b/src/command.ts @@ -246,20 +246,23 @@ export default abstract class Command { const g: any = global g['http-call'] = g['http-call'] || {} g['http-call']!.userAgent = this.config.userAgent - this.checkForDeprecations() + this.warnIfCommandDeprecated() } - protected checkForDeprecations() { + protected warnIfFlagDeprecated(flags: Record) { + for (const flag of Object.keys(flags)) { + const deprecated = this.ctor.flags[flag]?.deprecated + if (deprecated) { + this.warn(formatFlagDeprecationWarning(flag, deprecated)) + } + } + } + + protected warnIfCommandDeprecated(): void { if (this.ctor.state === 'deprecated') { const cmdName = toConfiguredId(this.ctor.id, this.config) this.warn(formatCommandDeprecationWarning(cmdName, this.ctor.deprecationOptions)) } - - for (const [flag, opts] of Object.entries(this.ctor.flags ?? {})) { - if (opts.deprecated) { - this.warn(formatFlagDeprecationWarning(flag, opts.deprecated)) - } - } } protected async parse(options?: Interfaces.Input, argv = this.argv): Promise> { @@ -268,7 +271,10 @@ export default abstract class Command { // the spread operator doesn't work with getters so we have to manually add it here opts.flags = options?.flags opts.args = options?.args - return Parser.parse(argv, opts) + const results = await Parser.parse(argv, opts) + this.warnIfFlagDeprecated(results.flags ?? {}) + + return results } protected async catch(err: Interfaces.CommandError): Promise { diff --git a/test/command/command.test.ts b/test/command/command.test.ts index 72e8693c..e8b7629a 100644 --- a/test/command/command.test.ts +++ b/test/command/command.test.ts @@ -246,18 +246,47 @@ describe('command', () => { version: '2.0.0', }, }), + force: Flags.boolean(), } async run() { + await this.parse(CMD) this.log('running command') } } - await CMD.run([]) + await CMD.run(['--name', 'astro']) }) .do(ctx => expect(ctx.stderr).to.include('Warning: The "name" flag has been deprecated')) .it('shows warning for deprecated flags') }) + describe('deprecated flags that are not provided', () => { + fancy + .stdout() + .stderr() + .do(async () => { + class CMD extends Command { + static flags = { + name: Flags.string({ + deprecated: { + to: '--full-name', + version: '2.0.0', + }, + }), + force: Flags.boolean(), + } + + async run() { + await this.parse(CMD) + this.log('running command') + } + } + await CMD.run(['--force']) + }) + .do(ctx => expect(ctx.stderr).to.not.include('Warning: The "name" flag has been deprecated')) + .it('does not show warning for deprecated flags if they are not provided') + }) + describe('deprecated state', () => { fancy .stdout() @@ -273,7 +302,7 @@ describe('command', () => { await CMD.run([]) }) .do(ctx => expect(ctx.stderr).to.include('Warning: The "my:command" command has been deprecated')) - .it('shows warning for deprecated flags') + .it('shows warning for deprecated command') }) describe('deprecated state with options', () => { @@ -300,7 +329,7 @@ describe('command', () => { expect(ctx.stderr).to.include('in version 2.0.0') expect(ctx.stderr).to.include('Use "my:other:command" instead') }) - .it('shows warning for deprecated flags') + .it('shows warning for deprecated command with custom options') }) describe('stdout err', () => {