Make multi-arg options optional
Summary:
This PR fixes issue #331, where single-value options in the CLI handler were incorrectly consuming positional arguments. The parser now correctly distinguishes between single-value and multi-value options, ensuring options like -k or --kit consume only one argument, while options like --plugins can accept multiple values.
Key Changes:
-
Updated
OptionInterface:- Added
acceptsMultipleValues?: booleanto indicate if an option accepts multiple values.
- Added
-
Modified
optionMethod:- Now accepts an
acceptsMultipleValuesparameter (default isfalse).
- Now accepts an
-
Adjusted
parseOptionsMethod:- Single-value options consume only the next argument.
- Multi-value options collect arguments until another option or
--is encountered. - Introduced
--as a separator between options and positional arguments.
-
Updated
processSubCommandMethod:- Utilizes the index from
parseOptionsto correctly handle positional arguments.
- Utilizes the index from
-
Added Tests:
- Verified that single-value options do not consume positional arguments.
- Ensured multi-value options handle multiple values correctly using
--separator.
Examples of Corrected Behavior:
-
Single-Value Option:
npx create-robo -k activity myactivity- Parsed Options:
{ kit: 'activity' } - Positional Arguments:
['myactivity']
- Parsed Options:
-
Multi-Value Option with Separator:
npx create-robo --plugins @robojs/ai @robojs/sync -- myactivity- Parsed Options:
{ plugins: ['@robojs/ai', '@robojs/sync'] } - Positional Arguments:
['myactivity']
- Parsed Options:
Impact:
- Fixes Issue: Single-value options no longer consume positional arguments.
- Maintainer's Feedback Addressed: Multi-value options are supported as intended.
Testing:
- Tests Passing:
- All new and existing tests pass successfully.
- Validation:
- Confirmed that options and positional arguments are parsed correctly.
Notes:
- Users should use
--to separate multi-value options from positional arguments when necessary. - Documentation should be updated to reflect these changes and guide users accordingly.
Thank you for reviewing this PR.
The latest updates on your projects. Learn more about Vercel for Git ↗︎
| Name | Status | Preview | Comments | Updated (UTC) |
|---|---|---|---|---|
| robo | ✅ Ready (Inspect) | Visit Preview | 💬 Add feedback | Oct 28, 2024 8:37pm |
CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅
I have read the CLA Document and I hereby sign the CLA
Thank you for this! Can you please verify that multi-arg options are still possible? If so, how does the API distinguish between them?
For example, a --plugins option should still be able to accept multiple values. (e.g. "--plugins @robojs/ai @robojs/sync")
Thank you for this! Can you please verify that multi-arg options are still possible? If so, how does the API distinguish between them?
For example, a
--pluginsoption should still be able to accept multiple values. (e.g. "--plugins @robojs/ai @robojs/sync")
Hey @Pkmmte , I've made the changes based on your feedback. Please check it and let me know if this approach is good or any further changes are required.
Hi @Pkmmte,
Thank you for your feedback and for accepting the PR for Hacktoberfest. I’ve been working on addressing the issues mentioned, specifically ensuring options like -v after positional arguments are correctly parsed and not treated as file paths.
Here’s what I’ve tried so far:
-
Adjusted
parseOptionsincli-handler.ts: Refined the parsing logic to separate options from positional arguments and ensure options with spaces return single strings by default. -
Verified Argument Parsing with Tests: Added and confirmed tests for scenarios with options appearing after positional arguments, ensuring options are recognized correctly.
-
Encountering Persistent
ENOENTError: When running the command:npx robo build src/cli/commands/add.ts -vthe
-vflag continues to be interpreted as a positional argument, causing an attempt to locate a file at-v, and resulting in theENOENTerror. This behavior appears on both the main branch and my modified branch, suggesting it may be due to the overall handling of positional arguments in thebuildcommand.
Would you be able to provide additional guidance on addressing this issue? Specifically, are there any expected parsing behaviors or modifications within cli-handler.ts that would prevent options like -v from being treated as positional arguments in the context of the build command?
Thank you for your time and assistance.
Hello ! thanks for your contribution, I have just tested it out, and it seems to be working just fine for me, are you sure to be testing out the right branch ?
Hello ! thanks for your contribution, I have just tested it out, and it seems to be working just fine for me, are you sure to be testing out the right branch ?
Hi @Nazeofel, Thanks for testing it out. Is this the expected output or am I doing something wrong?
npx robo build src/cli/commands/add.ts -v
info - Building Robo...
node:internal/fs/promises:1032
const result = await PromisePrototypeThen(
^
Error: ENOENT: no such file or directory, stat '/home/rishi/code/robo.js/packages/robo/-v'
at async Object.stat (node:internal/fs/promises:1032:18)
at async traverse (file:///home/rishi/code/robo.js/packages/robo/dist/cli/utils/compiler.js:59:1
8) at async Object.buildCode (file:///home/rishi/code/robo.js/packages/robo/dist/cli/utils/compiler
.js:133:3) at async Command.buildAction [as _handler] (file:///home/rishi/code/robo.js/packages/robo/dist/c
li/commands/build/index.js:63:23) at async Command.processSubCommand (file:///home/rishi/code/robo.js/packages/robo/dist/cli/utils
/cli-handler.js:255:5) at async Command.processSubCommand (file:///home/rishi/code/robo.js/packages/robo/dist/cli/utils
/cli-handler.js:245:9) { errno: -2,
code: 'ENOENT',
syscall: 'stat',
path: '/home/rishi/code/robo.js/packages/robo/-v'
}
Node.js v22.9.0
But when I removed -v, I got this output(which is not an error I guess):
npx robo build src/cli/commands/add.ts
info - Building Robo...
Type Name Description
───────────────────────────────────────────────
Command Δ /help Displays a list of commands.
Event Δ interactionCreate
Event Δ ready
Δ = Automatically generated
Robo size: 836.92 kB
Built in 113ms
discord:error - DISCORD_TOKEN or DISCORD_CLIENT_ID not found in environment variables
Is this what you are talking about in the previous comment @Pkmmte?
Hey @Pkmmte,
I’ve tested the issue on the main branch as well and observed the following outputs in two cases:
-
Without
-v:npx robo build src/cli/commands/add.ts info - Building Robo... Type Name Description ─────────────────────────────────────────────── Command Δ /help Displays a list of commands. Event Δ interactionCreate Event Δ ready Δ = Automatically generated Robo size: 836.59 kB Built in 109ms discord:error - DISCORD_TOKEN or DISCORD_CLIENT_ID not found in environment variables -
With
-v:npx robo build src/cli/commands/add.ts -v info - Building Robo... node:internal/fs/promises:1032 const result = await PromisePrototypeThen( ^ Error: ENOENT: no such file or directory, stat '/home/rishi/code/robo.js/packages/robo/-v' at async Object.stat (node:internal/fs/promises:1032:18) at async traverse (file:///home/rishi/code/robo.js/packages/robo/dist/cli/utils/compiler.js:59:1 8) at async Object.buildCode (file:///home/rishi/code/robo.js/packages/robo/dist/cli/utils/compiler .js:133:3) at async Command.buildAction [as _handler] (file:///home/rishi/code/robo.js/packages/robo/dist/c li/commands/build/index.js:63:23) at async Command.processSubCommand (file:///home/rishi/code/robo.js/packages/robo/dist/cli/utils /cli-handler.js:255:5) at async Command.processSubCommand (file:///home/rishi/code/robo.js/packages/robo/dist/cli/utils /cli-handler.js:245:9) { errno: -2, code: 'ENOENT', syscall: 'stat', path: '/home/rishi/code/robo.js/packages/robo/-v' } Node.js v22.9.0
This ENOENT error seems unrelated to my changes since it’s happening on the main branch as well. Could you confirm if I’m missing something or if there’s another approach I should take? Your guidance would be greatly appreciated.
Thank you!
Hey ! it should work just fine on the main branch and latest version of Robo, just to confirm somethin, which OS are you running ?
Hey ! it should work just fine on the main branch and latest version of Robo, just to confirm somethin, which OS are you running ?
Ubuntu noble 24.04 x86_64
Hey sorry to get back to you so late, we cannot reproduce this issue at all on our environment with the main branch, tested on MacOS, Windows, and Linux too. would you be able to share your all Linux configuration where you are running the command please.