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