1
0
Эх сурвалжийг харах

refactor(cli): Separate long- and shorthands for options

HouseinIsProgramming 7 сар өмнө
parent
commit
a3d3f40569

+ 7 - 38
packages/cli/src/commands/command-declarations.ts

@@ -1,5 +1,3 @@
-import { log } from '@clack/prompts';
-
 import { CliCommandDefinition } from '../shared/cli-command-definition';
 
 export const cliCommands: CliCommandDefinition[] = [
@@ -17,22 +15,25 @@ export const cliCommands: CliCommandDefinition[] = [
         description: 'Generate, run or revert a database migration',
         options: [
             {
-                flag: '-g, --generate <name>',
+                short: '-g',
+                long: '--generate <name>',
                 description: 'Generate a new migration with the specified name',
                 required: false,
             },
             {
-                flag: '-r, --run',
+                short: '-r',
+                long: '--run',
                 description: 'Run pending migrations',
                 required: false,
             },
             {
-                flag: '--revert',
+                long: '--revert',
                 description: 'Revert the last migration',
                 required: false,
             },
             {
-                flag: '-o, --output-dir <path>',
+                short: '-o',
+                long: '--output-dir <path>',
                 description: 'Output directory for generated migrations',
                 required: false,
             },
@@ -43,36 +44,4 @@ export const cliCommands: CliCommandDefinition[] = [
             process.exit(0);
         },
     },
-    // Example of a command with options
-    {
-        name: 'example',
-        description: 'Example command with options',
-        options: [
-            {
-                flag: '-f, --file <path>',
-                description: 'Path to the file',
-                required: true,
-            },
-            {
-                flag: '-v, --verbose',
-                description: 'Enable verbose output',
-                required: false,
-                defaultValue: false,
-            },
-        ],
-        action: options => {
-            // Example action implementation with options
-            log.info('Example command executed');
-            if (options) {
-                // Validate required options
-                if (!options.file) {
-                    log.error('Error: --file option is required');
-                    process.exit(1);
-                }
-                log.info(`File path: ${String(options.file)}`);
-                log.info(`Verbose mode: ${String(options.verbose)}`);
-            }
-            process.exit(0);
-        },
-    },
 ];

+ 2 - 1
packages/cli/src/shared/cli-command-definition.ts

@@ -1,5 +1,6 @@
 export interface CliCommandOption {
-    flag: string;
+    long: string;
+    short?: string;
     description: string;
     required?: boolean;
     defaultValue?: any;

+ 12 - 4
packages/cli/src/shared/command-registry.ts

@@ -14,10 +14,18 @@ export function registerCommands(program: Command, commands: CliCommandDefinitio
         // Add options if they exist
         if (commandDef.options) {
             commandDef.options.forEach(option => {
-                // Handle both required and optional options
-                const optionString = option.required
-                    ? option.flag
-                    : option.flag.replace(/<([^>]+)>/g, '[$1]');
+                const parts: string[] = [];
+                if (option.short) {
+                    parts.push(option.short);
+                }
+                parts.push(option.long);
+
+                let optionString = parts.join(', ');
+
+                // Handle optional options which expect a value by converting <value> to [value]
+                if (!option.required) {
+                    optionString = optionString.replace(/<([^>]+)>/g, '[$1]');
+                }
 
                 command.option(optionString, option.description, option.defaultValue);
             });