1 |
|
2 | #include "cpp/embedded_file.h"
|
3 |
|
4 | namespace embedded_file {
|
5 | GLOBAL_STR(gStr0, R"zZXx(
|
6 | Errors
|
7 | <a class="group-link" href="chap-errors.html">errors</a>
|
8 |
|
9 |
|
10 | [UTF8] err-utf8-encode err-utf8-decode
|
11 | [J8 String] err-j8-str-encode err-j8-str-decode
|
12 | [J8 Lines] err-j8-lines-encode err-j8-lines-decode
|
13 | [JSON] err-json-encode err-json-decode
|
14 | [JSON8] err-json8-encode err-json8-decode
|
15 | )zZXx");
|
16 |
|
17 | GLOBAL_STR(gStr1, R"zZXx(
|
18 |
|
19 | Front End <a class="group-link" href="chap-front-end.html">front-end</a>
|
20 |
|
21 |
|
22 | [Lexing] ascii-whitespace [ \t\r\n]
|
23 | ascii-control-chars
|
24 | )zZXx");
|
25 |
|
26 | GLOBAL_STR(gStr2, R"zZXx(
|
27 | J8 Notation
|
28 | <a class="group-link" href="chap-j8.html">j8</a>
|
29 |
|
30 |
|
31 | [J8 Strings] json-string "hi"
|
32 | json-escape \" \\ \u1234
|
33 | surrogate-pair \ud83e\udd26
|
34 | j8-escape \' \u{1f926} \yff
|
35 | u-prefix u'hi'
|
36 | b-prefix b'hi'
|
37 | no-prefix 'hi'
|
38 | [J8 Lines] unquoted-line
|
39 | [JSON8] json8-num json8-str
|
40 | X json8-list X json8-dict
|
41 | json8-comment
|
42 | [TSV8] column-attrs column-types
|
43 | )zZXx");
|
44 |
|
45 | GLOBAL_STR(gStr3, R"zZXx(Usage: help TOPIC?
|
46 |
|
47 | Examples:
|
48 |
|
49 | help # this help
|
50 | help echo # help on the 'echo' builtin
|
51 | help command-sub # help on command sub $(date)
|
52 |
|
53 | help oils-usage # identical to oils-for-unix --help
|
54 | help osh-usage # osh --help
|
55 | help ysh-usage # ysh --help
|
56 | )zZXx");
|
57 |
|
58 | GLOBAL_STR(gStr4, R"zZXx(bin/oils-for-unix is an executable that contains OSH, YSH, and more.
|
59 |
|
60 | Usage: oils-for-unix MAIN_NAME ARG*
|
61 | MAIN_NAME ARG*
|
62 |
|
63 | It behaves like busybox. The command name can be passed as the first argument:
|
64 |
|
65 | oils-for-unix ysh -c 'echo hi'
|
66 |
|
67 | More commonly, it's invoked through a symlink like 'ysh', which causes it to
|
68 | behave like that command:
|
69 |
|
70 | ysh -c 'echo hi'
|
71 | )zZXx");
|
72 |
|
73 | GLOBAL_STR(gStr5, R"zZXx(
|
74 | Builtin Commands <a class="group-link" href="chap-builtin-cmd.html">builtin-cmd</a>
|
75 |
|
76 |
|
77 | [I/O] read echo printf
|
78 | readarray mapfile
|
79 | [Run Code] source . eval trap
|
80 | [Set Options] set shopt
|
81 | [Working Dir] cd pwd pushd popd dirs
|
82 | [Completion] complete compgen compopt compadjust compexport
|
83 | [Shell Process] exec X logout
|
84 | umask ulimit times
|
85 | [Child Process] jobs wait
|
86 | fg X bg X kill X disown
|
87 | [External] test [ getopts
|
88 | [Introspection] help hash cmd/type X caller
|
89 | [Word Lookup] command builtin
|
90 | [Interactive] alias unalias history X fc X bind
|
91 | X [Unsupported] enable
|
92 | )zZXx");
|
93 |
|
94 | GLOBAL_STR(gStr6, R"zZXx(The reference is divided in to "chapters", each of which has its own table of
|
95 | contents. Type:
|
96 |
|
97 | help osh-$CHAPTER
|
98 |
|
99 | Where $CHAPTER is one of:
|
100 |
|
101 | front-end
|
102 | command-lang
|
103 | osh-assign
|
104 | word-lang
|
105 | mini-lang
|
106 | builtin-cmd
|
107 | option
|
108 | special-var
|
109 | plugin
|
110 |
|
111 | Example:
|
112 |
|
113 | help osh-word-lang
|
114 | )zZXx");
|
115 |
|
116 | GLOBAL_STR(gStr7, R"zZXx(
|
117 | Command Language <a class="group-link" href="chap-cmd-lang.html">cmd-lang</a>
|
118 |
|
119 |
|
120 | [Commands] simple-command semicolon ;
|
121 | [Conditional] case if dbracket [[
|
122 | true false colon :
|
123 | bang ! and && or ||
|
124 | [Iteration] while until for for-expr-sh ((
|
125 | [Control Flow] break continue return exit
|
126 | [Grouping] sh-func sh-block { subshell (
|
127 | [Concurrency] pipe | X pipe-amp |& ampersand &
|
128 | [Redirects] redir-file > >> >| < <> not impl: &>
|
129 | redir-desc >& <&
|
130 | here-doc << <<- <<<
|
131 | [Other Command] dparen (( time X coproc X select
|
132 | )zZXx");
|
133 |
|
134 | GLOBAL_STR(gStr8, R"zZXx(
|
135 | Front End <a class="group-link" href="chap-front-end.html">front-end</a>
|
136 |
|
137 |
|
138 | [Usage] oils-usage osh-usage config
|
139 | startup line-editing exit-codes
|
140 | [Lexing] comment # line-continuation \ ascii-whitespace [ \t\r\n]
|
141 | )zZXx");
|
142 |
|
143 | GLOBAL_STR(gStr9, R"zZXx(
|
144 | Other Mini Languages <a class="group-link" href="chap-mini-lang.html">mini-lang</a>
|
145 |
|
146 |
|
147 | [Arithmetic] arith-context Where legacy arithmetic is allowed
|
148 | sh-numbers 0xFF 0755 etc.
|
149 | sh-arith 1 + 2*3 a *= 2
|
150 | sh-logical !a && b
|
151 | sh-bitwise ~a ^ b
|
152 | [Boolean] bool-expr [[ ! $x && $y || $z ]]
|
153 | test ! $x -a $y -o $z
|
154 | bool-infix $a -nt $b $x == $y
|
155 | bool-path -d /etc
|
156 | bool-str -n foo -z ''
|
157 | bool-other -o errexit -v name[index]
|
158 | [Patterns] glob-pat *.py
|
159 | extglob ,(*.py|*.sh)
|
160 | regex [[ foo =~ [a-z]+ ]]
|
161 | [Other Sublang] braces {alice,bob}@example.com
|
162 | histsub !$ !! !n
|
163 | char-escapes \t \c \x00 \u03bc
|
164 | )zZXx");
|
165 |
|
166 | GLOBAL_STR(gStr10, R"zZXx(
|
167 | Global Shell Options <a class="group-link" href="chap-option.html">option</a>
|
168 |
|
169 |
|
170 | [Errors] nounset -u errexit -e inherit_errexit pipefail
|
171 | [Globbing] noglob -f nullglob failglob X dotglob
|
172 | dashglob (true)
|
173 | [Debugging] xtrace X verbose X extdebug
|
174 | [Interactive] emacs vi
|
175 | [Other POSIX] X noclobber
|
176 | [Compat] eval_unsafe_arith ignore_flags_not_impl
|
177 | )zZXx");
|
178 |
|
179 | GLOBAL_STR(gStr11, R"zZXx(
|
180 | Assignments and Expressions <a class="group-link" href="chap-osh-assign.html">osh-assign</a>
|
181 |
|
182 |
|
183 | [Literals] sh-array array=(a b c) array[1]=B "${a[@]}"
|
184 | sh-assoc assoc=(['a']=1 ['b']=2) assoc['x']=b
|
185 | [Operators] sh-assign str='xyz'
|
186 | sh-append str+='abc'
|
187 | [Builtins] local readonly export unset shift
|
188 | declare typeset X let
|
189 | )zZXx");
|
190 |
|
191 | GLOBAL_STR(gStr12, R"zZXx(
|
192 | Plugins and Hooks <a class="group-link" href="chap-plugin.html">plugin</a>
|
193 |
|
194 |
|
195 | [Signals] SIGTERM SIGINT SIGQUIT
|
196 | SIGTTIN SIGTTOU SIGWINCH
|
197 | [Traps] DEBUG ERR EXIT X RETURN
|
198 | [Words] PS1 X PS2 X PS3 PS4
|
199 | [Completion] complete
|
200 | [Other Plugin] PROMPT_COMMAND X command_not_found
|
201 | )zZXx");
|
202 |
|
203 | GLOBAL_STR(gStr13, R"zZXx(
|
204 | Special Variables <a class="group-link" href="chap-special-var.html">special-var</a>
|
205 |
|
206 |
|
207 | [POSIX Special] $@ $* $# $? $- $$ $! $0 $9
|
208 | [Shell Vars] IFS X LANG X GLOBIGNORE
|
209 | [Shell Options] SHELLOPTS X BASHOPTS
|
210 | [Other Env] HOME PATH
|
211 | [Other Special] BASH_REMATCH @PIPESTATUS
|
212 | [Platform] HOSTNAME OSTYPE
|
213 | [Call Stack] @BASH_SOURCE @FUNCNAME @BASH_LINENO
|
214 | X @BASH_ARGV X @BASH_ARGC
|
215 | [Tracing] LINENO
|
216 | [Process State] UID EUID PPID X BASHPID
|
217 | X [Process Stack] BASH_SUBSHELL SHLVL
|
218 | X [Shell State] BASH_CMDS @DIRSTACK
|
219 | [Completion] @COMP_WORDS COMP_CWORD COMP_LINE COMP_POINT
|
220 | COMP_WORDBREAKS @COMPREPLY X COMP_KEY
|
221 | X COMP_TYPE COMP_ARGV
|
222 | [History] HISTFILE
|
223 | [cd] PWD OLDPWD X CDPATH
|
224 | [getopts] OPTIND OPTARG X OPTERR
|
225 | [read] REPLY
|
226 | [Functions] X RANDOM SECONDS
|
227 | )zZXx");
|
228 |
|
229 | GLOBAL_STR(gStr14, R"zZXx(
|
230 | Standard Library <a class="group-link" href="chap-stdlib.html">stdlib</a>
|
231 |
|
232 |
|
233 | [two] log die
|
234 | [byo-server-lib] byo-maybe-run byo-must-run
|
235 | )zZXx");
|
236 |
|
237 | GLOBAL_STR(gStr15, R"zZXx(
|
238 | OSH Types <a class="group-link" href="chap-type-method.html">type-method</a>
|
239 |
|
240 |
|
241 | [OSH] BashArray BashAssoc
|
242 | )zZXx");
|
243 |
|
244 | GLOBAL_STR(gStr16, R"zZXx(bin/osh is compatible with POSIX shell, bash, and other shells.
|
245 |
|
246 | Usage: osh FLAG* SCRIPT ARG*
|
247 | osh FLAG* -c COMMAND ARG*
|
248 | osh FLAG*
|
249 |
|
250 | The command line accepted by `bin/osh` is compatible with /bin/sh and bash.
|
251 |
|
252 | osh -c 'echo hi'
|
253 | osh myscript.sh
|
254 | echo 'echo hi' | osh
|
255 |
|
256 | It also has a few enhancements:
|
257 |
|
258 | osh -n -c 'hello' # pretty-print the AST
|
259 | osh --ast-format text -n -c 'hello' # print it full
|
260 |
|
261 | osh accepts POSIX sh flags, with these additions:
|
262 |
|
263 | -n parse the program but don't execute it. Print the AST.
|
264 | --ast-format what format the AST should be in
|
265 | )zZXx");
|
266 |
|
267 | GLOBAL_STR(gStr17, R"zZXx(
|
268 | Word Language <a class="group-link" href="chap-word-lang.html">word-lang</a>
|
269 |
|
270 |
|
271 | [Quotes] osh-string 'abc' $'line\n' "$var"
|
272 | [Substitutions] command-sub $(command) `command`
|
273 | var-sub ${var} $0 $9
|
274 | arith-sub $((1 + 2))
|
275 | tilde-sub ~/src
|
276 | proc-sub diff <(sort L.txt) <(sort R.txt)
|
277 | [Var Ops] op-test ${x:-default}
|
278 | op-strip ${x%%suffix} etc.
|
279 | op-replace ${x//y/z}
|
280 | op-index ${a[i+1}
|
281 | op-slice ${a[@]:0:1}
|
282 | op-format ${x@P}
|
283 | )zZXx");
|
284 |
|
285 | GLOBAL_STR(gStr18, R"zZXx(
|
286 | Builtin Commands <a class="group-link" href="chap-builtin-cmd.html">builtin-cmd</a>
|
287 |
|
288 |
|
289 | [Memory] cmd/append Add elements to end of array
|
290 | pp asdl cell X gc-stats line proc
|
291 | [Handle Errors] error error 'failed' (status=2)
|
292 | try Run with errexit, set _error
|
293 | failed Test if _error.code !== 0
|
294 | boolstatus Enforce 0 or 1 exit status
|
295 | [Shell State] ysh-cd ysh-shopt compatible, and takes a block
|
296 | shvar Temporary modify global settings
|
297 | ctx Share and update a temporary "context"
|
298 | push-registers Save registers like $?, PIPESTATUS
|
299 | [Modules] runproc Run a proc; use as main entry point
|
300 | module guard against duplicate 'source'
|
301 | is-main false when sourcing a file
|
302 | use change first word lookup
|
303 | [I/O] ysh-read flags --all, -0
|
304 | ysh-echo no -e -n with simple_echo
|
305 | write Like echo, with --, --sep, --end
|
306 | fork forkwait Replace & and (), and takes a block
|
307 | fopen Open multiple streams, takes a block
|
308 | X dbg Only thing that can be used in funcs
|
309 | [Hay Config] hay haynode For DSLs and config files
|
310 | [Completion] compadjust compexport
|
311 | [Data Formats] json read write
|
312 | json8 read write
|
313 | X [Testing] assert takes an expression
|
314 | )zZXx");
|
315 |
|
316 | GLOBAL_STR(gStr19, R"zZXx(
|
317 | Builtin Functions <a class="group-link" href="chap-builtin-func.html">builtin-func</a>
|
318 |
|
319 |
|
320 | [Values] len() func/type() X repeat()
|
321 | [Conversions] bool() int() float() str() list() dict()
|
322 | X runes() X encodeRunes()
|
323 | X bytes() X encodeBytes()
|
324 | [Str] X strcmp() X split() shSplit()
|
325 | [List] join() any() all()
|
326 | [Collections] X copy() X deepCopy()
|
327 | [Word] glob() maybe()
|
328 | [Math] abs() max() min() X round() sum()
|
329 | [Serialize] toJson() fromJson()
|
330 | toJson8() fromJson8()
|
331 | X [J8 Decode] J8.Bool() J8.Int() ...
|
332 | [Pattern] _group() _start() _end()
|
333 | [Introspection] shvarGet() getVar() evalExpr()
|
334 | [Hay Config] parseHay() evalHay()
|
335 | X [Hashing] sha1dc() sha256()
|
336 | )zZXx");
|
337 |
|
338 | GLOBAL_STR(gStr20, R"zZXx(The reference is divided in to "chapters", each of which has its own table of
|
339 | contents. Type:
|
340 |
|
341 | help ysh-$CHAPTER
|
342 |
|
343 | Where $CHAPTER is one of:
|
344 |
|
345 | front-end
|
346 | command-lang
|
347 | expr-lang
|
348 | word-lang
|
349 | builtin-cmd
|
350 | option
|
351 | special-var
|
352 | type-method
|
353 | builtin-func
|
354 |
|
355 | Example:
|
356 |
|
357 | help ysh-expr-lang
|
358 | )zZXx");
|
359 |
|
360 | GLOBAL_STR(gStr21, R"zZXx(
|
361 | Command Language <a class="group-link" href="chap-cmd-lang.html">cmd-lang</a>
|
362 |
|
363 |
|
364 | [YSH Simple] typed-arg json write (x)
|
365 | lazy-expr-arg assert [42 === x]
|
366 | block-arg cd /tmp { echo $PWD }; cd /tmp (; ; blockexpr)
|
367 | [YSH Cond] ysh-case case (x) { *.py { echo 'python' } }
|
368 | ysh-if if (x > 0) { echo }
|
369 | [YSH Iter] ysh-while while (x > 0) { echo }
|
370 | ysh-for for i, item in (mylist) { echo }
|
371 | )zZXx");
|
372 |
|
373 | GLOBAL_STR(gStr22, R"zZXx(
|
374 | Expression Language and Assignments <a class="group-link" href="chap-expr-lang.html">expr-lang</a>
|
375 |
|
376 |
|
377 | [Assignment] assign =
|
378 | aug-assign += -= *= /= **= //= %=
|
379 | &= |= ^= <<= >>=
|
380 | [Literals] atom-literal true false null
|
381 | int-literal 42 65_536 0xFF 0o755 0b10
|
382 | float-lit 3.14 1.5e-10
|
383 | X num-suffix 42 K Ki M Mi G Gi T Ti / ms us
|
384 | ysh-string "x is $x" $"x is $x" r'[a-z]\n'
|
385 | u'line\n' b'byte \yff'
|
386 | triple-quoted """ $""" r''' u''' b'''
|
387 | str-template ^"$a and $b" for Str::replace()
|
388 | list-literal ['one', 'two', 3] :| unquoted words |
|
389 | dict-literal {name: 'bob'} {a, b}
|
390 | range 1 .. n+1
|
391 | block-expr ^(echo $PWD)
|
392 | expr-literal ^[1 + 2*3]
|
393 | X expr-sub $[myobj]
|
394 | X expr-splice @[myobj]
|
395 | [Operators] op-precedence Like Python
|
396 | concat s1 ++ s2, L1 ++ L2
|
397 | ysh-equals === !== ~== is, is not
|
398 | ysh-in in, not in
|
399 | ysh-compare < <= > >= (numbers only)
|
400 | ysh-logical not and or
|
401 | ysh-arith + - * / // % **
|
402 | ysh-bitwise ~ & | ^ << >>
|
403 | ysh-ternary '+' if x >= 0 else '-'
|
404 | ysh-index s[0] mylist[3] mydict['key']
|
405 | ysh-attr mydict.key
|
406 | ysh-slice a[1:-1] s[1:-1]
|
407 | func-call f(x, y; ...named)
|
408 | thin-arrow mylist->pop()
|
409 | fat-arrow mystr => startsWith('prefix')
|
410 | match-ops ~ !~ ~~ !~~
|
411 | [Eggex] re-literal / d+ ; re-flags ; ERE /
|
412 | re-primitive %zero 'sq'
|
413 | class-literal [c a-z 'abc' @str_var \\ \xFF \u0100]
|
414 | named-class dot digit space word d s w
|
415 | re-repeat d? d* d+ d{3} d{2,4}
|
416 | re-compound seq1 seq2 alt1|alt2 (expr1 expr2)
|
417 | re-capture <capture d+ as name: int>
|
418 | re-splice Subpattern @subpattern
|
419 | re-flags reg_icase reg_newline
|
420 | X re-multiline ///
|
421 | )zZXx");
|
422 |
|
423 | GLOBAL_STR(gStr23, R"zZXx(
|
424 | Front End <a class="group-link" href="chap-front-end.html">front-end</a>
|
425 |
|
426 |
|
427 | [Usage] oils-usage ysh-usage
|
428 | [Lexing] ascii-whitespace [ \t\r\n]
|
429 | doc-comment ### multiline-command ...
|
430 | [Tools] cat-em
|
431 | )zZXx");
|
432 |
|
433 | GLOBAL_STR(gStr24, R"zZXx(
|
434 | Other Mini Languages <a class="group-link" href="chap-mini-lang.html">mini-lang</a>
|
435 |
|
436 |
|
437 | [Patterns] glob-pat *.py
|
438 | [Other Sublang] braces {alice,bob}@example.com
|
439 | )zZXx");
|
440 |
|
441 | GLOBAL_STR(gStr25, R"zZXx(
|
442 | Global Shell Options <a class="group-link" href="chap-option.html">option</a>
|
443 |
|
444 |
|
445 | [Groups] strict:all ysh:upgrade ysh:all
|
446 | [YSH Details] opts-redefine opts-internal
|
447 | )zZXx");
|
448 |
|
449 | GLOBAL_STR(gStr26, R"zZXx(
|
450 | Plugins and Hooks <a class="group-link" href="chap-plugin.html">plugin</a>
|
451 |
|
452 |
|
453 | [YSH] renderPrompt()
|
454 | )zZXx");
|
455 |
|
456 | GLOBAL_STR(gStr27, R"zZXx(
|
457 | Special Variables <a class="group-link" href="chap-special-var.html">special-var</a>
|
458 |
|
459 |
|
460 | [YSH Vars] ARGV X ENV X _ESCAPE
|
461 | _this_dir
|
462 | [YSH Status] _error
|
463 | _pipeline_status _process_sub_status
|
464 | [YSH Tracing] SHX_indent SHX_punct SHX_pid_str
|
465 | [YSH read] _reply
|
466 | [History] YSH_HISTFILE
|
467 | [Oils VM] OILS_VERSION
|
468 | OILS_GC_THRESHOLD OILS_GC_ON_EXIT
|
469 | OILS_GC_STATS OILS_GC_STATS_FD
|
470 | )zZXx");
|
471 |
|
472 | GLOBAL_STR(gStr28, R"zZXx(
|
473 | Standard Library<a class="group-link" href="chap-stdlib.html">stdlib</a>
|
474 |
|
475 |
|
476 | [Args Parser] parser Parse command line arguments
|
477 | flag
|
478 | arg
|
479 | rest
|
480 | parseArgs()
|
481 | X [Testing] describe Test harness
|
482 | X [Lines] slurp-by combine adjacent lines into cells
|
483 | X [Awk] each-line --j8 --max-jobs (Str, Template, Block) - xargs
|
484 | each-row --max-jobs (Str, Template, Block) - xargs
|
485 | split-by (str=\n, ifs=':', pattern=/s+/)
|
486 | if-split-by
|
487 | chop alias for split-by (pattern=/s+/)
|
488 | must-match (/ <capture d+> </capture w+> /)
|
489 | if-match
|
490 | X [Table Create] table --by-row --by-col (&place); construct/parse a table
|
491 | table/cols cols name age - cols name:Str age:Int
|
492 | types type Str Int
|
493 | attr attr units - secs
|
494 | row emit row
|
495 | table cat concatenate TSV8
|
496 | table align to ssv8
|
497 | table tabify to tsv8
|
498 | table header (cols = :|name age|, types = :|Str Int|, units = :|- secs|)
|
499 | table slice e.g. slice (1, -1) slice (5, 7)
|
500 | table to-tsv lose type info, and error on \t in cells
|
501 | X [Table Ops] where subset of rows; dplyr filter()
|
502 | pick subset of columns ('select' taken by shell)
|
503 | mutate transmute [average = count / sum] - drop the ones that are used?
|
504 | rename (bytes='bytes', path='filename')
|
505 | group-by add a column with a group ID [ext]
|
506 | sort-by sort by columns; dplyr arrange() [ext]
|
507 | summary count, sum, histogram, any, all, reduce(), etc. [ext]
|
508 | )zZXx");
|
509 |
|
510 | GLOBAL_STR(gStr29, R"zZXx(
|
511 | Types and Methods <a class="group-link" href="chap-type-method.html">type-method</a>
|
512 |
|
513 |
|
514 | [Atom Types] Null Bool
|
515 | [Number Types] Int Float
|
516 | [Str] X find() replace()
|
517 | trim() trimStart() trimEnd()
|
518 | startsWith() endsWith()
|
519 | upper() lower()
|
520 | search() leftMatch()
|
521 | [List] List/append() pop() extend() indexOf()
|
522 | X insert() X remove() reverse()
|
523 | [Dict] keys() values() X get() X erase()
|
524 | X inc() X accum()
|
525 | [Range]
|
526 | [Eggex]
|
527 | [Match] group() start() end()
|
528 | X groups() X groupDict()
|
529 | [Place] setValue()
|
530 | [Code Types] Expr Command
|
531 | BuiltinFunc BoundFunc
|
532 | X [Func] name() location() toJson()
|
533 | X [Proc] name() location() toJson()
|
534 | X [Module] name() filename()
|
535 | [IO] X eval() X captureStdout()
|
536 | promptVal()
|
537 | X time() X strftime()
|
538 | X glob()
|
539 | X [Guts] heapId()
|
540 | )zZXx");
|
541 |
|
542 | GLOBAL_STR(gStr30, R"zZXx(bin/ysh is the shell with data tYpes, influenced by pYthon, JavaScript, ...
|
543 |
|
544 | Usage: ysh FLAG* SCRIPT ARG*
|
545 | ysh FLAG* -c COMMAND ARG*
|
546 | ysh FLAG*
|
547 |
|
548 | `bin/ysh` is the same as `bin/osh` with a the `ysh:all` option group set. So
|
549 | `bin/ysh` also accepts shell flags.
|
550 |
|
551 | ysh -c 'echo hi'
|
552 | ysh myscript.ysh
|
553 | echo 'echo hi' | ysh
|
554 | )zZXx");
|
555 |
|
556 | GLOBAL_STR(gStr31, R"zZXx(
|
557 | Word Language <a class="group-link" href="chap-word-lang.html">word-lang</a>
|
558 |
|
559 |
|
560 | [Quotes] ysh-string "x is $x" $"x is $x" r'[a-z]\n'
|
561 | u'line\n' b'byte \yff'
|
562 | triple-quoted """ $""" r''' u''' b'''
|
563 | X tagged-str "<span id=$x>"html
|
564 | [Substitutions] expr-sub echo $[42 + a[i]]
|
565 | expr-splice echo @[split(x)]
|
566 | var-splice @myarray @ARGV
|
567 | command-sub @(split command)
|
568 | [Formatting] X ysh-printf ${x %.3f}
|
569 | X ysh-format ${x|html}
|
570 | )zZXx");
|
571 |
|
572 | GLOBAL_STR(gStr32, R"zZXx(
|
573 | YSH Command Language Keywords <a class="group-link" href="chap-ysh-cmd.html">ysh-cmd</a>
|
574 |
|
575 |
|
576 | [Assignment] const var Declare variables
|
577 | setvar setvar a[i] = 42
|
578 | setglobal setglobal d.key = 'foo'
|
579 | [Expression] equal = = 1 + 2*3
|
580 | call call mylist->append(42)
|
581 | [Definitions] proc proc p (s, ...rest) {
|
582 | typed proc p (; typed, ...rest; n=0; b) {
|
583 | func func f(x; opt1, opt2) { return (x + 1) }
|
584 | ysh-return return (myexpr)
|
585 | )zZXx");
|
586 |
|
587 | GLOBAL_STR(gStr33, R"zZXx(# args.ysh
|
588 | #
|
589 | # Usage:
|
590 | # source --builtin args.sh
|
591 | #
|
592 | # parser (&spec) {
|
593 | # flag -v --verbose (help="Verbosely") # default is Bool, false
|
594 | #
|
595 | # flag -P --max-procs ('int', default=-1, doc='''
|
596 | # Run at most P processes at a time
|
597 | # ''')
|
598 | #
|
599 | # flag -i --invert ('bool', default=true, doc='''
|
600 | # Long multiline
|
601 | # Description
|
602 | # ''')
|
603 | #
|
604 | # arg src (help='Source')
|
605 | # arg dest (help='Dest')
|
606 | # arg times (help='Foo')
|
607 | #
|
608 | # rest files
|
609 | # }
|
610 | #
|
611 | # var args = parseArgs(spec, ARGV)
|
612 | #
|
613 | # echo "Verbose $[args.verbose]"
|
614 |
|
615 | # TODO: See list
|
616 | # - It would be nice to keep `flag` and `arg` private, injecting them into the
|
617 | # proc namespace only within `Args`
|
618 | # - We need "type object" to replace the strings 'int', 'bool', etc.
|
619 | # - flag builtin:
|
620 | # - handle only long flag or only short flag
|
621 | # - flag aliases
|
622 |
|
623 | proc parser (; place ; ; block_def) {
|
624 | ## Create an args spec which can be passed to parseArgs.
|
625 | ##
|
626 | ## Example:
|
627 | ##
|
628 | ## # NOTE: &spec will create a variable named spec
|
629 | ## parser (&spec) {
|
630 | ## flag -v --verbose ('bool')
|
631 | ## }
|
632 | ##
|
633 | ## var args = parseArgs(spec, ARGV)
|
634 |
|
635 | var p = {flags: [], args: []}
|
636 | ctx push (p; ; block_def)
|
637 |
|
638 | # Validate that p.rest = [name] or null and reduce p.rest into name or null.
|
639 | if ('rest' in p) {
|
640 | if (len(p.rest) > 1) {
|
641 | error '`rest` was called more than once' (code=3)
|
642 | } else {
|
643 | setvar p.rest = p.rest[0]
|
644 | }
|
645 | } else {
|
646 | setvar p.rest = null
|
647 | }
|
648 |
|
649 | var names = {}
|
650 | for items in ([p.flags, p.args]) {
|
651 | for x in (items) {
|
652 | if (x.name in names) {
|
653 | error "Duplicate flag/arg name $[x.name] in spec" (code=3)
|
654 | }
|
655 |
|
656 | setvar names[x.name] = null
|
657 | }
|
658 | }
|
659 |
|
660 | # TODO: what about `flag --name` and then `arg name`?
|
661 |
|
662 | call place->setValue(p)
|
663 | }
|
664 |
|
665 | proc flag (short, long ; type='bool' ; default=null, help=null) {
|
666 | ## Declare a flag within an `arg-parse`.
|
667 | ##
|
668 | ## Examples:
|
669 | ##
|
670 | ## arg-parse (&spec) {
|
671 | ## flag -v --verbose
|
672 | ## flag -n --count ('int', default=1)
|
673 | ## flag -f --file ('str', help="File to process")
|
674 | ## }
|
675 |
|
676 | # bool has a default of false, not null
|
677 | if (type === 'bool' and default === null) {
|
678 | setvar default = false
|
679 | }
|
680 |
|
681 | # TODO: validate `type`
|
682 |
|
683 | # TODO: Should use "trimPrefix"
|
684 | var name = long[2:]
|
685 |
|
686 | ctx emit flags ({short, long, name, type, default, help})
|
687 | }
|
688 |
|
689 | proc arg (name ; ; help=null) {
|
690 | ## Declare a positional argument within an `arg-parse`.
|
691 | ##
|
692 | ## Examples:
|
693 | ##
|
694 | ## arg-parse (&spec) {
|
695 | ## arg name
|
696 | ## arg config (help="config file path")
|
697 | ## }
|
698 |
|
699 | ctx emit args ({name, help})
|
700 | }
|
701 |
|
702 | proc rest (name) {
|
703 | ## Take the remaining positional arguments within an `arg-parse`.
|
704 | ##
|
705 | ## Examples:
|
706 | ##
|
707 | ## arg-parse (&grepSpec) {
|
708 | ## arg query
|
709 | ## rest files
|
710 | ## }
|
711 |
|
712 | # We emit instead of set to detect multiple invocations of "rest"
|
713 | ctx emit rest (name)
|
714 | }
|
715 |
|
716 | func parseArgs(spec, argv) {
|
717 | ## Given a spec created by `parser`. Parse an array of strings `argv` per
|
718 | ## that spec.
|
719 | ##
|
720 | ## See `parser` for examples of use.
|
721 |
|
722 | var i = 0
|
723 | var positionalPos = 0
|
724 | var argc = len(argv)
|
725 | var args = {}
|
726 | var rest = []
|
727 |
|
728 | var value
|
729 | var found
|
730 | while (i < argc) {
|
731 | var arg = argv[i]
|
732 | if (arg->startsWith('-')) {
|
733 | setvar found = false
|
734 |
|
735 | for flag in (spec.flags) {
|
736 | if ( (flag.short and flag.short === arg) or
|
737 | (flag.long and flag.long === arg) ) {
|
738 | case (flag.type) {
|
739 | ('bool') | (null) { setvar value = true }
|
740 | int {
|
741 | setvar i += 1
|
742 | if (i >= len(argv)) {
|
743 | error "Expected integer after '$arg'" (code=2)
|
744 | }
|
745 |
|
746 | try { setvar value = int(argv[i]) }
|
747 | if (_status !== 0) {
|
748 | error "Expected integer after '$arg', got '$[argv[i]]'" (code=2)
|
749 | }
|
750 | }
|
751 | }
|
752 |
|
753 | setvar args[flag.name] = value
|
754 | setvar found = true
|
755 | break
|
756 | }
|
757 | }
|
758 |
|
759 | if (not found) {
|
760 | error "Unknown flag '$arg'" (code=2)
|
761 | }
|
762 | } elif (positionalPos >= len(spec.args)) {
|
763 | if (not spec.rest) {
|
764 | error "Too many arguments, unexpected '$arg'" (code=2)
|
765 | }
|
766 |
|
767 | call rest->append(arg)
|
768 | } else {
|
769 | var pos = spec.args[positionalPos]
|
770 | setvar positionalPos += 1
|
771 | setvar value = arg
|
772 | setvar args[pos.name] = value
|
773 | }
|
774 |
|
775 | setvar i += 1
|
776 | }
|
777 |
|
778 | if (spec.rest) {
|
779 | setvar args[spec.rest] = rest
|
780 | }
|
781 |
|
782 | # Set defaults for flags
|
783 | for flag in (spec.flags) {
|
784 | if (flag.name not in args) {
|
785 | setvar args[flag.name] = flag.default
|
786 | }
|
787 | }
|
788 |
|
789 | # Raise error on missing args
|
790 | for arg in (spec.args) {
|
791 | if (arg.name not in args) {
|
792 | error "Usage Error: Missing required argument $[arg.name]" (code=2)
|
793 | }
|
794 | }
|
795 |
|
796 | return (args)
|
797 | }
|
798 | )zZXx");
|
799 |
|
800 | GLOBAL_STR(gStr34, R"zZXx(func identity(x) {
|
801 | ## The identity function. Returns its argument.
|
802 |
|
803 | return (x)
|
804 | }
|
805 | )zZXx");
|
806 |
|
807 | GLOBAL_STR(gStr35, R"zZXx(func any(list) {
|
808 | ## Returns true if any value in the list is truthy.
|
809 | ##
|
810 | ## If the list is empty, return false.
|
811 |
|
812 | for item in (list) {
|
813 | if (item) {
|
814 | return (true)
|
815 | }
|
816 | }
|
817 | return (false)
|
818 | }
|
819 |
|
820 | func all(list) {
|
821 | ## Returns true if all values in the list are truthy.
|
822 | ##
|
823 | ## If the list is empty, return true.
|
824 |
|
825 | for item in (list) {
|
826 | if (not item) {
|
827 | return (false)
|
828 | }
|
829 | }
|
830 | return (true)
|
831 | }
|
832 |
|
833 | func sum(list; start=0) {
|
834 | ## Computes the sum of all elements in the list.
|
835 | ##
|
836 | ## Returns 0 for an empty list.
|
837 |
|
838 | var sum = start
|
839 | for item in (list) {
|
840 | setvar sum += item
|
841 | }
|
842 | return (sum)
|
843 | }
|
844 | )zZXx");
|
845 |
|
846 | GLOBAL_STR(gStr36, R"zZXx(func __math_select(list, cmp) {
|
847 | ## Internal helper for `max` and `min`.
|
848 | ##
|
849 | ## NOTE: If `list` is empty, then an error is thrown.
|
850 |
|
851 | if (len(list) === 0) {
|
852 | error "Unexpected empty list" (code=3)
|
853 | }
|
854 |
|
855 | if (len(list) === 1) {
|
856 | return (list[0])
|
857 | }
|
858 |
|
859 | var match = list[0]
|
860 | for i in (1 .. len(list)) {
|
861 | setvar match = cmp(list[i], match)
|
862 | }
|
863 | return (match)
|
864 | }
|
865 |
|
866 | func max(...args) {
|
867 | ## Compute the maximum of 2 or more values.
|
868 | ##
|
869 | ## `max` takes two different signatures:
|
870 | ## - `max(a, b)` to return the maximum of `a`, `b`
|
871 | ## - `max(list)` to return the greatest item in the `list`
|
872 | ##
|
873 | ## So, for example:
|
874 | ##
|
875 | ## max(1, 2) # => 2
|
876 | ## max([1, 2, 3]) # => 3
|
877 |
|
878 | case (len(args)) {
|
879 | (1) { return (__math_select(args[0], max)) }
|
880 | (2) {
|
881 | if (args[0] > args[1]) {
|
882 | return (args[0])
|
883 | } else {
|
884 | return (args[1])
|
885 | }
|
886 | }
|
887 | (else) { error "max expects 1 or 2 args" (code=3) }
|
888 | }
|
889 | }
|
890 |
|
891 | func min(...args) {
|
892 | ## Compute the minimum of 2 or more values.
|
893 | ##
|
894 | ## `min` takes two different signatures:
|
895 | ## - `min(a, b)` to return the minimum of `a`, `b`
|
896 | ## - `min(list)` to return the least item in the `list`
|
897 | ##
|
898 | ## So, for example:
|
899 | ##
|
900 | ## min(2, 3) # => 2
|
901 | ## max([1, 2, 3]) # => 1
|
902 |
|
903 | case (len(args)) {
|
904 | (1) { return (__math_select(args[0], min)) }
|
905 | (2) {
|
906 | if (args[0] < args[1]) {
|
907 | return (args[0])
|
908 | } else {
|
909 | return (args[1])
|
910 | }
|
911 | }
|
912 | (else) { error "min expects 1 or 2 args" (code=3) }
|
913 | }
|
914 | }
|
915 |
|
916 | func abs(x) {
|
917 | ## Compute the absolute (positive) value of a number (float or int).
|
918 |
|
919 | if (x < 0) {
|
920 | return (-x)
|
921 | } else {
|
922 | return (x)
|
923 | }
|
924 | }
|
925 | )zZXx");
|
926 |
|
927 | GLOBAL_STR(gStr37, R"zZXx(# Can we define methods in pure YSH?
|
928 | #
|
929 | # (mylist->find(42) !== -1)
|
930 | #
|
931 | # instead of
|
932 | #
|
933 | # ('42' in mylist)
|
934 | #
|
935 | # Because 'in' is for Dict
|
936 |
|
937 | func find (haystack List, needle) {
|
938 | for i, x in (haystack) {
|
939 | if (x === needle) {
|
940 | return (i)
|
941 | }
|
942 | }
|
943 | return (-1)
|
944 | }
|
945 | )zZXx");
|
946 |
|
947 | GLOBAL_STR(gStr38, R"zZXx(# Library to turn a shell file into a "BYO test server"
|
948 | #
|
949 | # Usage:
|
950 | #
|
951 | # # from both bash and OSH
|
952 | # if test -z "$LIB_OSH"; then LIB_OSH=stdlib/osh; fi
|
953 | # source $LIB_OSH/byo-server-lib.sh
|
954 | #
|
955 | # The client creates a clean process state and directory state for each tests.
|
956 | #
|
957 | # (This file requires compgen -A, and maybe declare -f, so it's not POSIX
|
958 | # shell.)
|
959 |
|
960 | # TODO: How do I get stdlib/two.sh
|
961 | log() {
|
962 | echo "$@" >& 2
|
963 | }
|
964 |
|
965 | die() {
|
966 | log "$0: fatal: $@"
|
967 | exit 1
|
968 | }
|
969 |
|
970 | byo-maybe-run() {
|
971 | local command=${BYO_COMMAND:-}
|
972 |
|
973 | case $command in
|
974 | '')
|
975 | # Do nothing if it's not specified
|
976 | return
|
977 | ;;
|
978 |
|
979 | detect)
|
980 | # all the commands supported, except 'detect'
|
981 | echo list-tests
|
982 | echo run-test
|
983 |
|
984 | exit 66 # ASCII code for 'B' - what the protocol specifies
|
985 | ;;
|
986 |
|
987 | list-tests)
|
988 | # bash extension that OSH also implements
|
989 | compgen -A function | grep '^test-'
|
990 | exit 0
|
991 | ;;
|
992 |
|
993 | run-test)
|
994 | local test_name=${BYO_ARG:-}
|
995 | if test -z "$test_name"; then
|
996 | die "BYO run-test: Expected BYO_ARG"
|
997 | fi
|
998 |
|
999 | # Shell convention: we name functions test-*
|
1000 | $test_name
|
1001 |
|
1002 | # Only run if not set -e. Either way it's equivalent
|
1003 | exit $?
|
1004 | ;;
|
1005 |
|
1006 | *)
|
1007 | die "Invalid BYO command '$command'"
|
1008 | ;;
|
1009 | esac
|
1010 |
|
1011 | # Do nothing if BYO_COMMAND is not set.
|
1012 | # The program continues to its "main".
|
1013 | }
|
1014 |
|
1015 | byo-must-run() {
|
1016 | local command=${BYO_COMMAND:-}
|
1017 | if test -z "$command"; then
|
1018 | die "Expected BYO_COMMAND= in environment"
|
1019 | fi
|
1020 |
|
1021 | byo-maybe-run
|
1022 | }
|
1023 | )zZXx");
|
1024 |
|
1025 | GLOBAL_STR(gStr39, R"zZXx(#!/usr/bin/env bash
|
1026 | #
|
1027 | # Testing library for bash and OSH.
|
1028 | #
|
1029 | # Capture status/stdout/stderr, and sh-assert.
|
1030 |
|
1031 | source stdlib/osh/two.sh
|
1032 |
|
1033 | sh-assert() {
|
1034 | ### Must be run with errexit off
|
1035 |
|
1036 | if ! test "$@"; then
|
1037 | # old message
|
1038 | # note: it's extremely weird that we use -1 and 0, but that seems to be how
|
1039 | # bash works.
|
1040 | #die "${BASH_SOURCE[-1]}:${BASH_LINENO[0]}: sh-assertn '$@' failed"
|
1041 |
|
1042 | die "line ${BASH_LINENO[0]}: sh-assert '$@' failed"
|
1043 | fi
|
1044 | }
|
1045 |
|
1046 | capture-command() {
|
1047 | ### capture status and stdout
|
1048 |
|
1049 | local -n out_status=$1
|
1050 | local -n out_stdout=$2
|
1051 | shift 2
|
1052 |
|
1053 | local __status
|
1054 | local __stdout
|
1055 |
|
1056 | # Tricky: turn errexit off so we can capture it, but turn it on against
|
1057 | set +o errexit
|
1058 | __stdout=$(set -o errexit; "$@")
|
1059 | __status=$?
|
1060 | set -o errexit
|
1061 |
|
1062 | out_status=$__status
|
1063 | out_stdout=$__stdout
|
1064 | }
|
1065 |
|
1066 | capture-command-2() {
|
1067 | ### capture status and stderr
|
1068 |
|
1069 | # This is almost identical to the above
|
1070 |
|
1071 | local -n out_status=$1
|
1072 | local -n out_stderr=$2
|
1073 | shift 2
|
1074 |
|
1075 | local __status
|
1076 | local __stderr
|
1077 |
|
1078 | # Tricky: turn errexit off so we can capture it, but turn it on against
|
1079 | set +o errexit
|
1080 | __stderr=$(set -o errexit; "$@" 2>&1)
|
1081 | __status=$?
|
1082 | set -o errexit
|
1083 |
|
1084 | out_status=$__status
|
1085 | out_stderr=$__stderr
|
1086 | }
|
1087 |
|
1088 | _demo-stderr() {
|
1089 | echo zzz "$@" >& 2
|
1090 | return 99
|
1091 | }
|
1092 |
|
1093 | test-capture-command() {
|
1094 | local status stdout
|
1095 | capture-command status stdout \
|
1096 | echo hi
|
1097 |
|
1098 | sh-assert 0 = "$status"
|
1099 | sh-assert 'hi' = "$stdout"
|
1100 |
|
1101 | local stderr
|
1102 | capture-command-2 status stderr \
|
1103 | _demo-stderr yyy
|
1104 |
|
1105 | #echo "stderr: [$stderr]"
|
1106 |
|
1107 | sh-assert 99 = "$status"
|
1108 | sh-assert 'zzz yyy' = "$stderr"
|
1109 |
|
1110 | capture-command status stdout \
|
1111 | _demo-stderr aaa
|
1112 |
|
1113 | #echo "stderr: [$stderr]"
|
1114 |
|
1115 | sh-assert 99 = "$status"
|
1116 | sh-assert '' = "$stdout"
|
1117 | }
|
1118 |
|
1119 | name=$(basename $0)
|
1120 | if test "$name" = 'testing.sh'; then
|
1121 | "$@"
|
1122 | fi
|
1123 |
|
1124 | )zZXx");
|
1125 |
|
1126 | GLOBAL_STR(gStr40, R"zZXx(# Two functions I actually use, all the time.
|
1127 | #
|
1128 | # To keep depenedencies small, this library will NEVER grow other functions
|
1129 | # (and is named to imply that.)
|
1130 | #
|
1131 | # Usage:
|
1132 | # source --builtin two.sh
|
1133 | #
|
1134 | # Examples:
|
1135 | # log 'hi'
|
1136 | # die 'expected a number'
|
1137 |
|
1138 | log() {
|
1139 | ### Write a message to stderr.
|
1140 | echo "$@" >&2
|
1141 | }
|
1142 |
|
1143 | die() {
|
1144 | ### Write an error message with the script name, and exit with status 1.
|
1145 | log "$0: fatal: $@"
|
1146 | exit 1
|
1147 | }
|
1148 |
|
1149 | )zZXx");
|
1150 |
|
1151 | GLOBAL_STR(gStr41, R"zZXx(# These were helpful while implementing args.ysh
|
1152 | # Maybe we will want to export them in a prelude so that others can use them too?
|
1153 | #
|
1154 | # Prior art: Rust has `todo!()` which is quite nice. Other languages allow
|
1155 | # users to `raise NotImplmentedError()`.
|
1156 |
|
1157 | # Andy comments:
|
1158 | # - 'pass' can be : or true in shell. It's a little obscure / confusing, but
|
1159 | # there is an argument for minimalism. Although I prefer words like 'true',
|
1160 | # and that already means something.
|
1161 | # - UPDATE: we once took 'pass' as a keyword, but users complained because
|
1162 | # there is a command 'pass'. So we probably can't have this by default.
|
1163 | # Need to discuss source --builtin.
|
1164 |
|
1165 | # - todo could be more static? Rust presumably does it at compile time
|
1166 |
|
1167 | proc todo () {
|
1168 | ## Raises a not implemented error when run.
|
1169 | error ("TODO: not implemented") # TODO: is error code 1 ok?
|
1170 | }
|
1171 |
|
1172 | proc pass () {
|
1173 | ## Use when you want to temporarily leave a block empty.
|
1174 | _ null
|
1175 | }
|
1176 | )zZXx");
|
1177 |
|
1178 | GLOBAL_STR(gStr42, R"zZXx(# stream.ysh
|
1179 | #
|
1180 | # Usage:
|
1181 | # source --builtin stream.ysh
|
1182 | #
|
1183 | # For reading lines, decoding, extracting, splitting
|
1184 |
|
1185 | # make this file a test server
|
1186 | source --builtin osh/byo-server-lib.sh
|
1187 |
|
1188 | source --builtin args.ysh
|
1189 |
|
1190 | proc slurp-by (; num_lines) {
|
1191 | # TODO: (stdin)
|
1192 | for line in <> {
|
1193 | echo TODO
|
1194 | }
|
1195 | }
|
1196 |
|
1197 | # Note:
|
1198 | # - these are all the same algorithm
|
1199 | # - also word, block, etc. are all optional
|
1200 |
|
1201 | proc each-line (...words; template=null; ; block=null) {
|
1202 | # TODO:
|
1203 | # parse --j8 --max-jobs flag
|
1204 |
|
1205 | # parse template_str as string
|
1206 | # TODO: this is dangerous though ... because you can execute code
|
1207 | # I think you need a SAFE version
|
1208 |
|
1209 | # evaluate template string expression - I guess that allows $(echo hi) and so
|
1210 | # forth
|
1211 |
|
1212 | # evaluate block with _line binding
|
1213 | # block: execute in parallel with --max-jobs
|
1214 |
|
1215 | for line in <> {
|
1216 | echo TODO
|
1217 | }
|
1218 | }
|
1219 |
|
1220 | proc test-each-line {
|
1221 | echo 'TODO: need basic test runner'
|
1222 |
|
1223 | # ysh-tool test stream.ysh
|
1224 | #
|
1225 | # Col
|
1226 |
|
1227 |
|
1228 | }
|
1229 |
|
1230 | proc each-row (; ; block) {
|
1231 | echo TODO
|
1232 | }
|
1233 |
|
1234 | proc split-by (; ifs=null; block) {
|
1235 | echo TODO
|
1236 | }
|
1237 |
|
1238 | proc if-split-by (; ifs=null; block) {
|
1239 | echo TODO
|
1240 | }
|
1241 |
|
1242 | proc chop () {
|
1243 | ### alias for if-split-by
|
1244 | echo TODO
|
1245 | }
|
1246 |
|
1247 | proc must-match (; pattern; block) {
|
1248 | echo TODO
|
1249 | }
|
1250 |
|
1251 | proc if-match (; pattern; block) {
|
1252 | echo TODO
|
1253 | }
|
1254 |
|
1255 | # Protocol:
|
1256 | #
|
1257 | # - The file lists its tests the "actions"
|
1258 | # - Then the test harness runs them
|
1259 | # - But should it be ENV vars
|
1260 | #
|
1261 | # - BYO_LIST_TESTS=1
|
1262 | # - BYO_RUN_TEST=foo
|
1263 | # - $PWD is a CLEAN temp dir, the process doesn't have to do anything
|
1264 |
|
1265 | # - silent on success, but prints file on output
|
1266 | # - OK this makes sense
|
1267 | #
|
1268 | # The trivial test in Python:
|
1269 | #
|
1270 | # from test import byo
|
1271 | # byo.maybe_main()
|
1272 | #
|
1273 | # bash library:
|
1274 | # source --builtin byo-server.sh
|
1275 | #
|
1276 | # byo-maybe-main # reads env variables, and then exits
|
1277 | #
|
1278 | # source --builtin assertions.ysh
|
1279 | #
|
1280 | # assert-ok 'echo hi'
|
1281 | # assert-stdout 'hi' 'echo -n hi'
|
1282 | #
|
1283 | # "$@"
|
1284 | #
|
1285 | # Run all tests
|
1286 | # util/byo-client.sh run-tests $YSH stdlib/table.ysh
|
1287 | # util/byo-client.sh run-tests -f x $YSH stdlib/table.ysh
|
1288 |
|
1289 | # Clean process
|
1290 | # Clean working dir
|
1291 |
|
1292 | #
|
1293 | # Stream Protocol:
|
1294 | # #.byo - is this she-dot, that's for a file
|
1295 | # Do we need metadata?
|
1296 | #
|
1297 |
|
1298 | # The harness
|
1299 | #
|
1300 | # It's process based testing.
|
1301 | #
|
1302 | # Test runner process: bash or OSH (unlike sharness!)
|
1303 | # Tested process: any language - bash,
|
1304 | #
|
1305 | # Key point: you don't have to quote shell code?
|
1306 |
|
1307 | list-byo-tests() {
|
1308 | echo TODO
|
1309 | }
|
1310 |
|
1311 | run-byo-tests() {
|
1312 | # source it
|
1313 | echo TODO
|
1314 | }
|
1315 |
|
1316 | byo-maybe-run
|
1317 | )zZXx");
|
1318 |
|
1319 | GLOBAL_STR(gStr43, R"zZXx(# table.ysh - Library for tables.
|
1320 | #
|
1321 | # Usage:
|
1322 | # source --builtin table.ysh
|
1323 |
|
1324 | # make this file a test server
|
1325 | source --builtin osh/byo-server-lib.sh
|
1326 |
|
1327 | proc table (...words; place; ; block) {
|
1328 | var n = len(words)
|
1329 |
|
1330 | # TODO: parse flags
|
1331 | #
|
1332 | # --by-row
|
1333 | # --by-col
|
1334 | #
|
1335 | # Place is optional
|
1336 |
|
1337 | if (n === 0) {
|
1338 | echo TODO
|
1339 | return
|
1340 | }
|
1341 |
|
1342 | var action = words[0]
|
1343 |
|
1344 | # textual operations
|
1345 | case (action) {
|
1346 | cat {
|
1347 | echo todo
|
1348 | }
|
1349 | align {
|
1350 | echo todo
|
1351 | }
|
1352 | tabify {
|
1353 | echo todo
|
1354 | }
|
1355 | tabify {
|
1356 | echo todo
|
1357 | }
|
1358 | header {
|
1359 | echo todo
|
1360 | }
|
1361 | slice {
|
1362 | # this has typed args
|
1363 | # do we do some sort of splat?
|
1364 | echo todo
|
1365 | }
|
1366 | to-tsv {
|
1367 | echo todo
|
1368 | }
|
1369 | }
|
1370 |
|
1371 | echo TODO
|
1372 | }
|
1373 |
|
1374 | proc test-table {
|
1375 | return
|
1376 |
|
1377 | table (&files1) {
|
1378 | cols num_bytes path
|
1379 | type Int Str
|
1380 |
|
1381 | row 10 README.md
|
1382 | row 12 main.py
|
1383 |
|
1384 | row (12, 'lib.py')
|
1385 | row (num_bytes=12, path='util.py')
|
1386 | }
|
1387 |
|
1388 | # 2 columns - The default is by column?
|
1389 | assert ['Dict' === type(files1)]
|
1390 | assert [2 === len(files1)]
|
1391 |
|
1392 | # Same table
|
1393 | table --by-row (&files2) {
|
1394 | cols num_bytes path
|
1395 | type Int Str
|
1396 |
|
1397 | row 10 README.md
|
1398 | row 12 main.py
|
1399 |
|
1400 | row (12, 'lib.py')
|
1401 | row (num_bytes=12, path='util.py')
|
1402 | }
|
1403 |
|
1404 | # 4 rows
|
1405 | assert ['List' === type(files2)]
|
1406 | assert [4 === len(files2)]
|
1407 | }
|
1408 |
|
1409 | # "subcommands" of the dialect
|
1410 |
|
1411 | proc cols (...names) {
|
1412 | # cols name age
|
1413 | echo TODO
|
1414 | }
|
1415 |
|
1416 | proc types (...types) {
|
1417 | # types - Int? Str?
|
1418 | echo TODO
|
1419 | }
|
1420 |
|
1421 | proc attr (name; ...values) {
|
1422 | # attr units ('-', 'secs')
|
1423 | echo TODO
|
1424 | }
|
1425 |
|
1426 | # is this allowed outside table {} blocks too?
|
1427 | proc row {
|
1428 | echo TODO
|
1429 | }
|
1430 |
|
1431 | #
|
1432 | # dplyr names
|
1433 | #
|
1434 |
|
1435 | # TODO: can we parse select?
|
1436 |
|
1437 | proc where {
|
1438 | echo
|
1439 | }
|
1440 |
|
1441 | # TODO: should be able to test argv[0] or something
|
1442 | # Or pass to _mutate-transmute
|
1443 |
|
1444 | proc mutate {
|
1445 | echo TODO
|
1446 | }
|
1447 |
|
1448 | proc transmute {
|
1449 | echo TODO
|
1450 | }
|
1451 |
|
1452 | proc rename {
|
1453 | echo TODO
|
1454 | }
|
1455 |
|
1456 | proc group-by {
|
1457 | echo TODO
|
1458 | }
|
1459 |
|
1460 | proc sort-by {
|
1461 | echo TODO
|
1462 | }
|
1463 |
|
1464 | proc summary {
|
1465 | echo TODO
|
1466 | }
|
1467 |
|
1468 | byo-maybe-run
|
1469 | )zZXx");
|
1470 |
|
1471 | GLOBAL_STR(gStr44, R"zZXx(# testing.ysh
|
1472 | #
|
1473 | # Usage:
|
1474 | # source --builtin testing.sh
|
1475 | #
|
1476 | # func f(x) { return (x + 1) }
|
1477 | #
|
1478 | # describe foo {
|
1479 | # assert (43 === f(42))
|
1480 | # }
|
1481 | #
|
1482 | # if is-main {
|
1483 | # run-tests @ARGV # --filter
|
1484 | # }
|
1485 |
|
1486 | module stdlib/testing || return 0
|
1487 |
|
1488 | source --builtin args.ysh
|
1489 |
|
1490 | proc assert ( ; cond ; fail_message='default fail message') {
|
1491 | echo 'hi from assert'
|
1492 |
|
1493 | = cond
|
1494 |
|
1495 | # I think this might be ready now?
|
1496 |
|
1497 | var val = evalExpr(cond)
|
1498 |
|
1499 | echo
|
1500 | echo 'value'
|
1501 | = val
|
1502 | pp line (val)
|
1503 |
|
1504 | = fail_message
|
1505 |
|
1506 | if (val) {
|
1507 | echo 'OK'
|
1508 | } else {
|
1509 | var m = evalExpr(fail_message)
|
1510 | echo "FAIL - this is where we extract the string - $m"
|
1511 | }
|
1512 | }
|
1513 |
|
1514 | proc test-assert {
|
1515 | var x = 42
|
1516 | assert [42 === x]
|
1517 | }
|
1518 |
|
1519 | proc test-expr ( ; expr ) {
|
1520 | echo 'expr'
|
1521 | pp line (expr)
|
1522 | }
|
1523 |
|
1524 | proc test-named ( ; ; n=^[99] ) {
|
1525 | echo 'n'
|
1526 | pp line (n)
|
1527 | }
|
1528 |
|
1529 | # What happens when there are duplicate test IDs?
|
1530 | #
|
1531 | # Also I think filter by "$test_id/$case_id"
|
1532 |
|
1533 | proc __it (case_id ; ; ; block) {
|
1534 | # This uses a clean directory
|
1535 | echo TODO
|
1536 | }
|
1537 |
|
1538 | # is this accessible to users?
|
1539 | # It can contain a global list of things to run
|
1540 |
|
1541 | # Naming convention: a proc named 'describe' mutates a global named _describe?
|
1542 | # Or maybe _describe_list ?
|
1543 |
|
1544 | var _describe_list = []
|
1545 |
|
1546 | proc describe (test_id ; ; ; block) {
|
1547 | echo describe
|
1548 | #= desc
|
1549 |
|
1550 | # TODO:
|
1551 | # - need append
|
1552 | # - need ::
|
1553 | # _ _describe->append(cmd)
|
1554 | #
|
1555 | # Need to clean this up
|
1556 | # append (_describe, cmd) # does NOT work!
|
1557 |
|
1558 | call _describe_list->append(block)
|
1559 | }
|
1560 |
|
1561 | proc Args {
|
1562 | echo TODO
|
1563 | }
|
1564 |
|
1565 | # Problem: this creates a global variable?
|
1566 | Args (&spec) {
|
1567 | flag --filter 'Regex of test descriptions'
|
1568 | }
|
1569 |
|
1570 | proc run-tests {
|
1571 | var opt, i = parseArgs(spec, ARGV)
|
1572 |
|
1573 | # TODO:
|
1574 | # - parse --filter foo, which you can use eggex for!
|
1575 |
|
1576 | for cmd in (_describe) {
|
1577 | # TODO: print filename and 'describe' name?
|
1578 | try {
|
1579 | eval (cmd)
|
1580 | }
|
1581 | if (_status !== 0) {
|
1582 | echo 'failed'
|
1583 | }
|
1584 | }
|
1585 | }
|
1586 | )zZXx");
|
1587 |
|
1588 |
|
1589 |
|
1590 | TextFile array[] = {
|
1591 | {.rel_path = "_devbuild/help/data-errors", .contents = gStr0},
|
1592 | {.rel_path = "_devbuild/help/data-front-end", .contents = gStr1},
|
1593 | {.rel_path = "_devbuild/help/data-j8-notation", .contents = gStr2},
|
1594 | {.rel_path = "_devbuild/help/help", .contents = gStr3},
|
1595 | {.rel_path = "_devbuild/help/oils-usage", .contents = gStr4},
|
1596 | {.rel_path = "_devbuild/help/osh-builtin-cmd", .contents = gStr5},
|
1597 | {.rel_path = "_devbuild/help/osh-chapters", .contents = gStr6},
|
1598 | {.rel_path = "_devbuild/help/osh-cmd-lang", .contents = gStr7},
|
1599 | {.rel_path = "_devbuild/help/osh-front-end", .contents = gStr8},
|
1600 | {.rel_path = "_devbuild/help/osh-mini-lang", .contents = gStr9},
|
1601 | {.rel_path = "_devbuild/help/osh-option", .contents = gStr10},
|
1602 | {.rel_path = "_devbuild/help/osh-osh-assign", .contents = gStr11},
|
1603 | {.rel_path = "_devbuild/help/osh-plugin", .contents = gStr12},
|
1604 | {.rel_path = "_devbuild/help/osh-special-var", .contents = gStr13},
|
1605 | {.rel_path = "_devbuild/help/osh-stdlib", .contents = gStr14},
|
1606 | {.rel_path = "_devbuild/help/osh-type-method", .contents = gStr15},
|
1607 | {.rel_path = "_devbuild/help/osh-usage", .contents = gStr16},
|
1608 | {.rel_path = "_devbuild/help/osh-word-lang", .contents = gStr17},
|
1609 | {.rel_path = "_devbuild/help/ysh-builtin-cmd", .contents = gStr18},
|
1610 | {.rel_path = "_devbuild/help/ysh-builtin-func", .contents = gStr19},
|
1611 | {.rel_path = "_devbuild/help/ysh-chapters", .contents = gStr20},
|
1612 | {.rel_path = "_devbuild/help/ysh-cmd-lang", .contents = gStr21},
|
1613 | {.rel_path = "_devbuild/help/ysh-expr-lang", .contents = gStr22},
|
1614 | {.rel_path = "_devbuild/help/ysh-front-end", .contents = gStr23},
|
1615 | {.rel_path = "_devbuild/help/ysh-mini-lang", .contents = gStr24},
|
1616 | {.rel_path = "_devbuild/help/ysh-option", .contents = gStr25},
|
1617 | {.rel_path = "_devbuild/help/ysh-plugin", .contents = gStr26},
|
1618 | {.rel_path = "_devbuild/help/ysh-special-var", .contents = gStr27},
|
1619 | {.rel_path = "_devbuild/help/ysh-stdlib", .contents = gStr28},
|
1620 | {.rel_path = "_devbuild/help/ysh-type-method", .contents = gStr29},
|
1621 | {.rel_path = "_devbuild/help/ysh-usage", .contents = gStr30},
|
1622 | {.rel_path = "_devbuild/help/ysh-word-lang", .contents = gStr31},
|
1623 | {.rel_path = "_devbuild/help/ysh-ysh-cmd", .contents = gStr32},
|
1624 | {.rel_path = "stdlib/args.ysh", .contents = gStr33},
|
1625 | {.rel_path = "stdlib/funcs.ysh", .contents = gStr34},
|
1626 | {.rel_path = "stdlib/list.ysh", .contents = gStr35},
|
1627 | {.rel_path = "stdlib/math.ysh", .contents = gStr36},
|
1628 | {.rel_path = "stdlib/methods.ysh", .contents = gStr37},
|
1629 | {.rel_path = "stdlib/osh/byo-server-lib.sh", .contents = gStr38},
|
1630 | {.rel_path = "stdlib/osh/testing.sh", .contents = gStr39},
|
1631 | {.rel_path = "stdlib/osh/two.sh", .contents = gStr40},
|
1632 | {.rel_path = "stdlib/prelude.ysh", .contents = gStr41},
|
1633 | {.rel_path = "stdlib/stream.ysh", .contents = gStr42},
|
1634 | {.rel_path = "stdlib/table.ysh", .contents = gStr43},
|
1635 | {.rel_path = "stdlib/testing.ysh", .contents = gStr44},
|
1636 |
|
1637 | {.rel_path = nullptr, .contents = nullptr},
|
1638 | };
|
1639 |
|
1640 | } // namespace embedded_file
|
1641 |
|
1642 | TextFile* gEmbeddedFiles = embedded_file::array; // turn array into pointer
|