question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

parsing of strings containing flag notation

See original GitHub issue
#!/usr/bin/env node
const mri = require('mri')
console.log(process.argv)
console.log(mri(process.argv.slice(2)))
./args.js -a -b '-c foo'
[
  '/usr/local/Cellar/node/16.0.0_1/bin/node',
  '/Users/j/web/pev2-cli/foo.js',
  '-a',
  '-b',
  '-c foo'
]
{
  _: [],
  a: true,
  b: true,
  c: true,
  ' ': true,
  f: true,
  o: [ true, true ]
}

Is that intended?

I would have expected this:

{
  _: ['-c foo'],
  a: true,
  b: true
}

Or maybe this (but I find the result above much more intuitive):

{
  _: [],
  a: true,
  b: true,
  c: 'foo'
}

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
sntrancommented, Dec 31, 2021

I would also like to add a simpler case, in which mri parses incorrectly:

./args.js -

The hyphen (-) is very common to indicate stdin or stdout, but when parsed with mri, the result is:

{ _: [] }

Whereas, - should be part of _ key.

1reaction
derhuerstcommented, May 26, 2021

I think you’d have to do this: ./args.js -a -b -- '-c foo' or maybe ./args.js -a -b -- -c foo

Anything after a -- is always thrown into the _ key space.

That doesn’t solve my problem unfortunately, because I would like to treat -b like any other option/flag: I want to be able to put other flags behind. Using -- defeats the point of using a flag and leveraging mri’s parsing logic.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use flags with python's regular expressions in string ...
I was wondering how to write more complex regular expressions using this notation, specifically how to use the IGNORECASE flag for the RE....
Read more >
Parse a string in exponential notation with only the AllowExponent ...
1. Parse a string in exponential notation with the AllowExponent and Number flags. 2. Parse a currency value with leading and trailing white...
Read more >
14.8 Command-Line Parsing - Brown CS
Command-line strings starting with - or + are parsed as flags, but arguments to flags are never parsed as flags, and integers and...
Read more >
How To Use the Flag Package in Go - DigitalOcean
The parsing phase is handled by the Parse() function. To illustrate, you'll create a program that defines a Boolean flag that changes the ......
Read more >
flag - Go Packages
TextVar defines a flag with a specified name, default value, and usage string. The argument p must be a pointer to a variable...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found