inquirerer 2.3.2 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +83 -0
- package/esm/prompt.js +62 -1
- package/package.json +2 -2
- package/prompt.d.ts +15 -0
- package/prompt.js +62 -1
- package/question/types.d.ts +1 -0
package/README.md
CHANGED
|
@@ -50,6 +50,7 @@ npm install inquirerer
|
|
|
50
50
|
- [Autocomplete Question](#autocomplete-question)
|
|
51
51
|
- [Checkbox Question](#checkbox-question)
|
|
52
52
|
- [Advanced Question Options](#advanced-question-options)
|
|
53
|
+
- [Positional Arguments](#positional-arguments)
|
|
53
54
|
- [Real-World Examples](#real-world-examples)
|
|
54
55
|
- [Project Setup Wizard](#project-setup-wizard)
|
|
55
56
|
- [Configuration Builder](#configuration-builder)
|
|
@@ -129,6 +130,7 @@ All questions support these base properties:
|
|
|
129
130
|
interface BaseQuestion {
|
|
130
131
|
name: string; // Property name in result object
|
|
131
132
|
type: string; // Question type
|
|
133
|
+
_?: boolean; // Mark as positional argument (can be passed without --name flag)
|
|
132
134
|
message?: string; // Prompt message to display
|
|
133
135
|
description?: string; // Additional context
|
|
134
136
|
default?: any; // Default value
|
|
@@ -420,6 +422,87 @@ Ensure questions appear in the correct order:
|
|
|
420
422
|
]
|
|
421
423
|
```
|
|
422
424
|
|
|
425
|
+
### Positional Arguments
|
|
426
|
+
|
|
427
|
+
The `_` property allows you to name positional parameters, enabling users to pass values without flags. This is useful for CLI tools where the first few arguments have obvious meanings.
|
|
428
|
+
|
|
429
|
+
#### Basic Usage
|
|
430
|
+
|
|
431
|
+
```typescript
|
|
432
|
+
const questions: Question[] = [
|
|
433
|
+
{
|
|
434
|
+
_: true,
|
|
435
|
+
name: 'database',
|
|
436
|
+
type: 'text',
|
|
437
|
+
message: 'Database name',
|
|
438
|
+
required: true
|
|
439
|
+
}
|
|
440
|
+
];
|
|
441
|
+
|
|
442
|
+
const argv = minimist(process.argv.slice(2));
|
|
443
|
+
const result = await prompter.prompt(argv, questions);
|
|
444
|
+
```
|
|
445
|
+
|
|
446
|
+
Now users can run either:
|
|
447
|
+
```bash
|
|
448
|
+
node myprogram.js mydb1
|
|
449
|
+
# or equivalently:
|
|
450
|
+
node myprogram.js --database mydb1
|
|
451
|
+
```
|
|
452
|
+
|
|
453
|
+
#### Multiple Positional Arguments
|
|
454
|
+
|
|
455
|
+
Positional arguments are assigned in declaration order:
|
|
456
|
+
|
|
457
|
+
```typescript
|
|
458
|
+
const questions: Question[] = [
|
|
459
|
+
{ _: true, name: 'source', type: 'text', message: 'Source file' },
|
|
460
|
+
{ name: 'verbose', type: 'confirm', default: false },
|
|
461
|
+
{ _: true, name: 'destination', type: 'text', message: 'Destination file' }
|
|
462
|
+
];
|
|
463
|
+
|
|
464
|
+
// Running: node copy.js input.txt output.txt --verbose
|
|
465
|
+
// Results in: { source: 'input.txt', destination: 'output.txt', verbose: true }
|
|
466
|
+
```
|
|
467
|
+
|
|
468
|
+
#### Named Arguments Take Precedence
|
|
469
|
+
|
|
470
|
+
When both positional and named arguments are provided, named arguments win and the positional slot is preserved for the next positional question:
|
|
471
|
+
|
|
472
|
+
```typescript
|
|
473
|
+
const questions: Question[] = [
|
|
474
|
+
{ _: true, name: 'foo', type: 'text' },
|
|
475
|
+
{ _: true, name: 'bar', type: 'text' },
|
|
476
|
+
{ _: true, name: 'baz', type: 'text' }
|
|
477
|
+
];
|
|
478
|
+
|
|
479
|
+
// Running: node myprogram.js pos1 pos2 --bar named-bar
|
|
480
|
+
// Results in: { foo: 'pos1', bar: 'named-bar', baz: 'pos2' }
|
|
481
|
+
```
|
|
482
|
+
|
|
483
|
+
In this example, `bar` gets its value from the named flag, so the two positional values go to `foo` and `baz`.
|
|
484
|
+
|
|
485
|
+
#### Positional with Options
|
|
486
|
+
|
|
487
|
+
Positional arguments work with list, autocomplete, and checkbox questions. The value is mapped through the options:
|
|
488
|
+
|
|
489
|
+
```typescript
|
|
490
|
+
const questions: Question[] = [
|
|
491
|
+
{
|
|
492
|
+
_: true,
|
|
493
|
+
name: 'framework',
|
|
494
|
+
type: 'list',
|
|
495
|
+
options: [
|
|
496
|
+
{ name: 'React', value: 'react' },
|
|
497
|
+
{ name: 'Vue', value: 'vue' }
|
|
498
|
+
]
|
|
499
|
+
}
|
|
500
|
+
];
|
|
501
|
+
|
|
502
|
+
// Running: node setup.js React
|
|
503
|
+
// Results in: { framework: 'react' }
|
|
504
|
+
```
|
|
505
|
+
|
|
423
506
|
## Real-World Examples
|
|
424
507
|
|
|
425
508
|
### Project Setup Wizard
|
package/esm/prompt.js
CHANGED
|
@@ -294,15 +294,32 @@ export class Inquirerer {
|
|
|
294
294
|
async prompt(argv, questions, options) {
|
|
295
295
|
// use local mutateArgs if defined, otherwise global mutateArgs
|
|
296
296
|
const shouldMutate = options?.mutateArgs !== undefined ? options.mutateArgs : this.mutateArgs;
|
|
297
|
+
// Create a working copy of argv - deep clone the _ array to avoid shared reference
|
|
297
298
|
let obj = shouldMutate ? argv : { ...argv };
|
|
299
|
+
const argvAny = argv;
|
|
300
|
+
if (!shouldMutate && Array.isArray(argvAny._)) {
|
|
301
|
+
obj._ = [...argvAny._];
|
|
302
|
+
}
|
|
298
303
|
// Resolve dynamic defaults before processing questions
|
|
299
304
|
await this.resolveDynamicDefaults(questions);
|
|
300
305
|
// Resolve dynamic options before processing questions
|
|
301
306
|
await this.resolveOptionsFrom(questions);
|
|
302
307
|
// Resolve setFrom values - these bypass prompting entirely
|
|
303
308
|
await this.resolveSetValues(questions, obj);
|
|
309
|
+
// Extract positional arguments from argv._ and assign to questions with _: true
|
|
310
|
+
// This must happen before applyOverrides so positional values flow through override pipeline
|
|
311
|
+
// Returns the number of positional arguments consumed for stripping
|
|
312
|
+
const consumedCount = this.extractPositionalArgs(obj, questions);
|
|
313
|
+
// Strip consumed positionals from obj._ (the working copy)
|
|
314
|
+
if (consumedCount > 0 && Array.isArray(obj._)) {
|
|
315
|
+
obj._ = obj._.slice(consumedCount);
|
|
316
|
+
// If mutating, also update the original argv._
|
|
317
|
+
if (shouldMutate) {
|
|
318
|
+
argvAny._ = obj._;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
304
321
|
// first loop through the question, and set any overrides in case other questions use objs for validation
|
|
305
|
-
this.applyOverrides(
|
|
322
|
+
this.applyOverrides(obj, obj, questions);
|
|
306
323
|
// Check for required arguments when no terminal is available (non-interactive mode)
|
|
307
324
|
if (this.noTty && this.hasMissingRequiredArgs(questions, argv)) {
|
|
308
325
|
// Apply default values for all questions
|
|
@@ -433,6 +450,50 @@ export class Inquirerer {
|
|
|
433
450
|
}
|
|
434
451
|
}
|
|
435
452
|
}
|
|
453
|
+
/**
|
|
454
|
+
* Extracts positional arguments from obj._ and assigns them to questions marked with _: true.
|
|
455
|
+
*
|
|
456
|
+
* Rules:
|
|
457
|
+
* 1. Named arguments take precedence - if a question already has a value in obj, skip it
|
|
458
|
+
* 2. Positional questions consume from obj._ left-to-right in declaration order
|
|
459
|
+
* 3. Returns the count of consumed positionals so caller can strip them from obj._
|
|
460
|
+
* 4. Missing positional values leave questions unset (for prompting/validation)
|
|
461
|
+
*
|
|
462
|
+
* This effectively allows "naming positional parameters" - users can pass values
|
|
463
|
+
* without flags and they'll be assigned to the appropriate question names.
|
|
464
|
+
*
|
|
465
|
+
* @returns The number of positional arguments consumed
|
|
466
|
+
*/
|
|
467
|
+
extractPositionalArgs(obj, questions) {
|
|
468
|
+
// Get positional arguments array from obj (minimist convention)
|
|
469
|
+
const positionals = Array.isArray(obj._) ? obj._ : [];
|
|
470
|
+
if (positionals.length === 0) {
|
|
471
|
+
return 0;
|
|
472
|
+
}
|
|
473
|
+
// Track which positional index we're consuming from
|
|
474
|
+
let positionalIndex = 0;
|
|
475
|
+
// Process questions in declaration order to maintain predictable assignment
|
|
476
|
+
for (const question of questions) {
|
|
477
|
+
// Only process questions marked as positional
|
|
478
|
+
if (!question._) {
|
|
479
|
+
continue;
|
|
480
|
+
}
|
|
481
|
+
// Skip if this question already has a named argument value
|
|
482
|
+
// Named arguments always take precedence over positional
|
|
483
|
+
if (question.name in obj && question.name !== '_') {
|
|
484
|
+
continue;
|
|
485
|
+
}
|
|
486
|
+
// Skip if we've exhausted all positional arguments
|
|
487
|
+
if (positionalIndex >= positionals.length) {
|
|
488
|
+
break;
|
|
489
|
+
}
|
|
490
|
+
// Assign the next positional value to this question
|
|
491
|
+
const value = positionals[positionalIndex];
|
|
492
|
+
obj[question.name] = value;
|
|
493
|
+
positionalIndex++;
|
|
494
|
+
}
|
|
495
|
+
return positionalIndex;
|
|
496
|
+
}
|
|
436
497
|
applyDefaultValues(questions, obj) {
|
|
437
498
|
questions.forEach(question => {
|
|
438
499
|
if ('default' in question) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "inquirerer",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.0",
|
|
4
4
|
"author": "Constructive <developers@constructive.io>",
|
|
5
5
|
"description": "TypeScript-first library for building beautiful CLI interfaces with interactive prompts",
|
|
6
6
|
"main": "index.js",
|
|
@@ -41,5 +41,5 @@
|
|
|
41
41
|
"clean-ansi": "0.1.5",
|
|
42
42
|
"makage": "0.1.8"
|
|
43
43
|
},
|
|
44
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "448f7aba6faabf4b25ddbb1c25395933fb51e9b8"
|
|
45
45
|
}
|
package/prompt.d.ts
CHANGED
|
@@ -80,6 +80,21 @@ export declare class Inquirerer {
|
|
|
80
80
|
* Updates the question.options property with the resolved array.
|
|
81
81
|
*/
|
|
82
82
|
private resolveOptionsFrom;
|
|
83
|
+
/**
|
|
84
|
+
* Extracts positional arguments from obj._ and assigns them to questions marked with _: true.
|
|
85
|
+
*
|
|
86
|
+
* Rules:
|
|
87
|
+
* 1. Named arguments take precedence - if a question already has a value in obj, skip it
|
|
88
|
+
* 2. Positional questions consume from obj._ left-to-right in declaration order
|
|
89
|
+
* 3. Returns the count of consumed positionals so caller can strip them from obj._
|
|
90
|
+
* 4. Missing positional values leave questions unset (for prompting/validation)
|
|
91
|
+
*
|
|
92
|
+
* This effectively allows "naming positional parameters" - users can pass values
|
|
93
|
+
* without flags and they'll be assigned to the appropriate question names.
|
|
94
|
+
*
|
|
95
|
+
* @returns The number of positional arguments consumed
|
|
96
|
+
*/
|
|
97
|
+
private extractPositionalArgs;
|
|
83
98
|
private applyDefaultValues;
|
|
84
99
|
private applyOverrides;
|
|
85
100
|
private handleOverrides;
|
package/prompt.js
CHANGED
|
@@ -301,15 +301,32 @@ class Inquirerer {
|
|
|
301
301
|
async prompt(argv, questions, options) {
|
|
302
302
|
// use local mutateArgs if defined, otherwise global mutateArgs
|
|
303
303
|
const shouldMutate = options?.mutateArgs !== undefined ? options.mutateArgs : this.mutateArgs;
|
|
304
|
+
// Create a working copy of argv - deep clone the _ array to avoid shared reference
|
|
304
305
|
let obj = shouldMutate ? argv : { ...argv };
|
|
306
|
+
const argvAny = argv;
|
|
307
|
+
if (!shouldMutate && Array.isArray(argvAny._)) {
|
|
308
|
+
obj._ = [...argvAny._];
|
|
309
|
+
}
|
|
305
310
|
// Resolve dynamic defaults before processing questions
|
|
306
311
|
await this.resolveDynamicDefaults(questions);
|
|
307
312
|
// Resolve dynamic options before processing questions
|
|
308
313
|
await this.resolveOptionsFrom(questions);
|
|
309
314
|
// Resolve setFrom values - these bypass prompting entirely
|
|
310
315
|
await this.resolveSetValues(questions, obj);
|
|
316
|
+
// Extract positional arguments from argv._ and assign to questions with _: true
|
|
317
|
+
// This must happen before applyOverrides so positional values flow through override pipeline
|
|
318
|
+
// Returns the number of positional arguments consumed for stripping
|
|
319
|
+
const consumedCount = this.extractPositionalArgs(obj, questions);
|
|
320
|
+
// Strip consumed positionals from obj._ (the working copy)
|
|
321
|
+
if (consumedCount > 0 && Array.isArray(obj._)) {
|
|
322
|
+
obj._ = obj._.slice(consumedCount);
|
|
323
|
+
// If mutating, also update the original argv._
|
|
324
|
+
if (shouldMutate) {
|
|
325
|
+
argvAny._ = obj._;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
311
328
|
// first loop through the question, and set any overrides in case other questions use objs for validation
|
|
312
|
-
this.applyOverrides(
|
|
329
|
+
this.applyOverrides(obj, obj, questions);
|
|
313
330
|
// Check for required arguments when no terminal is available (non-interactive mode)
|
|
314
331
|
if (this.noTty && this.hasMissingRequiredArgs(questions, argv)) {
|
|
315
332
|
// Apply default values for all questions
|
|
@@ -440,6 +457,50 @@ class Inquirerer {
|
|
|
440
457
|
}
|
|
441
458
|
}
|
|
442
459
|
}
|
|
460
|
+
/**
|
|
461
|
+
* Extracts positional arguments from obj._ and assigns them to questions marked with _: true.
|
|
462
|
+
*
|
|
463
|
+
* Rules:
|
|
464
|
+
* 1. Named arguments take precedence - if a question already has a value in obj, skip it
|
|
465
|
+
* 2. Positional questions consume from obj._ left-to-right in declaration order
|
|
466
|
+
* 3. Returns the count of consumed positionals so caller can strip them from obj._
|
|
467
|
+
* 4. Missing positional values leave questions unset (for prompting/validation)
|
|
468
|
+
*
|
|
469
|
+
* This effectively allows "naming positional parameters" - users can pass values
|
|
470
|
+
* without flags and they'll be assigned to the appropriate question names.
|
|
471
|
+
*
|
|
472
|
+
* @returns The number of positional arguments consumed
|
|
473
|
+
*/
|
|
474
|
+
extractPositionalArgs(obj, questions) {
|
|
475
|
+
// Get positional arguments array from obj (minimist convention)
|
|
476
|
+
const positionals = Array.isArray(obj._) ? obj._ : [];
|
|
477
|
+
if (positionals.length === 0) {
|
|
478
|
+
return 0;
|
|
479
|
+
}
|
|
480
|
+
// Track which positional index we're consuming from
|
|
481
|
+
let positionalIndex = 0;
|
|
482
|
+
// Process questions in declaration order to maintain predictable assignment
|
|
483
|
+
for (const question of questions) {
|
|
484
|
+
// Only process questions marked as positional
|
|
485
|
+
if (!question._) {
|
|
486
|
+
continue;
|
|
487
|
+
}
|
|
488
|
+
// Skip if this question already has a named argument value
|
|
489
|
+
// Named arguments always take precedence over positional
|
|
490
|
+
if (question.name in obj && question.name !== '_') {
|
|
491
|
+
continue;
|
|
492
|
+
}
|
|
493
|
+
// Skip if we've exhausted all positional arguments
|
|
494
|
+
if (positionalIndex >= positionals.length) {
|
|
495
|
+
break;
|
|
496
|
+
}
|
|
497
|
+
// Assign the next positional value to this question
|
|
498
|
+
const value = positionals[positionalIndex];
|
|
499
|
+
obj[question.name] = value;
|
|
500
|
+
positionalIndex++;
|
|
501
|
+
}
|
|
502
|
+
return positionalIndex;
|
|
503
|
+
}
|
|
443
504
|
applyDefaultValues(questions, obj) {
|
|
444
505
|
questions.forEach(question => {
|
|
445
506
|
if ('default' in question) {
|