Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parseArgs - support positional arguments per option key #54022

Open
Asaf-Federman opened this issue Jul 24, 2024 · 3 comments
Open

parseArgs - support positional arguments per option key #54022

Asaf-Federman opened this issue Jul 24, 2024 · 3 comments
Labels
feature request Issues that request new features to be added to Node.js. util Issues and PRs related to the built-in util module.

Comments

@Asaf-Federman
Copy link

Asaf-Federman commented Jul 24, 2024

What is the problem this feature will solve?

I would like to provide multiple positional arguments per option key.
For example, I'd like to provide a list of files to read and a list of files to write
Today, I'd have to write it with the following syntax node index.js --file-to-read file1.txt --file-to-read file2.txt --file-to-write file3.txt --file-to-write file4.txt which is a bit tedious to the user

What is the feature you are proposing to solve the problem?

I propose the following syntax
node index.js --files-to-read file1.txt file2.txt --files-to-write file3.txt file4.txt

What alternatives have you considered?

I'm currently using the aforementioned syntax which is a bit tedious to the end user in my opinion

@Asaf-Federman Asaf-Federman added the feature request Issues that request new features to be added to Node.js. label Jul 24, 2024
@bakkot
Copy link

bakkot commented Jul 25, 2024

This is a pretty unusual design for a CLI. parseArgs is intended as a basic utility, not something which supports every possible CLI design; I don't think it's worth supporting this one.

You can do this with the tokens array as follows, though:

import { parseArgs } from 'node:util';

let { tokens } = parseArgs({
  options: {
    'files-to-read': { type: 'boolean' },
    'files-to-write': { type: 'boolean' },
  },
  allowPositionals: true,
  tokens: true,
});

let read = [];
let write = [];

let target = null;
for (let token of tokens) {
  if (token.kind === 'option') {
    target = token.name === 'files-to-read' ? read : write;
  } else if (target === null || token.kind === 'option-terminator') {
    throw new Error('names must be specified after --files-to-read or --files-to-write');
  } else {
    target.push(token.value);
  }
}

console.log({ read, write });
$ node parseargs-pos.mjs --files-to-read file1.txt file2.txt --files-to-write file3.txt file4.txt
{
  read: [ 'file1.txt', 'file2.txt' ],
  write: [ 'file3.txt', 'file4.txt' ]
}

@Asaf-Federman
Copy link
Author

I don't mind invest time in order to add this option.
The question is whether it's something worth adding to the codebase (in your opinion)

@VoltrexKeyva VoltrexKeyva added the util Issues and PRs related to the built-in util module. label Jul 25, 2024
@rstagi
Copy link

rstagi commented Jul 28, 2024

My 2 cents: as a user I wouldn't expect the --files-to-read file1.txt file2.txt format tbh.

I'd rather expect some other more common ways like comma-separated or space-separated lists:

# comma-separated list
$ node arseargs-pos.mjs --files-to-read file1.txt,file2.txt

# space-separated list, but explicitly passed as a single string
$ node arseargs-pos.mjs --files-to-read "file1.txt file2.txt"

Which are already feasible with the current features.

Having the option you propose would be confusing to me. I hardly believe it would be widely used, and it would probably end up being a uselessly maintained feature in the long run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. util Issues and PRs related to the built-in util module.
Projects
Status: Awaiting Triage
Development

No branches or pull requests

4 participants