Skip to content

Commit

Permalink
cli: register arguments choices.
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Sep 24, 2024
1 parent c3116b6 commit ea244b0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
8 changes: 6 additions & 2 deletions cli/jhipster-command.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import chalk from 'chalk';
import { Command, Option } from 'commander';
import { Argument, Command, Option } from 'commander';
import { kebabCase } from 'lodash-es';
import { convertConfigToOption } from '../lib/command/index.js';

Expand Down Expand Up @@ -167,7 +167,11 @@ export default class JHipsterCommand extends Command {
Object.entries(jhipsterArguments ?? {}).forEach(([key, value]) => {
let argName = value.type === Array ? `${key}...` : key;
argName = value.required ? `<${argName}>` : `[${argName}]`;
this.argument(argName, value.description);
const argument = new Argument(argName, value.description);
if (value.choices) {
argument.choices(value.choices.map(choice => (typeof choice === 'string' ? choice : choice.value)));
}
this.addArgument(argument);
});
return this;
}
Expand Down
9 changes: 6 additions & 3 deletions lib/command/converter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { JHipsterOptionDefinition } from '../jdl/core/types/parsing.js';
import type { ConfigSpec, JHipsterArguments, JHipsterConfigs, JHipsterOption } from './types.js';
import type { ConfigSpec, JHipsterArguments, JHipsterConfigs, JHipsterOption, JHispterChoices } from './types.js';

export const extractArgumentsFromConfigs = (configs: JHipsterConfigs | undefined): JHipsterArguments => {
type JHipsterArgumentsWithChoices = JHipsterArguments & { choices?: JHispterChoices };

export const extractArgumentsFromConfigs = (configs: JHipsterConfigs | undefined): JHipsterArgumentsWithChoices => {
if (!configs) return {};
return Object.fromEntries(
Object.entries(configs)
Expand All @@ -11,10 +13,11 @@ export const extractArgumentsFromConfigs = (configs: JHipsterConfigs | undefined
{
description: def.description,
scope: def.scope,
choices: def.choices,
...def.argument,
},
]),
) as JHipsterArguments;
) as JHipsterArgumentsWithChoices;
};

export const extractJdlDefinitionFromCommandConfig = (configs: JHipsterConfigs = {}): JHipsterOptionDefinition[] =>
Expand Down

0 comments on commit ea244b0

Please sign in to comment.