Deno: Add -e cli flag

Created on 20 Oct 2018  路  10Comments  路  Source: denoland/deno

From Node.js docs, it may be done after #779, it helps with quick debugging such as inspecting the output of a one-liner.

Most helpful comment

Yea... we should probably add it. But considering we will probably add commands like "deno fmt" and "deno info" we might want to do "deno eval '[1,2].forEach(x => console.log(x))'" instead of trying to use -e and -p... We need to have a separate discussion regarding commands vs flags first.

All 10 comments

Yes - we should add this. We probably need to switch to using getopt for command-line parsing first.

I never felt the -p flag was so useful. It should just be default, or?

I believe -e/--eval is much simpler than -p/--print, plus we can already replicate -p's functionality by wrapping the code with a console.log inside the code for eval, having something to start with would be nice nevertheless.

If no other flag clash with -p, we can decide later whether we should implement it too or if -e already suffices.

Another option is to make -e print the result by default, instead of having two flags for the same thing, this would have parity with the eval() function, which does not only execute the code but also return it.

Just for a different perspective than only doing what Node does, here is how -e plus some complementary switches work in Perl:

-e evals the string like if it was a normal program (no implicit print or anything else)
-n runs the code* for all lines of standard input*
-p runs the code
for all lines of standard input** and prints the result
-l strips newlines from the input and adds newlines for print

*) the code is the value of -e if present or a normal program in a file otherwise (i.e. -p and other switches don't imply -e and in fact you can use -p or -n in a shebang lines of normal scripts which is super useful)

**) the input is actually stdin if no arguments are provided, or it opens file(s) if there are arguments (just like cat, sort, uniq and other Unix filters traditionally work)

In Perl the input and output is in the $_ variable (which is default for regex substitutions, print etc.), maybe in Deno some other way of providing the lines would make more sense - e.g. few years ago I created filt on npm to use Node for simple filters instead of sed or perl (but it's not nearly as useful) and it uses function return values instead of magic variables.

It would be really useful for certain use cases if we could use Deno to create simple Unix-style filters, this is currently the only thing that I still use Perl for on a daily basis.

And if you had to choose one, what would it be? Deno gets a budget of, say, 10 command line flags. Certainly -l and -n are out of the question. Reimplementing xargs is out of scope.

If I had to choose one, it would be -e with no implicit print.

This would be consistent with Node, Perl, Ruby, etc. Even awk doesn't print by default.

eval should have the same scope as the repl, so perhaps makes sense to implement -e after that?
(We may also won't to compile ts too.)

Plus it's easier to add console.log() when needed than to not print something that is printed by default.

Example:

$ node -e '[1,2].forEach(x => console.log(x))'
1
2

vs.

$ node -p '[1,2].forEach(x => console.log(x))'
1
2
undefined

Update:
I would need this if -e was like -p in Node:

$ node -p '[1,2].forEach(x => console.log(x))' | sed '$d'
1
2

@ry are you still interested in this implementing this regarding number of flags we already have?

Yea... we should probably add it. But considering we will probably add commands like "deno fmt" and "deno info" we might want to do "deno eval '[1,2].forEach(x => console.log(x))'" instead of trying to use -e and -p... We need to have a separate discussion regarding commands vs flags first.

Done in #2102

CC @ry

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sh7dm picture sh7dm  路  3Comments

davidbarratt picture davidbarratt  路  3Comments

kitsonk picture kitsonk  路  3Comments

JosephAkayesi picture JosephAkayesi  路  3Comments

zugende picture zugende  路  3Comments