tolmasky / language

A fast PEG parser written in JavaScript with first class errors
languagejs.com
MIT License
411 stars 48 forks source link

Improvements to CLI to add support for STDIN/OUT #2

Closed stevegeek closed 13 years ago

stevegeek commented 13 years ago

Makes read/writes async and also adds some error checking.

Now for CLI if: '-i' is specified without a parameter it defaults to stdin, if it is omitted no input is assumed. '-o' is specified without parameter or omitted then defaults to stdout '-g' is specified without a parameter or omitted defaults to stdin, (and if both input and grammar are from stdin then they are accepted in sequence, first grammar is input then 'input' )

I have tested it generally but this should be further tested by someone who can def verify output is correct.

Feedback/more testing/changes/hate/love welcome

tolmasky commented 13 years ago

So basically the idea was the following:

  1. If no -g or -o, then take the grammar from stdin and print the compiled grammar to stdout (something like gammar_generator | cli.js > result)
  2. If -g but no -o, take grammar from -g and print to stdout ( something like cli.js -g grammar.language > result)
  3. If -g and -i === -, take grammar from -g and use stdin for input to be parsed (something like cat objective_j_file | cli.js -g objj_gammar.language > result)

Does this do that? If so, I will take it in.

tolmasky commented 13 years ago

Also (just looking at your second commit). The idea is that grammar and input can't BOTH be stdin

tolmasky commented 13 years ago

Oops, didn't mean to close this

stevegeek commented 13 years ago

1) yes 2) yes 3) no , will update it later

stevegeek commented 13 years ago

Oh btw, '-i -' is not parsed by nomnom if im not mistaken, it assumes the extra '-' on the command line is the start of a new command i think, so in this case the value of the input option is as below: cmd -i - -t then input === true cmd -t -i - then input === true cmd -i -t then input === true cmd -i file then input === 'file' cmd -t -i then input === '' <- the odd one out

Will think about it more later when I have time

stevegeek commented 13 years ago

Ok now the examples in 1) 2) and 3) should all work as expected

eg echo 'start="a"' | node lib/language/cli.js -t -g languages/JavaScript.language -o out.txt -> out to file, input from stdin node lib/language/cli.js -t -g languages/JavaScript.language -> out to stdout echo 'start="a"' | node lib/language/cli.js -t -> grammar from stdin , out to stdout echo 'start="a"' | node lib/language/cli.js -t -g languages/JavaScript.language -i -> input from stdin

stevegeek commented 13 years ago

Ah one sec, seen something I think

stevegeek commented 13 years ago

Ok think that works, sorry about all the commits!

stevegeek commented 13 years ago

@tolmasky have you had a chance to look further at this? I think it fulfills the requirement. I have been using it for a few days and seems fine

tolmasky commented 13 years ago

Yeah sorry I haven't gotten to this yet, just been super busy. I will try to get it merged in tonight.

stevegeek commented 13 years ago

No probs, no rush

tolmasky commented 13 years ago

I really dislike asynchronous IO for command line apps.

stevegeek commented 13 years ago

Ah ok, sorry about that

tolmasky commented 13 years ago

nothing to apologize for, your thing works exactly as described.