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

double hyphen is parsed as an argument when defaultOption is set #119

Open
bakkot opened this issue Nov 2, 2021 · 0 comments
Open

double hyphen is parsed as an argument when defaultOption is set #119

bakkot opened this issue Nov 2, 2021 · 0 comments

Comments

@bakkot
Copy link

bakkot commented Nov 2, 2021

The wiki discusses how to correctly handle a double hyphen / double dash (--), but the suggested solution doesn't work when defaultOption is set, particularly alongside multiple:

'use strict';

const commandLineArgs = require('command-line-args')

const optionDefinitions = [
  { name: 'verbose', alias: 'v', type: Boolean },
  { name: 'src', type: String, defaultOption: true, multiple: true },
  { name: 'timeout', alias: 't', type: Number }
]

const options = commandLineArgs(optionDefinitions, { stopAtFirstUnknown: true })

console.log(options)

run with

node main.js one -t 0 -- --verbose two

produces

{ src: [ 'one', '--', 'two' ], timeout: 0, verbose: true }

That's not what -- means: the whole point is that encountering -- should mean that --verbose is interpreted as a positional argument rather than as a flag.

On the other hand, if you have defaultOption: false, then it will stop when it encounters the first positional argument, and the above invocation will result in

{ _unknown: [ 'one', '-t', '0', '--', '--verbose', 'two' ] }

i.e. it will fail to parse -t.

So neither approach actually allows you to correctly handle --.


For now, I'm working around it by manually splicing out the -- part before invoking commandLineArgs:

'use strict';

const commandLineArgs = require('command-line-args')

const optionDefinitions = [
  { name: 'verbose', alias: 'v', type: Boolean },
  { name: 'src', type: String, defaultOption: true, multiple: true },
  { name: 'timeout', alias: 't', type: Number }
]

const { argv } = process

let notParsed = []
const dashDashIndex = argv.indexOf('--')
if (dashDashIndex !== -1) {
  notParsed = argv.splice(dashDashIndex + 1)
  argv.pop()
}

const options = commandLineArgs(optionDefinitions, { argv })

options.src = (options.src || []).concat(notParsed);

console.log(options)

which gives for the above invocation

{ src: [ 'one', '--verbose', 'two' ], timeout: 0 }

which is the right thing.

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

No branches or pull requests

1 participant