Skip to content

Conversation

@pyramation
Copy link
Contributor

@pyramation pyramation commented Dec 25, 2025

feat(inquirerer): add positional arguments support with _ property

Summary

This PR adds the ability to "name positional parameters" in inquirerer. Questions marked with _: true can receive values from argv._ (minimist's positional array) without requiring the --name flag.

Example usage:

const questions = [
  { _: true, name: 'database', type: 'text', required: true }
];
// Users can now run: node myprogram.js mydb1
// Instead of: node myprogram.js --database mydb1

Key behaviors:

  • Positional arguments are assigned in question declaration order
  • Named arguments (--name value) take precedence over positional values
  • When a named arg is provided, its positional slot is preserved for the next positional question
  • Positional values flow through the override pipeline (option mapping for list/autocomplete/checkbox works correctly)
  • Consumed positionals are stripped from result._ - remaining extras stay in the array

Updates since last revision

Fixed mutateArgs behavior and added positional stripping:

  • extractPositionalArgs now returns the count of consumed positionals
  • Consumed positionals are stripped from result._ (e.g., { _: ['a', 'b', 'c'] } with one positional question becomes { _: ['b', 'c'], first: 'a' })
  • When mutateArgs: true (default): original argv._ is also stripped
  • When mutateArgs: false: original argv is completely untouched (deep clone of _ array)

Review & Testing Checklist for Human

  • Verify stripping behavior is desired: Consumed positionals are now removed from argv._. Test that node app.js a b c with one positional question results in { first: 'a', _: ['b', 'c'] } - confirm this is the expected behavior for your use cases
  • Test mutateArgs: false isolation: Verify that with mutateArgs: false, the original argv object (including argv._) is completely unchanged after prompt() returns
  • Test with actual minimist output: Ensure the feature works correctly with real minimist(process.argv.slice(2)) output, not just test fixtures

Recommended test plan:

  1. Create a simple CLI script using inquirerer with 2-3 positional questions
  2. Test various invocation patterns: all positional, all named, mixed positional/named
  3. Verify result._ contains only unconsumed positional values
  4. Test with mutateArgs: false and confirm original argv is unchanged

Notes

  • 31 new tests added covering corner cases including: mixed positional/named, interleaved questions, option mapping, checkbox handling, mutateArgs behavior, required fields, and edge cases
  • All 121 tests pass (90 existing + 31 new)

Devin session: https://app.devin.ai/sessions/43a1b03d22ef47bb866a272558cf3809
Requested by: Dan Lynch (@pyramation)

- Add _?: boolean property to BaseQuestion interface to mark questions as positional
- Implement extractPositionalArgs method to assign argv._ values to positional questions
- Positional arguments are assigned in declaration order
- Named arguments take precedence over positional (positional slot preserved for next question)
- Values flow through override pipeline for list/autocomplete/checkbox option mapping
- Add comprehensive test suite with 31 tests covering corner cases:
  - Basic positional argument handling
  - Named arguments taking precedence
  - Extra and missing positional values
  - Interleaved positional and non-positional questions
  - Positional with list/autocomplete/checkbox options
  - mutateArgs behavior
  - Required positional questions
  - Edge cases and complex scenarios
- Update README documentation with Positional Arguments section
@devin-ai-integration
Copy link

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

…utateArgs

- extractPositionalArgs now returns the count of consumed positionals
- Consumed positionals are stripped from result._ (remaining extras stay)
- When mutateArgs is true (default), original argv._ is also stripped
- When mutateArgs is false, original argv is not mutated at all
- Deep clone argv._ array when mutateArgs is false to avoid shared reference
- Updated tests to verify new stripping behavior
@pyramation pyramation merged commit e2e4ccd into main Dec 25, 2025
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants