diff options
Diffstat (limited to 'doc/docs/doc/getting-started/usage.md')
| -rw-r--r-- | doc/docs/doc/getting-started/usage.md | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/doc/docs/doc/getting-started/usage.md b/doc/docs/doc/getting-started/usage.md new file mode 100644 index 0000000..45161c6 --- /dev/null +++ b/doc/docs/doc/getting-started/usage.md | |||
| @@ -0,0 +1,111 @@ | |||
| 1 | # Usage | ||
| 2 | |||
| 3 | ## Lua Module | ||
| 4 | |||
| 5 | Use YueScript module in Lua: | ||
| 6 | |||
| 7 | * **Case 1** | ||
| 8 | |||
| 9 | Require "your_yuescript_entry.yue" in Lua. | ||
| 10 | ```Lua | ||
| 11 | require("yue")("your_yuescript_entry") | ||
| 12 | ``` | ||
| 13 | And this code still works when you compile "your_yuescript_entry.yue" to "your_yuescript_entry.lua" in the same path. In the rest YueScript files just use the normal **require** or **import**. The code line numbers in error messages will also be handled correctly. | ||
| 14 | |||
| 15 | * **Case 2** | ||
| 16 | |||
| 17 | Require YueScript module and rewite message by hand. | ||
| 18 | |||
| 19 | ```lua | ||
| 20 | local yue = require("yue") | ||
| 21 | yue.insert_loader() | ||
| 22 | local success, result = xpcall(function() | ||
| 23 | return require("yuescript_module_name") | ||
| 24 | end, function(err) | ||
| 25 | return yue.traceback(err) | ||
| 26 | end) | ||
| 27 | ``` | ||
| 28 | |||
| 29 | * **Case 3** | ||
| 30 | |||
| 31 | Use the YueScript compiler function in Lua. | ||
| 32 | |||
| 33 | ```lua | ||
| 34 | local yue = require("yue") | ||
| 35 | local codes, err, globals = yue.to_lua([[ | ||
| 36 | f = -> | ||
| 37 | print "hello world" | ||
| 38 | f! | ||
| 39 | ]],{ | ||
| 40 | implicit_return_root = true, | ||
| 41 | reserve_line_number = true, | ||
| 42 | lint_global = true, | ||
| 43 | space_over_tab = false, | ||
| 44 | options = { | ||
| 45 | target = "5.4", | ||
| 46 | path = "/script" | ||
| 47 | } | ||
| 48 | }) | ||
| 49 | ``` | ||
| 50 | |||
| 51 | ## YueScript Tool | ||
| 52 | |||
| 53 | Use YueScript tool with: | ||
| 54 | |||
| 55 | ```shell | ||
| 56 | > yue -h | ||
| 57 | Usage: yue | ||
| 58 | [options] [<file/directory>] ... | ||
| 59 | yue -e <code_or_file> [args...] | ||
| 60 | yue -w [<directory>] [options] | ||
| 61 | yue - | ||
| 62 | |||
| 63 | Notes: | ||
| 64 | - '-' / '--' must be the first and only argument. | ||
| 65 | - '-o/--output' can not be used with multiple input files. | ||
| 66 | - '-w/--watch' can not be used with file input (directory only). | ||
| 67 | - with '-e/--execute', remaining tokens are treated as script args. | ||
| 68 | |||
| 69 | Options: | ||
| 70 | -h, --help Show this help message and exit. | ||
| 71 | -e <str>, --execute <str> Execute a file or raw codes | ||
| 72 | -m, --minify Generate minified codes | ||
| 73 | -r, --rewrite Rewrite output to match original line numbers | ||
| 74 | -t <output_to>, --output-to <output_to> | ||
| 75 | Specify where to place compiled files | ||
| 76 | -o <file>, --output <file> Write output to file | ||
| 77 | -p, --print Write output to standard out | ||
| 78 | -b, --benchmark Dump compile time (doesn't write output) | ||
| 79 | -g, --globals Dump global variables used in NAME LINE COLUMN | ||
| 80 | -s, --spaces Use spaces in generated codes instead of tabs | ||
| 81 | -l, --line-numbers Write line numbers from source codes | ||
| 82 | -j, --no-implicit-return Disable implicit return at end of file | ||
| 83 | -c, --reserve-comments Reserve comments before statement from source codes | ||
| 84 | -w [<dir>], --watch [<dir>] | ||
| 85 | Watch changes and compile every file under directory | ||
| 86 | -v, --version Print version | ||
| 87 | - Read from standard in, print to standard out | ||
| 88 | (Must be first and only argument) | ||
| 89 | -- Same as '-' (kept for backward compatibility) | ||
| 90 | |||
| 91 | --target <version> Specify the Lua version that codes will be generated to | ||
| 92 | (version can only be 5.1 to 5.5) | ||
| 93 | --path <path_str> Append an extra Lua search path string to package.path | ||
| 94 | --<key>=<value> Pass compiler option in key=value form (existing behavior) | ||
| 95 | |||
| 96 | Execute without options to enter REPL, type symbol '$' | ||
| 97 | in a single line to start/stop multi-line mode | ||
| 98 | ``` | ||
| 99 | Use cases: | ||
| 100 | |||
| 101 | Recursively compile every YueScript file with extension **.yue** under current path: **yue .** | ||
| 102 | |||
| 103 | Compile and save results to a target path: **yue -t /target/path/ .** | ||
| 104 | |||
| 105 | Compile and reserve debug info: **yue -l .** | ||
| 106 | |||
| 107 | Compile and generate minified codes: **yue -m .** | ||
| 108 | |||
| 109 | Execute raw codes: **yue -e 'print 123'** | ||
| 110 | |||
| 111 | Execute a YueScript file: **yue -e main.yue** | ||
