1
var minimist = require("minimist")

const a = minimist(`executable --param "a b"`.split(' '))
console.log(a)

https://runkit.com/embed/57837xcuv5v0

actual output:

Object {_: ["executable", "b\""], param: "\"a"}

expected output:

Object {_: ["executable"], param: "a b"}


I'm also seeing same result when using yargs and commander.

It's strange because jest is using yargs and jest accept the following command: jest -t "test name with spaces"

Stav Alfi
  • 13,139
  • 23
  • 99
  • 171

1 Answers1

1

Based on your example code, the problem is your preparation of the string array which has broken up the string-with-spaces before the parser gets to see it:

$ node -e 'console.log(`executable --param "a b"`.split(" "))'
[ 'executable', '--param', '"a', 'b"' ]

A simple fix when manually setting up the arguments is to construct the array of parameters yourself, instead of using a string and split, like:

$ node -e 'console.log(["executable", "--param", "a b"])'   
[ 'executable', '--param', 'a b' ]

or

const a = minimist(['executable', '--param', 'a b'])

If what you need to do is break up a single string into arguments like the shell does, that is not done by Commander, or minimist.

You could look at https://www.npmjs.com/package/shell-quote which has a parse command.


Yargs does have a mode where it does the splitting, if you pass in a single string rather than an array of strings.

const yargs = require('yargs/yargs');
const argv = yargs('executable --param "a b"').parse();
console.log(argv);
% node index.js 
{ _: [ 'executable' ], param: 'a b', '$0': 'index.js' }
shadowspawn
  • 3,039
  • 22
  • 26
  • what is the solution? – Stav Alfi Mar 13 '20 at 11:09
  • I will add some examples – shadowspawn Mar 13 '20 at 21:58
  • but idk what is the command before runtime. this answer doesnt help. sory – Stav Alfi Mar 13 '20 at 22:45
  • Where is this command string coming from? All of commander and yargs and minimist take an array of arguments, like from process.argv, after the shell has already processed the raw command line and split it into separate arguments. – shadowspawn Mar 14 '20 at 02:39
  • it comes from the argv. you all answer is based on the fact that i know the keys and values. it makes no sense that i will even need help.......... – Stav Alfi Mar 15 '20 at 07:49
  • When writing tests, you construct the argument array yourself and have to be careful about how you construct it. When you are passing process.argv, then splitting up the arguments is a different problem because what matters is how the shell does the splitting. I tried to explain what is wrong with your example but if that is not the issue, what are you actually doing and what behaviour are you actually seeing? – shadowspawn Mar 15 '20 at 10:10
  • think that argv=`executable --param "a b"`. see the actual/expected output – Stav Alfi Mar 15 '20 at 11:30
  • 1
    Added shell-quote – shadowspawn Mar 16 '20 at 09:42
  • thats far away from what was i looking for but it's the best there is. so thanks!! :) – Stav Alfi Mar 16 '20 at 13:03
  • @StavAlfi FYI Yargs can parse a single string, and I have added an example. – shadowspawn Mar 12 '23 at 05:49