OILS / _gen / bin / text_files.cc View on Github | oilshell.org

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