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