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

1803 lines, 148 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_awk-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 awk '
1233 match($0, /^([^_ ][^ ]*)\(\)[ ]*{[ ]*$/, m) {
1234 print NR " shfunc " m[1]
1235 #print m[0]
1236 }
1237
1238 match($0, /^[ ]*###[ ]+(.*)$/, m) {
1239 print NR " docstring " m[1]
1240 }
1241' $0
1242}
1243
1244_show-help() {
1245 # TODO:
1246 # - Use awk to find comments at the top of the file?
1247 # - Use OSH to extract docstrings
1248 # - BYO_COMMAND=list-tasks will reuse that logic? It only applies to the
1249 # current file, not anything in a different file?
1250
1251 echo "Usage: $0 TASK_NAME ARGS..."
1252 echo
1253 echo "To complete tasks, run:"
1254 echo " source devtools/completion.bash"
1255 echo
1256 echo "Tasks:"
1257
1258 if command -v column >/dev/null; then
1259 _bash-print-funcs | column
1260 else
1261 _bash-print-funcs
1262 fi
1263}
1264
1265task-five() {
1266 # Respond to BYO_COMMAND=list-tasks, etc. All task files need this.
1267 byo-maybe-run
1268
1269 case ${1:-} in
1270 ''|--help|-h)
1271 _show-help
1272 exit 0
1273 ;;
1274 esac
1275
1276 if ! declare -f "$1" >/dev/null; then
1277 echo "$0: '$1' isn't an action in this task file. Try '$0 --help'"
1278 exit 1
1279 fi
1280
1281 "$@"
1282}
1283)zZXx");
1284
1285GLOBAL_STR(gStr42, R"zZXx(# Two functions I actually use, all the time.
1286#
1287# To keep depenedencies small, this library will NEVER grow other functions
1288# (and is named to imply that.)
1289#
1290# Usage:
1291# source --builtin two.sh
1292#
1293# Examples:
1294# log 'hi'
1295# die 'expected a number'
1296
1297log() {
1298 ### Write a message to stderr.
1299 echo "$@" >&2
1300}
1301
1302die() {
1303 ### Write an error message with the script name, and exit with status 1.
1304 log "$0: fatal: $@"
1305 exit 1
1306}
1307
1308)zZXx");
1309
1310GLOBAL_STR(gStr43, R"zZXx(# These were helpful while implementing args.ysh
1311# Maybe we will want to export them in a prelude so that others can use them too?
1312#
1313# Prior art: Rust has `todo!()` which is quite nice. Other languages allow
1314# users to `raise NotImplmentedError()`.
1315
1316# Andy comments:
1317# - 'pass' can be : or true in shell. It's a little obscure / confusing, but
1318# there is an argument for minimalism. Although I prefer words like 'true',
1319# and that already means something.
1320# - UPDATE: we once took 'pass' as a keyword, but users complained because
1321# there is a command 'pass'. So we probably can't have this by default.
1322# Need to discuss source --builtin.
1323
1324# - todo could be more static? Rust presumably does it at compile time
1325
1326proc todo () {
1327 ## Raises a not implemented error when run.
1328 error ("TODO: not implemented") # TODO: is error code 1 ok?
1329}
1330
1331proc pass () {
1332 ## Use when you want to temporarily leave a block empty.
1333 _ null
1334}
1335)zZXx");
1336
1337GLOBAL_STR(gStr44, R"zZXx(# stream.ysh
1338#
1339# Usage:
1340# source --builtin stream.ysh
1341#
1342# For reading lines, decoding, extracting, splitting
1343
1344# make this file a test server
1345source --builtin osh/byo-server.sh
1346
1347source --builtin args.ysh
1348
1349proc slurp-by (; num_lines) {
1350 # TODO: (stdin)
1351 for line in <> {
1352 echo TODO
1353 }
1354}
1355
1356# Note:
1357# - these are all the same algorithm
1358# - also word, block, etc. are all optional
1359
1360proc each-line (...words; template=null; ; block=null) {
1361 # TODO:
1362 # parse --j8 --max-jobs flag
1363
1364 # parse template_str as string
1365 # TODO: this is dangerous though ... because you can execute code
1366 # I think you need a SAFE version
1367
1368 # evaluate template string expression - I guess that allows $(echo hi) and so
1369 # forth
1370
1371 # evaluate block with _line binding
1372 # block: execute in parallel with --max-jobs
1373
1374 for line in <> {
1375 echo TODO
1376 }
1377}
1378
1379proc test-each-line {
1380 echo 'TODO: need basic test runner'
1381
1382 # ysh-tool test stream.ysh
1383 #
1384 # Col
1385
1386
1387}
1388
1389proc each-row (; ; block) {
1390 echo TODO
1391}
1392
1393proc split-by (; ifs=null; block) {
1394 echo TODO
1395}
1396
1397proc if-split-by (; ifs=null; block) {
1398 echo TODO
1399}
1400
1401proc chop () {
1402 ### alias for if-split-by
1403 echo TODO
1404}
1405
1406proc must-match (; pattern; block) {
1407 echo TODO
1408}
1409
1410proc if-match (; pattern; block) {
1411 echo TODO
1412}
1413
1414# Protocol:
1415#
1416# - The file lists its tests the "actions"
1417# - Then the test harness runs them
1418# - But should it be ENV vars
1419#
1420# - BYO_LIST_TESTS=1
1421# - BYO_RUN_TEST=foo
1422# - $PWD is a CLEAN temp dir, the process doesn't have to do anything
1423
1424# - silent on success, but prints file on output
1425# - OK this makes sense
1426#
1427# The trivial test in Python:
1428#
1429# from test import byo
1430# byo.maybe_main()
1431#
1432# bash library:
1433# source --builtin byo-server.sh
1434#
1435# byo-maybe-main # reads env variables, and then exits
1436#
1437# source --builtin assertions.ysh
1438#
1439# assert-ok 'echo hi'
1440# assert-stdout 'hi' 'echo -n hi'
1441#
1442# "$@"
1443#
1444# Run all tests
1445# util/byo-client.sh run-tests $YSH stdlib/table.ysh
1446# util/byo-client.sh run-tests -f x $YSH stdlib/table.ysh
1447
1448# Clean process
1449# Clean working dir
1450
1451#
1452# Stream Protocol:
1453# #.byo - is this she-dot, that's for a file
1454# Do we need metadata?
1455#
1456
1457# The harness
1458#
1459# It's process based testing.
1460#
1461# Test runner process: bash or OSH (unlike sharness!)
1462# Tested process: any language - bash,
1463#
1464# Key point: you don't have to quote shell code?
1465
1466list-byo-tests() {
1467 echo TODO
1468}
1469
1470run-byo-tests() {
1471 # source it
1472 echo TODO
1473}
1474
1475byo-maybe-run
1476)zZXx");
1477
1478GLOBAL_STR(gStr45, R"zZXx(# table.ysh - Library for tables.
1479#
1480# Usage:
1481# source --builtin table.ysh
1482
1483# make this file a test server
1484source --builtin osh/byo-server.sh
1485
1486proc table (...words; place; ; block) {
1487 var n = len(words)
1488
1489 # TODO: parse flags
1490 #
1491 # --by-row
1492 # --by-col
1493 #
1494 # Place is optional
1495
1496 if (n === 0) {
1497 echo TODO
1498 return
1499 }
1500
1501 var action = words[0]
1502
1503 # textual operations
1504 case (action) {
1505 cat {
1506 echo todo
1507 }
1508 align {
1509 echo todo
1510 }
1511 tabify {
1512 echo todo
1513 }
1514 tabify {
1515 echo todo
1516 }
1517 header {
1518 echo todo
1519 }
1520 slice {
1521 # this has typed args
1522 # do we do some sort of splat?
1523 echo todo
1524 }
1525 to-tsv {
1526 echo todo
1527 }
1528 }
1529
1530 echo TODO
1531}
1532
1533proc test-table {
1534 return
1535
1536 table (&files1) {
1537 cols num_bytes path
1538 type Int Str
1539
1540 row 10 README.md
1541 row 12 main.py
1542
1543 row (12, 'lib.py')
1544 row (num_bytes=12, path='util.py')
1545 }
1546
1547 # 2 columns - The default is by column?
1548 assert ['Dict' === type(files1)]
1549 assert [2 === len(files1)]
1550
1551 # Same table
1552 table --by-row (&files2) {
1553 cols num_bytes path
1554 type Int Str
1555
1556 row 10 README.md
1557 row 12 main.py
1558
1559 row (12, 'lib.py')
1560 row (num_bytes=12, path='util.py')
1561 }
1562
1563 # 4 rows
1564 assert ['List' === type(files2)]
1565 assert [4 === len(files2)]
1566}
1567
1568# "subcommands" of the dialect
1569
1570proc cols (...names) {
1571 # cols name age
1572 echo TODO
1573}
1574
1575proc types (...types) {
1576 # types - Int? Str?
1577 echo TODO
1578}
1579
1580proc attr (name; ...values) {
1581 # attr units ('-', 'secs')
1582 echo TODO
1583}
1584
1585# is this allowed outside table {} blocks too?
1586proc row {
1587 echo TODO
1588}
1589
1590#
1591# dplyr names
1592#
1593
1594# TODO: can we parse select?
1595
1596proc where {
1597 echo
1598}
1599
1600# TODO: should be able to test argv[0] or something
1601# Or pass to _mutate-transmute
1602
1603proc mutate {
1604 echo TODO
1605}
1606
1607proc transmute {
1608 echo TODO
1609}
1610
1611proc rename {
1612 echo TODO
1613}
1614
1615proc group-by {
1616 echo TODO
1617}
1618
1619proc sort-by {
1620 echo TODO
1621}
1622
1623proc summary {
1624 echo TODO
1625}
1626
1627byo-maybe-run
1628)zZXx");
1629
1630GLOBAL_STR(gStr46, R"zZXx(# testing.ysh
1631#
1632# Usage:
1633# source --builtin testing.sh
1634#
1635# func f(x) { return (x + 1) }
1636#
1637# describe foo {
1638# assert (43 === f(42))
1639# }
1640#
1641# if is-main {
1642# run-tests @ARGV # --filter
1643# }
1644
1645module stdlib/testing || return 0
1646
1647source --builtin args.ysh
1648
1649proc assert ( ; cond ; fail_message='default fail message') {
1650 echo 'hi from assert'
1651
1652 = cond
1653
1654 # I think this might be ready now?
1655
1656 var val = evalExpr(cond)
1657
1658 echo
1659 echo 'value'
1660 = val
1661 pp line (val)
1662
1663 = fail_message
1664
1665 if (val) {
1666 echo 'OK'
1667 } else {
1668 var m = evalExpr(fail_message)
1669 echo "FAIL - this is where we extract the string - $m"
1670 }
1671}
1672
1673proc test-assert {
1674 var x = 42
1675 assert [42 === x]
1676}
1677
1678proc test-expr ( ; expr ) {
1679 echo 'expr'
1680 pp line (expr)
1681}
1682
1683proc test-named ( ; ; n=^[99] ) {
1684 echo 'n'
1685 pp line (n)
1686}
1687
1688# What happens when there are duplicate test IDs?
1689#
1690# Also I think filter by "$test_id/$case_id"
1691
1692proc __it (case_id ; ; ; block) {
1693 # This uses a clean directory
1694 echo TODO
1695}
1696
1697# is this accessible to users?
1698# It can contain a global list of things to run
1699
1700# Naming convention: a proc named 'describe' mutates a global named _describe?
1701# Or maybe _describe_list ?
1702
1703var _describe_list = []
1704
1705proc describe (test_id ; ; ; block) {
1706 echo describe
1707 #= desc
1708
1709 # TODO:
1710 # - need append
1711 # - need ::
1712 # _ _describe->append(cmd)
1713 #
1714 # Need to clean this up
1715 # append (_describe, cmd) # does NOT work!
1716
1717 call _describe_list->append(block)
1718}
1719
1720proc Args {
1721 echo TODO
1722}
1723
1724# Problem: this creates a global variable?
1725Args (&spec) {
1726 flag --filter 'Regex of test descriptions'
1727}
1728
1729proc run-tests {
1730 var opt, i = parseArgs(spec, ARGV)
1731
1732 # TODO:
1733 # - parse --filter foo, which you can use eggex for!
1734
1735 for cmd in (_describe) {
1736 # TODO: print filename and 'describe' name?
1737 try {
1738 eval (cmd)
1739 }
1740 if (_status !== 0) {
1741 echo 'failed'
1742 }
1743 }
1744}
1745)zZXx");
1746
1747
1748
1749TextFile array[] = {
1750 {.rel_path = "_devbuild/help/data-errors", .contents = gStr0},
1751 {.rel_path = "_devbuild/help/data-front-end", .contents = gStr1},
1752 {.rel_path = "_devbuild/help/data-j8-notation", .contents = gStr2},
1753 {.rel_path = "_devbuild/help/help", .contents = gStr3},
1754 {.rel_path = "_devbuild/help/oils-usage", .contents = gStr4},
1755 {.rel_path = "_devbuild/help/osh-builtin-cmd", .contents = gStr5},
1756 {.rel_path = "_devbuild/help/osh-chapters", .contents = gStr6},
1757 {.rel_path = "_devbuild/help/osh-cmd-lang", .contents = gStr7},
1758 {.rel_path = "_devbuild/help/osh-front-end", .contents = gStr8},
1759 {.rel_path = "_devbuild/help/osh-mini-lang", .contents = gStr9},
1760 {.rel_path = "_devbuild/help/osh-option", .contents = gStr10},
1761 {.rel_path = "_devbuild/help/osh-osh-assign", .contents = gStr11},
1762 {.rel_path = "_devbuild/help/osh-plugin", .contents = gStr12},
1763 {.rel_path = "_devbuild/help/osh-special-var", .contents = gStr13},
1764 {.rel_path = "_devbuild/help/osh-stdlib", .contents = gStr14},
1765 {.rel_path = "_devbuild/help/osh-type-method", .contents = gStr15},
1766 {.rel_path = "_devbuild/help/osh-usage", .contents = gStr16},
1767 {.rel_path = "_devbuild/help/osh-word-lang", .contents = gStr17},
1768 {.rel_path = "_devbuild/help/ysh-builtin-cmd", .contents = gStr18},
1769 {.rel_path = "_devbuild/help/ysh-builtin-func", .contents = gStr19},
1770 {.rel_path = "_devbuild/help/ysh-chapters", .contents = gStr20},
1771 {.rel_path = "_devbuild/help/ysh-cmd-lang", .contents = gStr21},
1772 {.rel_path = "_devbuild/help/ysh-expr-lang", .contents = gStr22},
1773 {.rel_path = "_devbuild/help/ysh-front-end", .contents = gStr23},
1774 {.rel_path = "_devbuild/help/ysh-mini-lang", .contents = gStr24},
1775 {.rel_path = "_devbuild/help/ysh-option", .contents = gStr25},
1776 {.rel_path = "_devbuild/help/ysh-plugin", .contents = gStr26},
1777 {.rel_path = "_devbuild/help/ysh-special-var", .contents = gStr27},
1778 {.rel_path = "_devbuild/help/ysh-stdlib", .contents = gStr28},
1779 {.rel_path = "_devbuild/help/ysh-type-method", .contents = gStr29},
1780 {.rel_path = "_devbuild/help/ysh-usage", .contents = gStr30},
1781 {.rel_path = "_devbuild/help/ysh-word-lang", .contents = gStr31},
1782 {.rel_path = "_devbuild/help/ysh-ysh-cmd", .contents = gStr32},
1783 {.rel_path = "stdlib/args.ysh", .contents = gStr33},
1784 {.rel_path = "stdlib/funcs.ysh", .contents = gStr34},
1785 {.rel_path = "stdlib/list.ysh", .contents = gStr35},
1786 {.rel_path = "stdlib/math.ysh", .contents = gStr36},
1787 {.rel_path = "stdlib/methods.ysh", .contents = gStr37},
1788 {.rel_path = "stdlib/osh/bash-strict.sh", .contents = gStr38},
1789 {.rel_path = "stdlib/osh/byo-server.sh", .contents = gStr39},
1790 {.rel_path = "stdlib/osh/no-quotes.sh", .contents = gStr40},
1791 {.rel_path = "stdlib/osh/task-five.sh", .contents = gStr41},
1792 {.rel_path = "stdlib/osh/two.sh", .contents = gStr42},
1793 {.rel_path = "stdlib/prelude.ysh", .contents = gStr43},
1794 {.rel_path = "stdlib/stream.ysh", .contents = gStr44},
1795 {.rel_path = "stdlib/table.ysh", .contents = gStr45},
1796 {.rel_path = "stdlib/testing.ysh", .contents = gStr46},
1797
1798 {.rel_path = nullptr, .contents = nullptr},
1799};
1800
1801} // namespace embedded_file
1802
1803TextFile* gEmbeddedFiles = embedded_file::array; // turn array into pointer