1

I want to get user command exactly the way they typed. The only allowed difference is ' can come in place of " and vice versa.

For example, if the user typed.

node test.js git commit -m "first message"

I want to log either of following on console.

You typed: git commit -m 'first message' //line A
You typed: git commit -m "first message" //line B

But there is not acceptable:

You typed: "git" "commit" "-m" "first message" //line C
You typed: 'git' 'commit' '-m' 'first message' // line D

As you can see above, quotes can be in different than the user provided (' can replace " and vice versa like in line B) but they can't be misplaced (like in line C and D). Hope this is clear.

Edit: Edited the whole question to avoid confusion.

asdasd
  • 6,050
  • 3
  • 14
  • 32
  • 2
    The quotes are interpreted by the shell prior to being handed to node, so without explicit escaping and quoting the entire command (e.g. `"git commit -m \"first message\""`) you might not be able to do what you want. – Joe Jan 27 '19 at 17:43
  • Joe...I made an edit. Can you pls tell me if that is possible? – asdasd Jan 27 '19 at 17:52
  • the edit is not clear, please specify exactly what you want not the unacceptable results – Jay Jan 27 '19 at 17:58
  • I edited the question. Hope this is clear. – asdasd Jan 27 '19 at 18:09

1 Answers1

0

Your question is not very clear but assuming that you just want to log the arguments you are receiving you can do it like this:

let args = [];
// Get program arguments
process.argv.forEach((val, index) => {
if (index > 1)
    args.push(val);
});
let command = args.join(" ");
console.log("You typed: ", command);

But note that you will lose the quotes when printing the output, as said by @Joe inthe comments, the shell scapes them before passing them to node

Francisco Rangel
  • 129
  • 1
  • 11