#Parse Raw Command Line Terminal Input

15 messages · Page 1 of 1 (latest)

cloud cosmos
#

The input string represents the literal command entered in the terminal, like some-command --flag --arg1=0 --arg2 'TEXT 1' --arg3="TEXT WITH SPACE".

Is there a tool that can split this to an array of strings (like process.argv)?

Then the result can be passed to a command line parser package (like commander.js) and some logic will be implemented there.

fiery yacht
#

doesn't process.argv already return that array of strings?

#

should return ["some-command", "--flag", "--arg1=0", "--arg2", "TEXT 1", "--arg3=\"TEXT WITH SPACE\""]

cloud cosmos
#

I'm not running any programs 🙂 I'm using this to automate some stuff. For example this can be a text coming from a Telegram Bot. So, you could say I am using the shell-style as a DSL to represent the (variant/programmable) logic of a program.

fiery yacht
#

oh, so you want to split a string the way the shell would

cloud cosmos
#

Exactly! And taking care of string args is tricky business.

fiery yacht
#

just look for "parse" and "args" I guess

#

I'ved used yargs in the past to make CLIs and it was working pretty well, so the parser the made should work just well (yargs-parser)

cloud cosmos
#

I've looked into some of them. So far in all cases the parse(...) method expects a string array (the one that the shell has already split into strings)

#

(will continue to look into the ones that I have not seen so far)

fiery yacht
#

right, didn't think about that

#

have a look at yargs-parser

#

it should be supported