Using the Command Shell

The ejs command is a command shell for the Ejscript language and can be used to interactively interpret commands typed at the console or to run script files.

Interactive Mode

When invoked with no files on the command line (and without --class or --method switches), ejs runs in interactive mode. In this mode, ejs reads input from the console and parses and executes scripts as they are typed to the console.

In interactive mode, ejs will read input until it has a complete block of code and then will execute the block. The last result value computed will be echoed to the console. For example:

ejs
ejs-0>  if (true) { 
ejs-2>     print("Hello World"); 
ejs-2> }
Hello World
ejs-0>

Note: the input prompt includes a level of nest indicator starting at zero and increasing upward as commands nest. This is a hint as to when you have closed sufficient braces for ejs to execute your command. As this is an internal compiler nest value, it will sometimes increment more than one for each brace.

Executing Script Files

If ejs is invoked with a file name on the command line, that file will be read and its statements executed. One file may be supplied and the arguments after it will be passed into the script as command line arguments. For example:

ejs myscript.es arg1 arg2 arg3

If myscript.es contained the following script:

print(App.args)

the following would be output:

myscript.es,arg1,arg2,arg3

The script file may be either a script file or an Ejscript module with a ".mod" extension that has been previously compiled with the ejsc command.

If you require more script files to be parsed, use the --files "files ..." switch to supply extra scripts. These will be parsed in order, but after the primary script on the command line. For example:

ejs --files "extraScript1.es extraScript2.es" myscript.es arg1 arg2 arg3

In most cases, this is not necessary as you can use the "require" directive in your program cause a module to be loaded as required.

Run a Designated Method

The ejs command can also invoke a specific static method on startup. If either the --class or --method switches are supplied, then ejs will run the requested static method after the input file script statements have been executed. If the --method switch is omitted, then the "main" method name is used. If the --class switch is omitted, the first class with a static main method is used. For example:

ejs --class MyClass --method "main" myscript.es

This command will load the myscript.es file, run the script and then invoke the static main method in the MyClass class.

Command Options

The ejs command may be invoked with the following command options:

ejs [--class className] [--cmd "literal script"] [--debug] 
    [--debugger] [--files "files..."] [--log logSpec] 
    [--method methodName] [--nodebug] [--optimize level] 
    [--require "modules"] [--searchPath ejsPath] [--standard]
    [--stats] [--strict] [--verbose] [--version] [--warn level] 
    file [arguments ...]
The ejs command will parse and execute the given file with the supplied arguments passed into the App.args property.
Switch Description
--class className Use the given className when searching for the startup method specified via --method or "main" by default.
--cmd "literal script" Run the given script text instead of reading a script file.
--debug Include symbolic debug information in the byte code.
--debugger Run in debug mode with symbolic stack backtraces in exceptions.
--files "files..." Specifies an extra set of files to be compiled. The ejs command normally takes just one script file on the command line. The --files switch allows extra script to be compiled after the primary script, but before the scripts are run.
--log logName[:logLevel] Specify a file to write trace and error messages. The log level specifies the desired verbosity of output. Level 0 is the least verbose and level 9 is the most. Level 2 will include trace of each source statement and assembler instruction executed (provided --nodebug is not specified).
---method methodName Set the startup method name. Defaults to "main" if a --className is specified and --method is not.
--nodebug Run the script without debug information. This will result in exceptions not having symbolic stack backtraces.
--optimize level Set the code optimization level. Level values must be between 0 (least) and 9 (most). Default is 9.
--require modules ... Pre-load the required modules before running the script.
--searchPath ejsPath

Override the module search path. The module search path is a set of directories that the ejs command will use when locating and loading Ejscript modules. Given a module named "a.b.c" in a script, ejs will use the following search strategy to locate the module:

1. Search for a module file named "a.b.c.mod"

2. Search for a module file named "a/b/c.mod"

3. Search for a module file named "a.b.c.mod" in EJSPATH

4. Search for a module file named c.mod in EJSPATH

The search path is initially specified via the environment variable EJSPATH and may be overridden via the --searchPath ejsPath switch. EJSPATH and the ejsPath command line value are similar to the system PATH formats. On windows, path segments are separated by ";" and on Linux, Unix, FreeBSD and MAC, the path segments are separated by ":" delimiters.

--standard Run scripts in standard mode. Ejscript supports two parsing modes: strict and standard. Standard mode does not require variables be declared and typed before use.
--stats Print various statistics on exit.
--strict Run scripts in standard mode. Ejscript supports two parsing modes: strict and standard. Strict mode requires that all variables be declared and typed.
--warn level Set the compiler warning verbosity level. Level values must be between 0 (least verbose) and 9 (most). Default is 0.
--version Print the ejs command version and exit.

© Embedthis Software, 2003-2015. All rights reserved.