inquirerer 2.3.1 → 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 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
@@ -189,6 +191,34 @@ close(): void
189
191
  exit(): void
190
192
  ```
191
193
 
194
+ #### Managing Multiple Instances
195
+
196
+ When working with multiple `Inquirerer` instances that share the same input stream (typically `process.stdin`), only one instance should be actively prompting at a time. Each instance attaches its own keyboard listener, so having multiple active instances will cause duplicate or unexpected keypress behavior.
197
+
198
+ **Best practices:**
199
+
200
+ 1. **Reuse a single instance** - Create one `Inquirerer` instance and reuse it for all prompts:
201
+ ```typescript
202
+ const prompter = new Inquirerer();
203
+
204
+ // Use the same instance for multiple prompt sessions
205
+ const answers1 = await prompter.prompt({}, questions1);
206
+ const answers2 = await prompter.prompt({}, questions2);
207
+
208
+ prompter.close(); // Clean up when done
209
+ ```
210
+
211
+ 2. **Close before creating another** - If you need separate instances, close the first before using the second:
212
+ ```typescript
213
+ const prompter1 = new Inquirerer();
214
+ const answers1 = await prompter1.prompt({}, questions1);
215
+ prompter1.close(); // Important: close before creating another
216
+
217
+ const prompter2 = new Inquirerer();
218
+ const answers2 = await prompter2.prompt({}, questions2);
219
+ prompter2.close();
220
+ ```
221
+
192
222
  ### Question Types
193
223
 
194
224
  #### Text Question
@@ -392,6 +422,87 @@ Ensure questions appear in the correct order:
392
422
  ]
393
423
  ```
394
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
+
395
506
  ## Real-World Examples
396
507
 
397
508
  ### Project Setup Wizard
package/esm/keypress.js CHANGED
@@ -12,63 +12,24 @@ export const KEY_CODES = {
12
12
  BACKSPACE: '\x7f', // Commonly used BACKSPACE key in Unix-like systems
13
13
  BACKSPACE_LEGACY: '\x08' // For compatibility with some systems
14
14
  };
15
- const sharedInputStates = new WeakMap();
16
- function getOrCreateSharedState(input) {
17
- let state = sharedInputStates.get(input);
18
- if (!state) {
19
- const dataHandler = (key) => {
20
- const currentState = sharedInputStates.get(input);
21
- if (!currentState)
22
- return;
23
- // Only dispatch to the top of the active stack (current owner)
24
- const owner = currentState.activeStack[currentState.activeStack.length - 1];
25
- if (owner) {
26
- owner.handleKey(key);
27
- // Handle Ctrl+C via the current owner's process wrapper
28
- if (key === KEY_CODES.CTRL_C) {
29
- owner.exitProcess(0);
30
- }
31
- }
32
- };
33
- state = {
34
- dataHandler,
35
- instances: new Set(),
36
- activeStack: [],
37
- rawModeSet: false
38
- };
39
- sharedInputStates.set(input, state);
40
- input.on('data', dataHandler);
41
- }
42
- return state;
43
- }
44
- function removeFromSharedState(input, instance) {
45
- const state = sharedInputStates.get(input);
46
- if (!state)
47
- return;
48
- state.instances.delete(instance);
49
- // Remove from active stack as well
50
- const stackIndex = state.activeStack.indexOf(instance);
51
- if (stackIndex !== -1) {
52
- state.activeStack.splice(stackIndex, 1);
53
- }
54
- // If stack is now empty, disable raw mode
55
- if (state.activeStack.length === 0 && state.rawModeSet) {
56
- if (typeof input.setRawMode === 'function') {
57
- input.setRawMode(false);
58
- }
59
- state.rawModeSet = false;
60
- }
61
- if (state.instances.size === 0) {
62
- input.removeListener('data', state.dataHandler);
63
- sharedInputStates.delete(input);
64
- }
65
- }
15
+ /**
16
+ * Handles keyboard input for interactive prompts.
17
+ *
18
+ * **Important**: Only one TerminalKeypress instance should be actively listening
19
+ * on a given input stream at a time. If you need multiple Inquirerer instances,
20
+ * call `close()` on the first instance before using the second, or reuse a single
21
+ * instance for all prompts.
22
+ *
23
+ * Multiple instances sharing the same input stream (e.g., process.stdin) will
24
+ * each receive all keypresses, which can cause duplicate or unexpected behavior.
25
+ */
66
26
  export class TerminalKeypress {
67
27
  listeners = {};
28
+ active = true;
68
29
  noTty;
69
30
  input;
70
31
  proc;
71
- destroyed = false;
32
+ dataHandler = null;
72
33
  constructor(noTty = false, input = process.stdin, proc = defaultProcessWrapper) {
73
34
  this.noTty = noTty;
74
35
  this.input = input;
@@ -77,32 +38,22 @@ export class TerminalKeypress {
77
38
  this.input.resume();
78
39
  this.input.setEncoding('utf8');
79
40
  }
80
- this.registerWithSharedState();
81
- }
82
- registerWithSharedState() {
83
- const state = getOrCreateSharedState(this.input);
84
- state.instances.add(this);
41
+ this.setupListeners();
85
42
  }
86
43
  isTTY() {
87
44
  return !this.noTty;
88
45
  }
89
- isActive() {
90
- if (this.destroyed)
91
- return false;
92
- const state = sharedInputStates.get(this.input);
93
- if (!state)
94
- return false;
95
- // Active only if this instance is the current owner (top of stack)
96
- return state.activeStack[state.activeStack.length - 1] === this;
97
- }
98
- exitProcess(code) {
99
- this.proc.exit(code);
100
- }
101
- handleKey(key) {
102
- if (this.destroyed)
103
- return;
104
- const handlers = this.listeners[key];
105
- handlers?.forEach(handler => handler());
46
+ setupListeners() {
47
+ this.dataHandler = (key) => {
48
+ if (!this.active)
49
+ return;
50
+ const handlers = this.listeners[key];
51
+ handlers?.forEach(handler => handler());
52
+ if (key === KEY_CODES.CTRL_C) {
53
+ this.proc.exit(0);
54
+ }
55
+ };
56
+ this.input.on('data', this.dataHandler);
106
57
  }
107
58
  on(key, callback) {
108
59
  if (!this.listeners[key]) {
@@ -122,52 +73,23 @@ export class TerminalKeypress {
122
73
  this.listeners = {};
123
74
  }
124
75
  pause() {
125
- const state = sharedInputStates.get(this.input);
126
- if (!state)
127
- return;
128
- // Remove from active stack (from anywhere, not just top)
129
- const stackIndex = state.activeStack.indexOf(this);
130
- if (stackIndex !== -1) {
131
- state.activeStack.splice(stackIndex, 1);
132
- }
133
- // If stack is now empty, disable raw mode
134
- if (state.activeStack.length === 0 && state.rawModeSet) {
135
- if (this.isTTY() && typeof this.input.setRawMode === 'function') {
136
- this.input.setRawMode(false);
137
- }
138
- state.rawModeSet = false;
139
- }
76
+ this.active = false;
77
+ this.clearHandlers();
140
78
  }
141
79
  resume() {
142
- if (this.destroyed)
143
- return;
144
- const state = sharedInputStates.get(this.input);
145
- if (!state)
146
- return;
147
- // Move-to-top semantics: remove from anywhere in stack, then push to top
148
- const existingIndex = state.activeStack.indexOf(this);
149
- if (existingIndex !== -1) {
150
- state.activeStack.splice(existingIndex, 1);
151
- }
152
- state.activeStack.push(this);
153
- // Enable raw mode if TTY
80
+ this.active = true;
154
81
  if (this.isTTY() && typeof this.input.setRawMode === 'function') {
155
82
  this.input.setRawMode(true);
156
- state.rawModeSet = true;
157
83
  }
158
84
  }
159
85
  destroy() {
160
- if (this.destroyed)
161
- return;
162
- this.destroyed = true;
163
- this.clearHandlers();
164
- removeFromSharedState(this.input, this);
165
- const state = sharedInputStates.get(this.input);
166
- if (!state || state.instances.size === 0) {
167
- if (typeof this.input.setRawMode === 'function') {
168
- this.input.setRawMode(false);
169
- }
170
- this.input.pause();
86
+ if (typeof this.input.setRawMode === 'function') {
87
+ this.input.setRawMode(false);
88
+ }
89
+ this.input.pause();
90
+ if (this.dataHandler) {
91
+ this.input.removeListener('data', this.dataHandler);
92
+ this.dataHandler = null;
171
93
  }
172
94
  }
173
95
  }
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(argv, obj, questions);
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/keypress.d.ts CHANGED
@@ -14,18 +14,27 @@ export declare const KEY_CODES: {
14
14
  BACKSPACE: string;
15
15
  BACKSPACE_LEGACY: string;
16
16
  };
17
+ /**
18
+ * Handles keyboard input for interactive prompts.
19
+ *
20
+ * **Important**: Only one TerminalKeypress instance should be actively listening
21
+ * on a given input stream at a time. If you need multiple Inquirerer instances,
22
+ * call `close()` on the first instance before using the second, or reuse a single
23
+ * instance for all prompts.
24
+ *
25
+ * Multiple instances sharing the same input stream (e.g., process.stdin) will
26
+ * each receive all keypresses, which can cause duplicate or unexpected behavior.
27
+ */
17
28
  export declare class TerminalKeypress {
18
29
  private listeners;
30
+ private active;
19
31
  private noTty;
20
32
  private input;
21
33
  private proc;
22
- private destroyed;
34
+ private dataHandler;
23
35
  constructor(noTty?: boolean, input?: Readable, proc?: ProcessWrapper);
24
- private registerWithSharedState;
25
36
  isTTY(): boolean;
26
- isActive(): boolean;
27
- exitProcess(code?: number): void;
28
- handleKey(key: string): void;
37
+ private setupListeners;
29
38
  on(key: string, callback: KeyHandler): void;
30
39
  off(key: string, callback: KeyHandler): void;
31
40
  clearHandlers(): void;
package/keypress.js CHANGED
@@ -15,63 +15,24 @@ exports.KEY_CODES = {
15
15
  BACKSPACE: '\x7f', // Commonly used BACKSPACE key in Unix-like systems
16
16
  BACKSPACE_LEGACY: '\x08' // For compatibility with some systems
17
17
  };
18
- const sharedInputStates = new WeakMap();
19
- function getOrCreateSharedState(input) {
20
- let state = sharedInputStates.get(input);
21
- if (!state) {
22
- const dataHandler = (key) => {
23
- const currentState = sharedInputStates.get(input);
24
- if (!currentState)
25
- return;
26
- // Only dispatch to the top of the active stack (current owner)
27
- const owner = currentState.activeStack[currentState.activeStack.length - 1];
28
- if (owner) {
29
- owner.handleKey(key);
30
- // Handle Ctrl+C via the current owner's process wrapper
31
- if (key === exports.KEY_CODES.CTRL_C) {
32
- owner.exitProcess(0);
33
- }
34
- }
35
- };
36
- state = {
37
- dataHandler,
38
- instances: new Set(),
39
- activeStack: [],
40
- rawModeSet: false
41
- };
42
- sharedInputStates.set(input, state);
43
- input.on('data', dataHandler);
44
- }
45
- return state;
46
- }
47
- function removeFromSharedState(input, instance) {
48
- const state = sharedInputStates.get(input);
49
- if (!state)
50
- return;
51
- state.instances.delete(instance);
52
- // Remove from active stack as well
53
- const stackIndex = state.activeStack.indexOf(instance);
54
- if (stackIndex !== -1) {
55
- state.activeStack.splice(stackIndex, 1);
56
- }
57
- // If stack is now empty, disable raw mode
58
- if (state.activeStack.length === 0 && state.rawModeSet) {
59
- if (typeof input.setRawMode === 'function') {
60
- input.setRawMode(false);
61
- }
62
- state.rawModeSet = false;
63
- }
64
- if (state.instances.size === 0) {
65
- input.removeListener('data', state.dataHandler);
66
- sharedInputStates.delete(input);
67
- }
68
- }
18
+ /**
19
+ * Handles keyboard input for interactive prompts.
20
+ *
21
+ * **Important**: Only one TerminalKeypress instance should be actively listening
22
+ * on a given input stream at a time. If you need multiple Inquirerer instances,
23
+ * call `close()` on the first instance before using the second, or reuse a single
24
+ * instance for all prompts.
25
+ *
26
+ * Multiple instances sharing the same input stream (e.g., process.stdin) will
27
+ * each receive all keypresses, which can cause duplicate or unexpected behavior.
28
+ */
69
29
  class TerminalKeypress {
70
30
  listeners = {};
31
+ active = true;
71
32
  noTty;
72
33
  input;
73
34
  proc;
74
- destroyed = false;
35
+ dataHandler = null;
75
36
  constructor(noTty = false, input = process.stdin, proc = defaultProcessWrapper) {
76
37
  this.noTty = noTty;
77
38
  this.input = input;
@@ -80,32 +41,22 @@ class TerminalKeypress {
80
41
  this.input.resume();
81
42
  this.input.setEncoding('utf8');
82
43
  }
83
- this.registerWithSharedState();
84
- }
85
- registerWithSharedState() {
86
- const state = getOrCreateSharedState(this.input);
87
- state.instances.add(this);
44
+ this.setupListeners();
88
45
  }
89
46
  isTTY() {
90
47
  return !this.noTty;
91
48
  }
92
- isActive() {
93
- if (this.destroyed)
94
- return false;
95
- const state = sharedInputStates.get(this.input);
96
- if (!state)
97
- return false;
98
- // Active only if this instance is the current owner (top of stack)
99
- return state.activeStack[state.activeStack.length - 1] === this;
100
- }
101
- exitProcess(code) {
102
- this.proc.exit(code);
103
- }
104
- handleKey(key) {
105
- if (this.destroyed)
106
- return;
107
- const handlers = this.listeners[key];
108
- handlers?.forEach(handler => handler());
49
+ setupListeners() {
50
+ this.dataHandler = (key) => {
51
+ if (!this.active)
52
+ return;
53
+ const handlers = this.listeners[key];
54
+ handlers?.forEach(handler => handler());
55
+ if (key === exports.KEY_CODES.CTRL_C) {
56
+ this.proc.exit(0);
57
+ }
58
+ };
59
+ this.input.on('data', this.dataHandler);
109
60
  }
110
61
  on(key, callback) {
111
62
  if (!this.listeners[key]) {
@@ -125,52 +76,23 @@ class TerminalKeypress {
125
76
  this.listeners = {};
126
77
  }
127
78
  pause() {
128
- const state = sharedInputStates.get(this.input);
129
- if (!state)
130
- return;
131
- // Remove from active stack (from anywhere, not just top)
132
- const stackIndex = state.activeStack.indexOf(this);
133
- if (stackIndex !== -1) {
134
- state.activeStack.splice(stackIndex, 1);
135
- }
136
- // If stack is now empty, disable raw mode
137
- if (state.activeStack.length === 0 && state.rawModeSet) {
138
- if (this.isTTY() && typeof this.input.setRawMode === 'function') {
139
- this.input.setRawMode(false);
140
- }
141
- state.rawModeSet = false;
142
- }
79
+ this.active = false;
80
+ this.clearHandlers();
143
81
  }
144
82
  resume() {
145
- if (this.destroyed)
146
- return;
147
- const state = sharedInputStates.get(this.input);
148
- if (!state)
149
- return;
150
- // Move-to-top semantics: remove from anywhere in stack, then push to top
151
- const existingIndex = state.activeStack.indexOf(this);
152
- if (existingIndex !== -1) {
153
- state.activeStack.splice(existingIndex, 1);
154
- }
155
- state.activeStack.push(this);
156
- // Enable raw mode if TTY
83
+ this.active = true;
157
84
  if (this.isTTY() && typeof this.input.setRawMode === 'function') {
158
85
  this.input.setRawMode(true);
159
- state.rawModeSet = true;
160
86
  }
161
87
  }
162
88
  destroy() {
163
- if (this.destroyed)
164
- return;
165
- this.destroyed = true;
166
- this.clearHandlers();
167
- removeFromSharedState(this.input, this);
168
- const state = sharedInputStates.get(this.input);
169
- if (!state || state.instances.size === 0) {
170
- if (typeof this.input.setRawMode === 'function') {
171
- this.input.setRawMode(false);
172
- }
173
- this.input.pause();
89
+ if (typeof this.input.setRawMode === 'function') {
90
+ this.input.setRawMode(false);
91
+ }
92
+ this.input.pause();
93
+ if (this.dataHandler) {
94
+ this.input.removeListener('data', this.dataHandler);
95
+ this.dataHandler = null;
174
96
  }
175
97
  }
176
98
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inquirerer",
3
- "version": "2.3.1",
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": "b8ebff70a30df8ee4fb60794f7ba7842730ba199"
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(argv, obj, questions);
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) {
@@ -15,6 +15,7 @@ export interface Validation {
15
15
  export interface BaseQuestion {
16
16
  name: string;
17
17
  type: string;
18
+ _?: boolean;
18
19
  default?: any;
19
20
  defaultFrom?: string;
20
21
  setFrom?: string;