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