I want to get the array of arguments so I can use it with optparse-js library so If I have something like
-f foo -b -a -z baz bar
I want array like this
["-f", "foo", "-b", "-a", "-z", "baz", "bar"]
it should work with strings that have escape quote inside and long GNU options. So far I have regex that match the string
/("(?:\\"|[^"])*"|'(?:\\'|[^'])*')/g
it match strings like "das" or "asd\"asd" or 'asd' or 'sad\'asd'
Can I use regex for this or do I need a parser (like using PEG) it would be nice if it match regex to so I can do
-p "hello b\"ar baz" -f /^ [^ ]+ $/
UPDATE: with help from @Damask I've created this regex:
/('(\\'|[^'])*'|"(\\"|[^"])*"|\/(\\\/|[^\/])*\/|(\\ |[^ ])+|[\w-]+)/g
it work for strings like this:
echo -p "hello b\"ar baz" -f /^ [^ ]+ $/
it return
['echo', '-p', '"hello b\"ar baz"', '-f', '/^ [^ ]+ $/']
but if fail on strings like this:
echo "©\\\\" abc "baz"
it match command and two arguments instead of 3 arguments demo
if argument don't have spaces like "foo"baz it should be one item in array, quotes need to be included but I will remove not escaped ones from string (like in bash when you execute echo "foo"bar echo will get one foobar argument).
-p "hello b\"ar baz" -f /^ [^ ]+ $/