command icon indicating copy to clipboard operation
command copied to clipboard

Async flag options?

Open devshorts opened this issue 6 years ago • 3 comments

Is it possible to hydrate a list of options in the flags from an async function? A good use case would be to dynamically detect the input options for an array.

devshorts avatar Nov 07 '19 23:11 devshorts

Options is string[] type. Would need some reworking to accomplish. You could get "close" and enforce options with a custom flag parser.

RasPhilCo avatar Nov 12 '19 18:11 RasPhilCo

I'm also looking for more flexibility in this area.

Specifically I want installed plugins (using @oclif/plugin-plugins) to be able to register themselves using hooks (runHook). runHooks is async and also not possible to execute this.config.runHooks outside of the Command's instance context, as far as I can tell.

Are there any way to currently do this?

import { Command, flags } from '@oclif/command'

import { compilerRegistry } from '../compiler-registry';

export default class Extract extends Command {

	static flags = {
		compiler: flags.string({
			char: 'c',
			required: true,
			description: 'Compiler to use for translations',
			options: () => {
				return compilerRegistry.getRegistered().map(compiler => compiler.shortName);
			}
		})
	}

	async init() {
		await this.config.runHook('register-compilers', {
			id: 'register-compilers',
			registry: compilerRegistry
		});
	}
}

biesbjerg avatar Mar 28 '20 10:03 biesbjerg

In my case I would like to make a flag's default function async, e.g. return a Promise. Is that reasonable?

sisou avatar Aug 25 '21 13:08 sisou