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

1809 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: ${LIB_OSH:-stdlib/osh}
977source $LIB_OSH/two.sh
978
979byo-maybe-run() {
980 local command=${BYO_COMMAND:-}
981
982 case $command in
983 '')
984 # Do nothing if it's not specified
985 return
986 ;;
987
988 detect)
989 # all the commands supported, except 'detect'
990 echo list-tests
991 echo run-test
992
993 exit 66 # ASCII code for 'B' - what the protocol specifies
994 ;;
995
996 list-tests)
997 # bash extension that OSH also implements
998 compgen -A function | grep '^test-'
999 exit 0
1000 ;;
1001
1002 run-test)
1003 local test_name=${BYO_ARG:-}
1004 if test -z "$test_name"; then
1005 die "BYO run-test: Expected BYO_ARG"
1006 fi
1007
1008 # Shell convention: we name functions test-*
1009 $test_name
1010
1011 # Only run if not set -e. Either way it's equivalent
1012 exit $?
1013 ;;
1014
1015 *)
1016 die "Invalid BYO command '$command'"
1017 ;;
1018 esac
1019
1020 # Do nothing if BYO_COMMAND is not set.
1021 # The program continues to its "main".
1022}
1023
1024byo-must-run() {
1025 local command=${BYO_COMMAND:-}
1026 if test -z "$command"; then
1027 die "Expected BYO_COMMAND= in environment"
1028 fi
1029
1030 byo-maybe-run
1031}
1032)zZXx");
1033
1034GLOBAL_STR(gStr40, R"zZXx(#!/usr/bin/env bash
1035#
1036# Testing library for bash and OSH.
1037#
1038# Capture status/stdout/stderr, and nq-assert those values.
1039
1040: ${LIB_OSH=stdlib/osh}
1041source $LIB_OSH/two.sh
1042
1043nq-assert() {
1044 ### Assertion with same syntax as shell 'test'
1045
1046 if ! test "$@"; then
1047 die "line ${BASH_LINENO[0]}: nq-assert $(printf '%q ' "$@") failed"
1048 fi
1049}
1050
1051# Problem: we want to capture status and stdout at the same time
1052#
1053# We use:
1054#
1055# __stdout=$(set -o errexit; "$@")
1056# __status=$?
1057#
1058# However, we lose the trailing \n, since that's how command subs work.
1059
1060# Here is another possibility:
1061#
1062# shopt -s lastpipe # need this too
1063# ( set -o errexit; "$@" ) | read -r -d __stdout
1064# __status=${PIPESTATUS[0]}
1065# shopt -u lastpipe
1066#
1067# But this feels complex for just the \n issue, which can be easily worked
1068# around.
1069
1070nq-run() {
1071 ### capture status only
1072
1073 local -n out_status=$1
1074 shift
1075
1076 local __status
1077
1078 # Tricky: turn errexit off so we can capture it, but turn it on against
1079 set +o errexit
1080 ( set -o errexit; "$@" )
1081 __status=$?
1082 set -o errexit
1083
1084 out_status=$__status
1085}
1086
1087nq-capture() {
1088 ### capture status and stdout
1089
1090 local -n out_status=$1
1091 local -n out_stdout=$2
1092 shift 2
1093
1094 local __status
1095 local __stdout
1096
1097 # Tricky: turn errexit off so we can capture it, but turn it on against
1098 set +o errexit
1099 __stdout=$(set -o errexit; "$@")
1100 __status=$?
1101 set -o errexit
1102
1103 out_status=$__status
1104 out_stdout=$__stdout
1105}
1106
1107nq-capture-2() {
1108 ### capture status and stderr
1109
1110 # This is almost identical to the above
1111
1112 local -n out_status=$1
1113 local -n out_stderr=$2
1114 shift 2
1115
1116 local __status
1117 local __stderr
1118
1119 # Tricky: turn errexit off so we can capture it, but turn it on against
1120 set +o errexit
1121 __stderr=$(set -o errexit; "$@" 2>&1)
1122 __status=$?
1123 set -o errexit
1124
1125 out_status=$__status
1126 out_stderr=$__stderr
1127}
1128
1129# 'byo test' can set this?
1130: ${NQ_TEST_TEMP=/tmp}
1131
1132nq-redir() {
1133 ### capture status and stdout
1134
1135 local -n out_status=$1
1136 local -n out_stdout_file=$2
1137 shift 2
1138
1139 local __status
1140 local __stdout_file=$NQ_TEST_TEMP/nq-redir-$$.txt
1141
1142 # Tricky: turn errexit off so we can capture it, but turn it on against
1143 set +o errexit
1144 ( set -o errexit; "$@" ) > $__stdout_file
1145 __status=$?
1146 set -o errexit
1147
1148 out_status=$__status
1149 out_stdout_file=$__stdout_file
1150}
1151
1152nq-redir-2() {
1153 ### capture status and stdout
1154
1155 local -n out_status=$1
1156 local -n out_stderr_file=$2
1157 shift 2
1158
1159 local __status
1160 local __stderr_file=$NQ_TEST_TEMP/nq-redir-$$.txt
1161
1162 # Tricky: turn errexit off so we can capture it, but turn it on against
1163 set +o errexit
1164 ( set -o errexit; "$@" ) 2> $__stderr_file
1165 __status=$?
1166 set -o errexit
1167
1168 out_status=$__status
1169 out_stderr_file=$__stderr_file
1170}
1171)zZXx");
1172
1173GLOBAL_STR(gStr41, R"zZXx(#!/usr/bin/env bash
1174#
1175# Common shell functions for task scripts.
1176#
1177# Usage:
1178# source $LIB_OSH/task-five.sh
1179#
1180# test-foo() { # define task functions
1181# echo foo
1182# }
1183# task-five "$@"
1184
1185# Definition of a "task"
1186#
1187# - File invokes task-five "$@"
1188# - or maybe you can look at its source
1189# - It's a shell function
1190# - Has ### docstring
1191# - Doesn't start with _
1192
1193: ${LIB_OSH=stdlib/osh}
1194source $LIB_OSH/byo-server.sh
1195
1196
1197# List all functions defined in this file (and not in sourced files).
1198_bash-print-funcs() {
1199 ### Print shell functions in this file that don't start with _ (bash reflection)
1200
1201 local funcs
1202 funcs=($(compgen -A function))
1203 # extdebug makes `declare -F` print the file path, but, annoyingly, only
1204 # if you pass the function names as arguments.
1205 shopt -s extdebug
1206 declare -F "${funcs[@]}" | grep --fixed-strings " $0" | awk '{print $1}'
1207 shopt -u extdebug
1208}
1209
1210_gawk-print-funcs() {
1211 ### Print shell functions in this file that don't start with _ (awk parsing)
1212
1213 # Using gawk because it has match()
1214 # - doesn't start with _
1215
1216 # space = / ' '* /
1217 # shfunc = / %begin
1218 # <capture !['_' ' '] ![' ']*>
1219 # '()' space '{' space
1220 # %end /
1221 # docstring = / %begin
1222 # space '###' ' '+
1223 # <capture dot*>
1224 # %end /
1225 gawk '
1226 match($0, /^([^_ ][^ ]*)\(\)[ ]*{[ ]*$/, m) {
1227 #print NR " shfunc " m[1]
1228 print m[1]
1229 #print m[0]
1230 }
1231
1232 match($0, /^[ ]*###[ ]+(.*)$/, m) {
1233 print NR " docstring " m[1]
1234 }
1235' $0
1236}
1237
1238_print-funcs() {
1239 if command -v gawk > /dev/null; then
1240 _gawk-print-funcs
1241 else
1242 _bash-print-funcs
1243 fi
1244}
1245
1246_show-help() {
1247 # TODO:
1248 # - Use awk to find comments at the top of the file?
1249 # - Use OSH to extract docstrings
1250 # - BYO_COMMAND=list-tasks will reuse that logic? It only applies to the
1251 # current file, not anything in a different file?
1252
1253 echo "Usage: $0 TASK_NAME ARGS..."
1254 echo
1255 echo "To complete tasks, run:"
1256 echo " source devtools/completion.bash"
1257 echo
1258 echo "Tasks:"
1259
1260 if command -v column >/dev/null; then
1261 _print-funcs | column
1262 else
1263 _print-funcs
1264 fi
1265}
1266
1267task-five() {
1268 # Respond to BYO_COMMAND=list-tasks, etc. All task files need this.
1269 byo-maybe-run
1270
1271 case ${1:-} in
1272 ''|--help|-h)
1273 _show-help
1274 exit 0
1275 ;;
1276 esac
1277
1278 if ! declare -f "$1" >/dev/null; then
1279 echo "$0: '$1' isn't an action in this task file. Try '$0 --help'"
1280 exit 1
1281 fi
1282
1283 "$@"
1284}
1285)zZXx");
1286
1287GLOBAL_STR(gStr42, R"zZXx(# Two functions I actually use, all the time.
1288#
1289# To keep depenedencies small, this library will NEVER grow other functions
1290# (and is named to imply that.)
1291#
1292# Usage:
1293# source --builtin two.sh
1294#
1295# Examples:
1296# log 'hi'
1297# die 'expected a number'
1298
1299if command -v module >/dev/null; then # include guard for YSH
1300 module two || return 0
1301fi
1302
1303log() {
1304 ### Write a message to stderr.
1305 echo "$@" >&2
1306}
1307
1308die() {
1309 ### Write an error message with the script name, and exit with status 1.
1310 log "$0: fatal: $@"
1311 exit 1
1312}
1313
1314)zZXx");
1315
1316GLOBAL_STR(gStr43, R"zZXx(# These were helpful while implementing args.ysh
1317# Maybe we will want to export them in a prelude so that others can use them too?
1318#
1319# Prior art: Rust has `todo!()` which is quite nice. Other languages allow
1320# users to `raise NotImplmentedError()`.
1321
1322# Andy comments:
1323# - 'pass' can be : or true in shell. It's a little obscure / confusing, but
1324# there is an argument for minimalism. Although I prefer words like 'true',
1325# and that already means something.
1326# - UPDATE: we once took 'pass' as a keyword, but users complained because
1327# there is a command 'pass'. So we probably can't have this by default.
1328# Need to discuss source --builtin.
1329
1330# - todo could be more static? Rust presumably does it at compile time
1331
1332proc todo () {
1333 ## Raises a not implemented error when run.
1334 error ("TODO: not implemented") # TODO: is error code 1 ok?
1335}
1336
1337proc pass () {
1338 ## Use when you want to temporarily leave a block empty.
1339 _ null
1340}
1341)zZXx");
1342
1343GLOBAL_STR(gStr44, R"zZXx(# stream.ysh
1344#
1345# Usage:
1346# source --builtin stream.ysh
1347#
1348# For reading lines, decoding, extracting, splitting
1349
1350# make this file a test server
1351source --builtin osh/byo-server.sh
1352
1353source --builtin args.ysh
1354
1355proc slurp-by (; num_lines) {
1356 # TODO: (stdin)
1357 for line in <> {
1358 echo TODO
1359 }
1360}
1361
1362# Note:
1363# - these are all the same algorithm
1364# - also word, block, etc. are all optional
1365
1366proc each-line (...words; template=null; ; block=null) {
1367 # TODO:
1368 # parse --j8 --max-jobs flag
1369
1370 # parse template_str as string
1371 # TODO: this is dangerous though ... because you can execute code
1372 # I think you need a SAFE version
1373
1374 # evaluate template string expression - I guess that allows $(echo hi) and so
1375 # forth
1376
1377 # evaluate block with _line binding
1378 # block: execute in parallel with --max-jobs
1379
1380 for line in <> {
1381 echo TODO
1382 }
1383}
1384
1385proc test-each-line {
1386 echo 'TODO: need basic test runner'
1387
1388 # ysh-tool test stream.ysh
1389 #
1390 # Col
1391
1392
1393}
1394
1395proc each-row (; ; block) {
1396 echo TODO
1397}
1398
1399proc split-by (; ifs=null; block) {
1400 echo TODO
1401}
1402
1403proc if-split-by (; ifs=null; block) {
1404 echo TODO
1405}
1406
1407proc chop () {
1408 ### alias for if-split-by
1409 echo TODO
1410}
1411
1412proc must-match (; pattern; block) {
1413 echo TODO
1414}
1415
1416proc if-match (; pattern; block) {
1417 echo TODO
1418}
1419
1420# Protocol:
1421#
1422# - The file lists its tests the "actions"
1423# - Then the test harness runs them
1424# - But should it be ENV vars
1425#
1426# - BYO_LIST_TESTS=1
1427# - BYO_RUN_TEST=foo
1428# - $PWD is a CLEAN temp dir, the process doesn't have to do anything
1429
1430# - silent on success, but prints file on output
1431# - OK this makes sense
1432#
1433# The trivial test in Python:
1434#
1435# from test import byo
1436# byo.maybe_main()
1437#
1438# bash library:
1439# source --builtin byo-server.sh
1440#
1441# byo-maybe-main # reads env variables, and then exits
1442#
1443# source --builtin assertions.ysh
1444#
1445# assert-ok 'echo hi'
1446# assert-stdout 'hi' 'echo -n hi'
1447#
1448# "$@"
1449#
1450# Run all tests
1451# util/byo-client.sh run-tests $YSH stdlib/table.ysh
1452# util/byo-client.sh run-tests -f x $YSH stdlib/table.ysh
1453
1454# Clean process
1455# Clean working dir
1456
1457#
1458# Stream Protocol:
1459# #.byo - is this she-dot, that's for a file
1460# Do we need metadata?
1461#
1462
1463# The harness
1464#
1465# It's process based testing.
1466#
1467# Test runner process: bash or OSH (unlike sharness!)
1468# Tested process: any language - bash,
1469#
1470# Key point: you don't have to quote shell code?
1471
1472list-byo-tests() {
1473 echo TODO
1474}
1475
1476run-byo-tests() {
1477 # source it
1478 echo TODO
1479}
1480
1481byo-maybe-run
1482)zZXx");
1483
1484GLOBAL_STR(gStr45, R"zZXx(# table.ysh - Library for tables.
1485#
1486# Usage:
1487# source --builtin table.ysh
1488
1489# make this file a test server
1490source --builtin osh/byo-server.sh
1491
1492proc table (...words; place; ; block) {
1493 var n = len(words)
1494
1495 # TODO: parse flags
1496 #
1497 # --by-row
1498 # --by-col
1499 #
1500 # Place is optional
1501
1502 if (n === 0) {
1503 echo TODO
1504 return
1505 }
1506
1507 var action = words[0]
1508
1509 # textual operations
1510 case (action) {
1511 cat {
1512 echo todo
1513 }
1514 align {
1515 echo todo
1516 }
1517 tabify {
1518 echo todo
1519 }
1520 tabify {
1521 echo todo
1522 }
1523 header {
1524 echo todo
1525 }
1526 slice {
1527 # this has typed args
1528 # do we do some sort of splat?
1529 echo todo
1530 }
1531 to-tsv {
1532 echo todo
1533 }
1534 }
1535
1536 echo TODO
1537}
1538
1539proc test-table {
1540 return
1541
1542 table (&files1) {
1543 cols num_bytes path
1544 type Int Str
1545
1546 row 10 README.md
1547 row 12 main.py
1548
1549 row (12, 'lib.py')
1550 row (num_bytes=12, path='util.py')
1551 }
1552
1553 # 2 columns - The default is by column?
1554 assert ['Dict' === type(files1)]
1555 assert [2 === len(files1)]
1556
1557 # Same table
1558 table --by-row (&files2) {
1559 cols num_bytes path
1560 type Int Str
1561
1562 row 10 README.md
1563 row 12 main.py
1564
1565 row (12, 'lib.py')
1566 row (num_bytes=12, path='util.py')
1567 }
1568
1569 # 4 rows
1570 assert ['List' === type(files2)]
1571 assert [4 === len(files2)]
1572}
1573
1574# "subcommands" of the dialect
1575
1576proc cols (...names) {
1577 # cols name age
1578 echo TODO
1579}
1580
1581proc types (...types) {
1582 # types - Int? Str?
1583 echo TODO
1584}
1585
1586proc attr (name; ...values) {
1587 # attr units ('-', 'secs')
1588 echo TODO
1589}
1590
1591# is this allowed outside table {} blocks too?
1592proc row {
1593 echo TODO
1594}
1595
1596#
1597# dplyr names
1598#
1599
1600# TODO: can we parse select?
1601
1602proc where {
1603 echo
1604}
1605
1606# TODO: should be able to test argv[0] or something
1607# Or pass to _mutate-transmute
1608
1609proc mutate {
1610 echo TODO
1611}
1612
1613proc transmute {
1614 echo TODO
1615}
1616
1617proc rename {
1618 echo TODO
1619}
1620
1621proc group-by {
1622 echo TODO
1623}
1624
1625proc sort-by {
1626 echo TODO
1627}
1628
1629proc summary {
1630 echo TODO
1631}
1632
1633byo-maybe-run
1634)zZXx");
1635
1636GLOBAL_STR(gStr46, R"zZXx(# testing.ysh
1637#
1638# Usage:
1639# source --builtin testing.sh
1640#
1641# func f(x) { return (x + 1) }
1642#
1643# describe foo {
1644# assert (43 === f(42))
1645# }
1646#
1647# if is-main {
1648# run-tests @ARGV # --filter
1649# }
1650
1651module stdlib/testing || return 0
1652
1653source --builtin args.ysh
1654
1655proc assert ( ; cond ; fail_message='default fail message') {
1656 echo 'hi from assert'
1657
1658 = cond
1659
1660 # I think this might be ready now?
1661
1662 var val = evalExpr(cond)
1663
1664 echo
1665 echo 'value'
1666 = val
1667 pp line (val)
1668
1669 = fail_message
1670
1671 if (val) {
1672 echo 'OK'
1673 } else {
1674 var m = evalExpr(fail_message)
1675 echo "FAIL - this is where we extract the string - $m"
1676 }
1677}
1678
1679proc test-assert {
1680 var x = 42
1681 assert [42 === x]
1682}
1683
1684proc test-expr ( ; expr ) {
1685 echo 'expr'
1686 pp line (expr)
1687}
1688
1689proc test-named ( ; ; n=^[99] ) {
1690 echo 'n'
1691 pp line (n)
1692}
1693
1694# What happens when there are duplicate test IDs?
1695#
1696# Also I think filter by "$test_id/$case_id"
1697
1698proc __it (case_id ; ; ; block) {
1699 # This uses a clean directory
1700 echo TODO
1701}
1702
1703# is this accessible to users?
1704# It can contain a global list of things to run
1705
1706# Naming convention: a proc named 'describe' mutates a global named _describe?
1707# Or maybe _describe_list ?
1708
1709var _describe_list = []
1710
1711proc describe (test_id ; ; ; block) {
1712 echo describe
1713 #= desc
1714
1715 # TODO:
1716 # - need append
1717 # - need ::
1718 # _ _describe->append(cmd)
1719 #
1720 # Need to clean this up
1721 # append (_describe, cmd) # does NOT work!
1722
1723 call _describe_list->append(block)
1724}
1725
1726proc Args {
1727 echo TODO
1728}
1729
1730# Problem: this creates a global variable?
1731Args (&spec) {
1732 flag --filter 'Regex of test descriptions'
1733}
1734
1735proc run-tests {
1736 var opt, i = parseArgs(spec, ARGV)
1737
1738 # TODO:
1739 # - parse --filter foo, which you can use eggex for!
1740
1741 for cmd in (_describe) {
1742 # TODO: print filename and 'describe' name?
1743 try {
1744 eval (cmd)
1745 }
1746 if (_status !== 0) {
1747 echo 'failed'
1748 }
1749 }
1750}
1751)zZXx");
1752
1753
1754
1755TextFile array[] = {
1756 {.rel_path = "_devbuild/help/data-errors", .contents = gStr0},
1757 {.rel_path = "_devbuild/help/data-front-end", .contents = gStr1},
1758 {.rel_path = "_devbuild/help/data-j8-notation", .contents = gStr2},
1759 {.rel_path = "_devbuild/help/help", .contents = gStr3},
1760 {.rel_path = "_devbuild/help/oils-usage", .contents = gStr4},
1761 {.rel_path = "_devbuild/help/osh-builtin-cmd", .contents = gStr5},
1762 {.rel_path = "_devbuild/help/osh-chapters", .contents = gStr6},
1763 {.rel_path = "_devbuild/help/osh-cmd-lang", .contents = gStr7},
1764 {.rel_path = "_devbuild/help/osh-front-end", .contents = gStr8},
1765 {.rel_path = "_devbuild/help/osh-mini-lang", .contents = gStr9},
1766 {.rel_path = "_devbuild/help/osh-option", .contents = gStr10},
1767 {.rel_path = "_devbuild/help/osh-osh-assign", .contents = gStr11},
1768 {.rel_path = "_devbuild/help/osh-plugin", .contents = gStr12},
1769 {.rel_path = "_devbuild/help/osh-special-var", .contents = gStr13},
1770 {.rel_path = "_devbuild/help/osh-stdlib", .contents = gStr14},
1771 {.rel_path = "_devbuild/help/osh-type-method", .contents = gStr15},
1772 {.rel_path = "_devbuild/help/osh-usage", .contents = gStr16},
1773 {.rel_path = "_devbuild/help/osh-word-lang", .contents = gStr17},
1774 {.rel_path = "_devbuild/help/ysh-builtin-cmd", .contents = gStr18},
1775 {.rel_path = "_devbuild/help/ysh-builtin-func", .contents = gStr19},
1776 {.rel_path = "_devbuild/help/ysh-chapters", .contents = gStr20},
1777 {.rel_path = "_devbuild/help/ysh-cmd-lang", .contents = gStr21},
1778 {.rel_path = "_devbuild/help/ysh-expr-lang", .contents = gStr22},
1779 {.rel_path = "_devbuild/help/ysh-front-end", .contents = gStr23},
1780 {.rel_path = "_devbuild/help/ysh-mini-lang", .contents = gStr24},
1781 {.rel_path = "_devbuild/help/ysh-option", .contents = gStr25},
1782 {.rel_path = "_devbuild/help/ysh-plugin", .contents = gStr26},
1783 {.rel_path = "_devbuild/help/ysh-special-var", .contents = gStr27},
1784 {.rel_path = "_devbuild/help/ysh-stdlib", .contents = gStr28},
1785 {.rel_path = "_devbuild/help/ysh-type-method", .contents = gStr29},
1786 {.rel_path = "_devbuild/help/ysh-usage", .contents = gStr30},
1787 {.rel_path = "_devbuild/help/ysh-word-lang", .contents = gStr31},
1788 {.rel_path = "_devbuild/help/ysh-ysh-cmd", .contents = gStr32},
1789 {.rel_path = "stdlib/args.ysh", .contents = gStr33},
1790 {.rel_path = "stdlib/funcs.ysh", .contents = gStr34},
1791 {.rel_path = "stdlib/list.ysh", .contents = gStr35},
1792 {.rel_path = "stdlib/math.ysh", .contents = gStr36},
1793 {.rel_path = "stdlib/methods.ysh", .contents = gStr37},
1794 {.rel_path = "stdlib/osh/bash-strict.sh", .contents = gStr38},
1795 {.rel_path = "stdlib/osh/byo-server.sh", .contents = gStr39},
1796 {.rel_path = "stdlib/osh/no-quotes.sh", .contents = gStr40},
1797 {.rel_path = "stdlib/osh/task-five.sh", .contents = gStr41},
1798 {.rel_path = "stdlib/osh/two.sh", .contents = gStr42},
1799 {.rel_path = "stdlib/prelude.ysh", .contents = gStr43},
1800 {.rel_path = "stdlib/stream.ysh", .contents = gStr44},
1801 {.rel_path = "stdlib/table.ysh", .contents = gStr45},
1802 {.rel_path = "stdlib/testing.ysh", .contents = gStr46},
1803
1804 {.rel_path = nullptr, .contents = nullptr},
1805};
1806
1807} // namespace embedded_file
1808
1809TextFile* gEmbeddedFiles = embedded_file::array; // turn array into pointer