You are not logged in.

#1 2013-01-12 01:30:55

From: Visalia, California
Registered: 2012-10-10
Posts: 219

[solved] cli interpreter jquery/regex/php

Hi I'm looking for a way to parse out a string into a first word and switches exactly the way bash handles.  For jquery/php
Like this.

command -a -b -c leftovertext | command -abc leftovertext

I've looked at a javascript parser (optparse), but it's pretty outdated and I'm having trouble using a regular expression with match and each in jquery;
Or if I should try doing this in php.
Any comments welcome.


I decided to match every non white space character with the regular expression


which gives me an array.

Then define flags and arguments with

var command = "this -h command unto";
var cmd = command.match(/(\S+)/g);
var flags = cmd[1].substring(1) ? cmd[1].substring(1) : 'h';
var argue = cmd[2] ? cmd[2] : false;
var descr = cmd[2] ? cmd[2] : false;

'h' for help.
The description/descriptor 'unto' can be parsed similarly or if the remaining string is needed ('mail -s user this is my message') can be matched from the argument(user) as the description/descriptor.
Only working with one flag switch but I'm happy with it.
Still to do the pipe.

Last edited by ackt1c (2013-01-15 23:48:22)


Board footer

Powered by FluxBB