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-replace ${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 | module guard against duplicate 'source'
|
308 | is-main false when sourcing a file
|
309 | use change first word lookup
|
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 | [Collections] X copy() X deepCopy()
|
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-while while (x > 0) { echo }
|
376 | ysh-for for i, item in (mylist) { 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
|
413 | ysh-slice a[1:-1] s[1:-1]
|
414 | func-call f(x, y; ...named)
|
415 | thin-arrow mylist->pop()
|
416 | fat-arrow mystr => startsWith('prefix')
|
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() X 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 | X [Guts] heapId()
|
523 | )zZXx");
|
524 |
|
525 | GLOBAL_STR(gStr30, R"zZXx(bin/ysh is the shell with data tYpes, influenced by pYthon, JavaScript, ...
|
526 |
|
527 | Usage: ysh FLAG* SCRIPT ARG*
|
528 | ysh FLAG* -c COMMAND ARG*
|
529 | ysh FLAG*
|
530 |
|
531 | `bin/ysh` is the same as `bin/osh` with a the `ysh:all` option group set. So
|
532 | `bin/ysh` also accepts shell flags.
|
533 |
|
534 | ysh -c 'echo hi'
|
535 | ysh myscript.ysh
|
536 | echo 'echo hi' | ysh
|
537 | )zZXx");
|
538 |
|
539 | GLOBAL_STR(gStr31, R"zZXx(
|
540 | Word Language <a class="group-link" href="chap-word-lang.html">word-lang</a>
|
541 |
|
542 |
|
543 | [Quotes] ysh-string "x is $x" $"x is $x" r'[a-z]\n'
|
544 | u'line\n' b'byte \yff'
|
545 | triple-quoted """ $""" r''' u''' b'''
|
546 | X tagged-str "<span id=$x>"html
|
547 | [Substitutions] expr-sub echo $[42 + a[i]]
|
548 | expr-splice echo @[split(x)]
|
549 | var-splice @myarray @ARGV
|
550 | command-sub @(split command)
|
551 | [Formatting] X ysh-printf ${x %.3f}
|
552 | X ysh-format ${x|html}
|
553 | )zZXx");
|
554 |
|
555 | GLOBAL_STR(gStr32, R"zZXx(
|
556 | YSH Command Language Keywords <a class="group-link" href="chap-ysh-cmd.html">ysh-cmd</a>
|
557 |
|
558 |
|
559 | [Assignment] const var Declare variables
|
560 | setvar setvar a[i] = 42
|
561 | setglobal setglobal d.key = 'foo'
|
562 | [Expression] equal = = 1 + 2*3
|
563 | call call mylist->append(42)
|
564 | [Definitions] proc proc p (s, ...rest) {
|
565 | typed proc p (; typed, ...rest; n=0; b) {
|
566 | func func f(x; opt1, opt2) { return (x + 1) }
|
567 | ysh-return return (myexpr)
|
568 | )zZXx");
|
569 |
|
570 | GLOBAL_STR(gStr33, R"zZXx(func identity(x) {
|
571 | ## The identity function. Returns its argument.
|
572 |
|
573 | return (x)
|
574 | }
|
575 | )zZXx");
|
576 |
|
577 | GLOBAL_STR(gStr34, R"zZXx(# Can we define methods in pure YSH?
|
578 | #
|
579 | # (mylist->find(42) !== -1)
|
580 | #
|
581 | # instead of
|
582 | #
|
583 | # ('42' in mylist)
|
584 | #
|
585 | # Because 'in' is for Dict
|
586 |
|
587 | func find (haystack List, needle) {
|
588 | for i, x in (haystack) {
|
589 | if (x === needle) {
|
590 | return (i)
|
591 | }
|
592 | }
|
593 | return (-1)
|
594 | }
|
595 | )zZXx");
|
596 |
|
597 | GLOBAL_STR(gStr35, R"zZXx(# Bash strict mode, updated for 2024
|
598 |
|
599 | set -o nounset
|
600 | set -o pipefail
|
601 | set -o errexit
|
602 | shopt -s inherit_errexit
|
603 | shopt -s strict:all 2>/dev/null || true # dogfood for OSH
|
604 |
|
605 | )zZXx");
|
606 |
|
607 | GLOBAL_STR(gStr36, R"zZXx(# Library to turn a shell file into a "BYO test server"
|
608 | #
|
609 | # Usage:
|
610 | #
|
611 | # # from both bash and OSH
|
612 | # if test -z "$LIB_OSH"; then LIB_OSH=stdlib/osh; fi
|
613 | # source $LIB_OSH/byo-server-lib.sh
|
614 | #
|
615 | # The client creates a clean process state and directory state for each tests.
|
616 | #
|
617 | # (This file requires compgen -A, and maybe declare -f, so it's not POSIX
|
618 | # shell.)
|
619 |
|
620 | : ${LIB_OSH:-stdlib/osh}
|
621 | source $LIB_OSH/two.sh
|
622 |
|
623 | byo-maybe-run() {
|
624 | local command=${BYO_COMMAND:-}
|
625 |
|
626 | case $command in
|
627 | '')
|
628 | # Do nothing if it's not specified
|
629 | return
|
630 | ;;
|
631 |
|
632 | detect)
|
633 | # all the commands supported, except 'detect'
|
634 | echo list-tests
|
635 | echo run-test
|
636 |
|
637 | exit 66 # ASCII code for 'B' - what the protocol specifies
|
638 | ;;
|
639 |
|
640 | list-tests)
|
641 | # bash extension that OSH also implements
|
642 | compgen -A function | grep '^test-'
|
643 | exit 0
|
644 | ;;
|
645 |
|
646 | run-test)
|
647 | local test_name=${BYO_ARG:-}
|
648 | if test -z "$test_name"; then
|
649 | die "BYO run-test: Expected BYO_ARG"
|
650 | fi
|
651 |
|
652 | # Shell convention: we name functions test-*
|
653 | $test_name
|
654 |
|
655 | # Only run if not set -e. Either way it's equivalent
|
656 | exit $?
|
657 | ;;
|
658 |
|
659 | *)
|
660 | die "Invalid BYO command '$command'"
|
661 | ;;
|
662 | esac
|
663 |
|
664 | # Do nothing if BYO_COMMAND is not set.
|
665 | # The program continues to its "main".
|
666 | }
|
667 |
|
668 | byo-must-run() {
|
669 | local command=${BYO_COMMAND:-}
|
670 | if test -z "$command"; then
|
671 | die "Expected BYO_COMMAND= in environment"
|
672 | fi
|
673 |
|
674 | byo-maybe-run
|
675 | }
|
676 | )zZXx");
|
677 |
|
678 | GLOBAL_STR(gStr37, R"zZXx(#!/usr/bin/env bash
|
679 | #
|
680 | # Testing library for bash and OSH.
|
681 | #
|
682 | # Capture status/stdout/stderr, and nq-assert those values.
|
683 |
|
684 | : ${LIB_OSH=stdlib/osh}
|
685 | source $LIB_OSH/two.sh
|
686 |
|
687 | nq-assert() {
|
688 | ### Assertion with same syntax as shell 'test'
|
689 |
|
690 | if ! test "$@"; then
|
691 | die "line ${BASH_LINENO[0]}: nq-assert $(printf '%q ' "$@") failed"
|
692 | fi
|
693 | }
|
694 |
|
695 | # Problem: we want to capture status and stdout at the same time
|
696 | #
|
697 | # We use:
|
698 | #
|
699 | # __stdout=$(set -o errexit; "$@")
|
700 | # __status=$?
|
701 | #
|
702 | # However, we lose the trailing \n, since that's how command subs work.
|
703 |
|
704 | # Here is another possibility:
|
705 | #
|
706 | # shopt -s lastpipe # need this too
|
707 | # ( set -o errexit; "$@" ) | read -r -d __stdout
|
708 | # __status=${PIPESTATUS[0]}
|
709 | # shopt -u lastpipe
|
710 | #
|
711 | # But this feels complex for just the \n issue, which can be easily worked
|
712 | # around.
|
713 |
|
714 | nq-run() {
|
715 | ### capture status only
|
716 |
|
717 | local -n out_status=$1
|
718 | shift
|
719 |
|
720 | local __status
|
721 |
|
722 | # Tricky: turn errexit off so we can capture it, but turn it on against
|
723 | set +o errexit
|
724 | ( set -o errexit; "$@" )
|
725 | __status=$?
|
726 | set -o errexit
|
727 |
|
728 | out_status=$__status
|
729 | }
|
730 |
|
731 | nq-capture() {
|
732 | ### capture status and stdout
|
733 |
|
734 | local -n out_status=$1
|
735 | local -n out_stdout=$2
|
736 | shift 2
|
737 |
|
738 | local __status
|
739 | local __stdout
|
740 |
|
741 | # Tricky: turn errexit off so we can capture it, but turn it on against
|
742 | set +o errexit
|
743 | __stdout=$(set -o errexit; "$@")
|
744 | __status=$?
|
745 | set -o errexit
|
746 |
|
747 | out_status=$__status
|
748 | out_stdout=$__stdout
|
749 | }
|
750 |
|
751 | nq-capture-2() {
|
752 | ### capture status and stderr
|
753 |
|
754 | # This is almost identical to the above
|
755 |
|
756 | local -n out_status=$1
|
757 | local -n out_stderr=$2
|
758 | shift 2
|
759 |
|
760 | local __status
|
761 | local __stderr
|
762 |
|
763 | # Tricky: turn errexit off so we can capture it, but turn it on against
|
764 | set +o errexit
|
765 | __stderr=$(set -o errexit; "$@" 2>&1)
|
766 | __status=$?
|
767 | set -o errexit
|
768 |
|
769 | out_status=$__status
|
770 | out_stderr=$__stderr
|
771 | }
|
772 |
|
773 | # 'byo test' can set this?
|
774 | : ${NQ_TEST_TEMP=/tmp}
|
775 |
|
776 | nq-redir() {
|
777 | ### capture status and stdout
|
778 |
|
779 | local -n out_status=$1
|
780 | local -n out_stdout_file=$2
|
781 | shift 2
|
782 |
|
783 | local __status
|
784 | local __stdout_file=$NQ_TEST_TEMP/nq-redir-$$.txt
|
785 |
|
786 | # Tricky: turn errexit off so we can capture it, but turn it on against
|
787 | set +o errexit
|
788 | ( set -o errexit; "$@" ) > $__stdout_file
|
789 | __status=$?
|
790 | set -o errexit
|
791 |
|
792 | out_status=$__status
|
793 | out_stdout_file=$__stdout_file
|
794 | }
|
795 |
|
796 | nq-redir-2() {
|
797 | ### capture status and stdout
|
798 |
|
799 | local -n out_status=$1
|
800 | local -n out_stderr_file=$2
|
801 | shift 2
|
802 |
|
803 | local __status
|
804 | local __stderr_file=$NQ_TEST_TEMP/nq-redir-$$.txt
|
805 |
|
806 | # Tricky: turn errexit off so we can capture it, but turn it on against
|
807 | set +o errexit
|
808 | ( set -o errexit; "$@" ) 2> $__stderr_file
|
809 | __status=$?
|
810 | set -o errexit
|
811 |
|
812 | out_status=$__status
|
813 | out_stderr_file=$__stderr_file
|
814 | }
|
815 | )zZXx");
|
816 |
|
817 | GLOBAL_STR(gStr38, R"zZXx(#!/usr/bin/env bash
|
818 | #
|
819 | # Common shell functions for task scripts.
|
820 | #
|
821 | # Usage:
|
822 | # source $LIB_OSH/task-five.sh
|
823 | #
|
824 | # test-foo() { # define task functions
|
825 | # echo foo
|
826 | # }
|
827 | # task-five "$@"
|
828 |
|
829 | # Definition of a "task"
|
830 | #
|
831 | # - File invokes task-five "$@"
|
832 | # - or maybe you can look at its source
|
833 | # - It's a shell function
|
834 | # - Has ### docstring
|
835 | # - Doesn't start with _
|
836 |
|
837 | : ${LIB_OSH=stdlib/osh}
|
838 | source $LIB_OSH/byo-server.sh
|
839 |
|
840 |
|
841 | # List all functions defined in this file (and not in sourced files).
|
842 | _bash-print-funcs() {
|
843 | ### Print shell functions in this file that don't start with _ (bash reflection)
|
844 |
|
845 | local funcs
|
846 | funcs=($(compgen -A function))
|
847 | # extdebug makes `declare -F` print the file path, but, annoyingly, only
|
848 | # if you pass the function names as arguments.
|
849 | shopt -s extdebug
|
850 | declare -F "${funcs[@]}" | grep --fixed-strings " $0" | awk '{print $1}'
|
851 | shopt -u extdebug
|
852 | }
|
853 |
|
854 | _gawk-print-funcs() {
|
855 | ### Print shell functions in this file that don't start with _ (awk parsing)
|
856 |
|
857 | # Using gawk because it has match()
|
858 | # - doesn't start with _
|
859 |
|
860 | # space = / ' '* /
|
861 | # shfunc = / %begin
|
862 | # <capture !['_' ' '] ![' ']*>
|
863 | # '()' space '{' space
|
864 | # %end /
|
865 | # docstring = / %begin
|
866 | # space '###' ' '+
|
867 | # <capture dot*>
|
868 | # %end /
|
869 | gawk '
|
870 | match($0, /^([^_ ][^ ]*)\(\)[ ]*{[ ]*$/, m) {
|
871 | #print NR " shfunc " m[1]
|
872 | print m[1]
|
873 | #print m[0]
|
874 | }
|
875 |
|
876 | match($0, /^[ ]*###[ ]+(.*)$/, m) {
|
877 | print NR " docstring " m[1]
|
878 | }
|
879 | ' $0
|
880 | }
|
881 |
|
882 | _print-funcs() {
|
883 | if command -v gawk > /dev/null; then
|
884 | _gawk-print-funcs
|
885 | else
|
886 | _bash-print-funcs
|
887 | fi
|
888 | }
|
889 |
|
890 | _show-help() {
|
891 | # TODO:
|
892 | # - Use awk to find comments at the top of the file?
|
893 | # - Use OSH to extract docstrings
|
894 | # - BYO_COMMAND=list-tasks will reuse that logic? It only applies to the
|
895 | # current file, not anything in a different file?
|
896 |
|
897 | echo "Usage: $0 TASK_NAME ARGS..."
|
898 | echo
|
899 | echo "To complete tasks, run:"
|
900 | echo " source devtools/completion.bash"
|
901 | echo
|
902 | echo "Tasks:"
|
903 |
|
904 | if command -v column >/dev/null; then
|
905 | _print-funcs | column
|
906 | else
|
907 | _print-funcs
|
908 | fi
|
909 | }
|
910 |
|
911 | task-five() {
|
912 | # Respond to BYO_COMMAND=list-tasks, etc. All task files need this.
|
913 | byo-maybe-run
|
914 |
|
915 | case ${1:-} in
|
916 | ''|--help|-h)
|
917 | _show-help
|
918 | exit 0
|
919 | ;;
|
920 | esac
|
921 |
|
922 | if ! declare -f "$1" >/dev/null; then
|
923 | echo "$0: '$1' isn't an action in this task file. Try '$0 --help'"
|
924 | exit 1
|
925 | fi
|
926 |
|
927 | "$@"
|
928 | }
|
929 | )zZXx");
|
930 |
|
931 | GLOBAL_STR(gStr39, R"zZXx(# Two functions I actually use, all the time.
|
932 | #
|
933 | # To keep depenedencies small, this library will NEVER grow other functions
|
934 | # (and is named to imply that.)
|
935 | #
|
936 | # Usage:
|
937 | # source --builtin two.sh
|
938 | #
|
939 | # Examples:
|
940 | # log 'hi'
|
941 | # die 'expected a number'
|
942 |
|
943 | if command -v source-guard >/dev/null; then # include guard for YSH
|
944 | source-guard two || return 0
|
945 | fi
|
946 |
|
947 | log() {
|
948 | ### Write a message to stderr.
|
949 | echo "$@" >&2
|
950 | }
|
951 |
|
952 | die() {
|
953 | ### Write an error message with the script name, and exit with status 1.
|
954 | log "$0: fatal: $@"
|
955 | exit 1
|
956 | }
|
957 |
|
958 | )zZXx");
|
959 |
|
960 | GLOBAL_STR(gStr40, R"zZXx(# These were helpful while implementing args.ysh
|
961 | # Maybe we will want to export them in a prelude so that others can use them too?
|
962 | #
|
963 | # Prior art: Rust has `todo!()` which is quite nice. Other languages allow
|
964 | # users to `raise NotImplmentedError()`.
|
965 |
|
966 | # Andy comments:
|
967 | # - 'pass' can be : or true in shell. It's a little obscure / confusing, but
|
968 | # there is an argument for minimalism. Although I prefer words like 'true',
|
969 | # and that already means something.
|
970 | # - UPDATE: we once took 'pass' as a keyword, but users complained because
|
971 | # there is a command 'pass'. So we probably can't have this by default.
|
972 | # Need to discuss source --builtin.
|
973 |
|
974 | # - todo could be more static? Rust presumably does it at compile time
|
975 |
|
976 | proc todo () {
|
977 | ## Raises a not implemented error when run.
|
978 | error ("TODO: not implemented") # TODO: is error code 1 ok?
|
979 | }
|
980 |
|
981 | proc pass () {
|
982 | ## Use when you want to temporarily leave a block empty.
|
983 | _ null
|
984 | }
|
985 | )zZXx");
|
986 |
|
987 | GLOBAL_STR(gStr41, R"zZXx(# testing.ysh
|
988 | #
|
989 | # Usage:
|
990 | # source --builtin testing.sh
|
991 | #
|
992 | # func f(x) { return (x + 1) }
|
993 | #
|
994 | # describe foo {
|
995 | # assert (43 === f(42))
|
996 | # }
|
997 | #
|
998 | # if is-main {
|
999 | # run-tests @ARGV # --filter
|
1000 | # }
|
1001 |
|
1002 | module stdlib/testing || return 0
|
1003 |
|
1004 | source --builtin args.ysh
|
1005 |
|
1006 | proc assert ( ; cond ; fail_message='default fail message') {
|
1007 | echo 'hi from assert'
|
1008 |
|
1009 | = cond
|
1010 |
|
1011 | # I think this might be ready now?
|
1012 |
|
1013 | var val = evalExpr(cond)
|
1014 |
|
1015 | echo
|
1016 | echo 'value'
|
1017 | = val
|
1018 | pp line (val)
|
1019 |
|
1020 | = fail_message
|
1021 |
|
1022 | if (val) {
|
1023 | echo 'OK'
|
1024 | } else {
|
1025 | var m = evalExpr(fail_message)
|
1026 | echo "FAIL - this is where we extract the string - $m"
|
1027 | }
|
1028 | }
|
1029 |
|
1030 | proc test-assert {
|
1031 | var x = 42
|
1032 | assert [42 === x]
|
1033 | }
|
1034 |
|
1035 | proc test-expr ( ; expr ) {
|
1036 | echo 'expr'
|
1037 | pp line (expr)
|
1038 | }
|
1039 |
|
1040 | proc test-named ( ; ; n=^[99] ) {
|
1041 | echo 'n'
|
1042 | pp line (n)
|
1043 | }
|
1044 |
|
1045 | # What happens when there are duplicate test IDs?
|
1046 | #
|
1047 | # Also I think filter by "$test_id/$case_id"
|
1048 |
|
1049 | proc __it (case_id ; ; ; block) {
|
1050 | # This uses a clean directory
|
1051 | echo TODO
|
1052 | }
|
1053 |
|
1054 | # is this accessible to users?
|
1055 | # It can contain a global list of things to run
|
1056 |
|
1057 | # Naming convention: a proc named 'describe' mutates a global named _describe?
|
1058 | # Or maybe _describe_list ?
|
1059 |
|
1060 | var _describe_list = []
|
1061 |
|
1062 | proc describe (test_id ; ; ; block) {
|
1063 | echo describe
|
1064 | #= desc
|
1065 |
|
1066 | # TODO:
|
1067 | # - need append
|
1068 | # - need ::
|
1069 | # _ _describe->append(cmd)
|
1070 | #
|
1071 | # Need to clean this up
|
1072 | # append (_describe, cmd) # does NOT work!
|
1073 |
|
1074 | call _describe_list->append(block)
|
1075 | }
|
1076 |
|
1077 | proc Args {
|
1078 | echo TODO
|
1079 | }
|
1080 |
|
1081 | # Problem: this creates a global variable?
|
1082 | Args (&spec) {
|
1083 | flag --filter 'Regex of test descriptions'
|
1084 | }
|
1085 |
|
1086 | proc run-tests {
|
1087 | var opt, i = parseArgs(spec, ARGV)
|
1088 |
|
1089 | # TODO:
|
1090 | # - parse --filter foo, which you can use eggex for!
|
1091 |
|
1092 | for cmd in (_describe) {
|
1093 | # TODO: print filename and 'describe' name?
|
1094 | try {
|
1095 | eval (cmd)
|
1096 | }
|
1097 | if (_status !== 0) {
|
1098 | echo 'failed'
|
1099 | }
|
1100 | }
|
1101 | }
|
1102 | )zZXx");
|
1103 |
|
1104 | GLOBAL_STR(gStr42, R"zZXx(# args.ysh
|
1105 | #
|
1106 | # Usage:
|
1107 | # source --builtin args.sh
|
1108 | #
|
1109 | # parser (&spec) {
|
1110 | # flag -v --verbose (help="Verbosely") # default is Bool, false
|
1111 | #
|
1112 | # flag -P --max-procs ('int', default=-1, doc='''
|
1113 | # Run at most P processes at a time
|
1114 | # ''')
|
1115 | #
|
1116 | # flag -i --invert ('bool', default=true, doc='''
|
1117 | # Long multiline
|
1118 | # Description
|
1119 | # ''')
|
1120 | #
|
1121 | # arg src (help='Source')
|
1122 | # arg dest (help='Dest')
|
1123 | # arg times (help='Foo')
|
1124 | #
|
1125 | # rest files
|
1126 | # }
|
1127 | #
|
1128 | # var args = parseArgs(spec, ARGV)
|
1129 | #
|
1130 | # echo "Verbose $[args.verbose]"
|
1131 |
|
1132 | # TODO: See list
|
1133 | # - It would be nice to keep `flag` and `arg` private, injecting them into the
|
1134 | # proc namespace only within `Args`
|
1135 | # - We need "type object" to replace the strings 'int', 'bool', etc.
|
1136 | # - flag builtin:
|
1137 | # - handle only long flag or only short flag
|
1138 | # - flag aliases
|
1139 |
|
1140 | proc parser (; place ; ; block_def) {
|
1141 | ## Create an args spec which can be passed to parseArgs.
|
1142 | ##
|
1143 | ## Example:
|
1144 | ##
|
1145 | ## # NOTE: &spec will create a variable named spec
|
1146 | ## parser (&spec) {
|
1147 | ## flag -v --verbose ('bool')
|
1148 | ## }
|
1149 | ##
|
1150 | ## var args = parseArgs(spec, ARGV)
|
1151 |
|
1152 | var p = {flags: [], args: []}
|
1153 | ctx push (p; ; block_def)
|
1154 |
|
1155 | # Validate that p.rest = [name] or null and reduce p.rest into name or null.
|
1156 | if ('rest' in p) {
|
1157 | if (len(p.rest) > 1) {
|
1158 | error '`rest` was called more than once' (code=3)
|
1159 | } else {
|
1160 | setvar p.rest = p.rest[0]
|
1161 | }
|
1162 | } else {
|
1163 | setvar p.rest = null
|
1164 | }
|
1165 |
|
1166 | var names = {}
|
1167 | for items in ([p.flags, p.args]) {
|
1168 | for x in (items) {
|
1169 | if (x.name in names) {
|
1170 | error "Duplicate flag/arg name $[x.name] in spec" (code=3)
|
1171 | }
|
1172 |
|
1173 | setvar names[x.name] = null
|
1174 | }
|
1175 | }
|
1176 |
|
1177 | # TODO: what about `flag --name` and then `arg name`?
|
1178 |
|
1179 | call place->setValue(p)
|
1180 | }
|
1181 |
|
1182 | proc flag (short, long ; type='bool' ; default=null, help=null) {
|
1183 | ## Declare a flag within an `arg-parse`.
|
1184 | ##
|
1185 | ## Examples:
|
1186 | ##
|
1187 | ## arg-parse (&spec) {
|
1188 | ## flag -v --verbose
|
1189 | ## flag -n --count ('int', default=1)
|
1190 | ## flag -f --file ('str', help="File to process")
|
1191 | ## }
|
1192 |
|
1193 | # bool has a default of false, not null
|
1194 | if (type === 'bool' and default === null) {
|
1195 | setvar default = false
|
1196 | }
|
1197 |
|
1198 | # TODO: validate `type`
|
1199 |
|
1200 | # TODO: Should use "trimPrefix"
|
1201 | var name = long[2:]
|
1202 |
|
1203 | ctx emit flags ({short, long, name, type, default, help})
|
1204 | }
|
1205 |
|
1206 | proc arg (name ; ; help=null) {
|
1207 | ## Declare a positional argument within an `arg-parse`.
|
1208 | ##
|
1209 | ## Examples:
|
1210 | ##
|
1211 | ## arg-parse (&spec) {
|
1212 | ## arg name
|
1213 | ## arg config (help="config file path")
|
1214 | ## }
|
1215 |
|
1216 | ctx emit args ({name, help})
|
1217 | }
|
1218 |
|
1219 | proc rest (name) {
|
1220 | ## Take the remaining positional arguments within an `arg-parse`.
|
1221 | ##
|
1222 | ## Examples:
|
1223 | ##
|
1224 | ## arg-parse (&grepSpec) {
|
1225 | ## arg query
|
1226 | ## rest files
|
1227 | ## }
|
1228 |
|
1229 | # We emit instead of set to detect multiple invocations of "rest"
|
1230 | ctx emit rest (name)
|
1231 | }
|
1232 |
|
1233 | func parseArgs(spec, argv) {
|
1234 | ## Given a spec created by `parser`. Parse an array of strings `argv` per
|
1235 | ## that spec.
|
1236 | ##
|
1237 | ## See `parser` for examples of use.
|
1238 |
|
1239 | var i = 0
|
1240 | var positionalPos = 0
|
1241 | var argc = len(argv)
|
1242 | var args = {}
|
1243 | var rest = []
|
1244 |
|
1245 | var value
|
1246 | var found
|
1247 | while (i < argc) {
|
1248 | var arg = argv[i]
|
1249 | if (arg->startsWith('-')) {
|
1250 | setvar found = false
|
1251 |
|
1252 | for flag in (spec.flags) {
|
1253 | if ( (flag.short and flag.short === arg) or
|
1254 | (flag.long and flag.long === arg) ) {
|
1255 | case (flag.type) {
|
1256 | ('bool') | (null) { setvar value = true }
|
1257 | int {
|
1258 | setvar i += 1
|
1259 | if (i >= len(argv)) {
|
1260 | error "Expected integer after '$arg'" (code=2)
|
1261 | }
|
1262 |
|
1263 | try { setvar value = int(argv[i]) }
|
1264 | if (_status !== 0) {
|
1265 | error "Expected integer after '$arg', got '$[argv[i]]'" (code=2)
|
1266 | }
|
1267 | }
|
1268 | }
|
1269 |
|
1270 | setvar args[flag.name] = value
|
1271 | setvar found = true
|
1272 | break
|
1273 | }
|
1274 | }
|
1275 |
|
1276 | if (not found) {
|
1277 | error "Unknown flag '$arg'" (code=2)
|
1278 | }
|
1279 | } elif (positionalPos >= len(spec.args)) {
|
1280 | if (not spec.rest) {
|
1281 | error "Too many arguments, unexpected '$arg'" (code=2)
|
1282 | }
|
1283 |
|
1284 | call rest->append(arg)
|
1285 | } else {
|
1286 | var pos = spec.args[positionalPos]
|
1287 | setvar positionalPos += 1
|
1288 | setvar value = arg
|
1289 | setvar args[pos.name] = value
|
1290 | }
|
1291 |
|
1292 | setvar i += 1
|
1293 | }
|
1294 |
|
1295 | if (spec.rest) {
|
1296 | setvar args[spec.rest] = rest
|
1297 | }
|
1298 |
|
1299 | # Set defaults for flags
|
1300 | for flag in (spec.flags) {
|
1301 | if (flag.name not in args) {
|
1302 | setvar args[flag.name] = flag.default
|
1303 | }
|
1304 | }
|
1305 |
|
1306 | # Raise error on missing args
|
1307 | for arg in (spec.args) {
|
1308 | if (arg.name not in args) {
|
1309 | error "Usage Error: Missing required argument $[arg.name]" (code=2)
|
1310 | }
|
1311 | }
|
1312 |
|
1313 | return (args)
|
1314 | }
|
1315 | )zZXx");
|
1316 |
|
1317 | GLOBAL_STR(gStr43, R"zZXx(func any(list) {
|
1318 | ### Returns true if any value in the list is truthy.
|
1319 | # Empty list: returns false
|
1320 |
|
1321 | for item in (list) {
|
1322 | if (item) {
|
1323 | return (true)
|
1324 | }
|
1325 | }
|
1326 | return (false)
|
1327 | }
|
1328 |
|
1329 | func all(list) {
|
1330 | ## Returns true if all values in the list are truthy.
|
1331 | # Empty list: returns true
|
1332 |
|
1333 | for item in (list) {
|
1334 | if (not item) {
|
1335 | return (false)
|
1336 | }
|
1337 | }
|
1338 | return (true)
|
1339 | }
|
1340 |
|
1341 | func sum(list; start=0) {
|
1342 | ### Returns the sum of all elements in the list.
|
1343 | # Empty list: returns 0
|
1344 |
|
1345 | var sum = start
|
1346 | for item in (list) {
|
1347 | setvar sum += item
|
1348 | }
|
1349 | return (sum)
|
1350 | }
|
1351 |
|
1352 | func repeat(x, n) {
|
1353 | ### Returns a list with the given string or list repeated
|
1354 |
|
1355 | # Like Python's 'foo'*3 or ['foo', 'bar']*3
|
1356 | # negative numbers are like 0 in Python
|
1357 |
|
1358 | var t = type(x)
|
1359 | case (t) {
|
1360 | Str {
|
1361 | var parts = []
|
1362 | for i in (0 .. n) {
|
1363 | call parts->append(x)
|
1364 | }
|
1365 | return (join(parts))
|
1366 | }
|
1367 | List {
|
1368 | var result = []
|
1369 | for i in (0 .. n) {
|
1370 | call result->extend(x)
|
1371 | }
|
1372 | return (result)
|
1373 | }
|
1374 | (else) {
|
1375 | error "Expected Str or List, got $t"
|
1376 | }
|
1377 | }
|
1378 | }
|
1379 | )zZXx");
|
1380 |
|
1381 | GLOBAL_STR(gStr44, R"zZXx(func __math_select(list, cmp) {
|
1382 | ## Internal helper for `max` and `min`.
|
1383 | ##
|
1384 | ## NOTE: If `list` is empty, then an error is thrown.
|
1385 |
|
1386 | if (len(list) === 0) {
|
1387 | error "Unexpected empty list" (code=3)
|
1388 | }
|
1389 |
|
1390 | if (len(list) === 1) {
|
1391 | return (list[0])
|
1392 | }
|
1393 |
|
1394 | var match = list[0]
|
1395 | for i in (1 .. len(list)) {
|
1396 | setvar match = cmp(list[i], match)
|
1397 | }
|
1398 | return (match)
|
1399 | }
|
1400 |
|
1401 | func max(...args) {
|
1402 | ## Compute the maximum of 2 or more values.
|
1403 | ##
|
1404 | ## `max` takes two different signatures:
|
1405 | ## - `max(a, b)` to return the maximum of `a`, `b`
|
1406 | ## - `max(list)` to return the greatest item in the `list`
|
1407 | ##
|
1408 | ## So, for example:
|
1409 | ##
|
1410 | ## max(1, 2) # => 2
|
1411 | ## max([1, 2, 3]) # => 3
|
1412 |
|
1413 | case (len(args)) {
|
1414 | (1) { return (__math_select(args[0], max)) }
|
1415 | (2) {
|
1416 | if (args[0] > args[1]) {
|
1417 | return (args[0])
|
1418 | } else {
|
1419 | return (args[1])
|
1420 | }
|
1421 | }
|
1422 | (else) { error "max expects 1 or 2 args" (code=3) }
|
1423 | }
|
1424 | }
|
1425 |
|
1426 | func min(...args) {
|
1427 | ## Compute the minimum of 2 or more values.
|
1428 | ##
|
1429 | ## `min` takes two different signatures:
|
1430 | ## - `min(a, b)` to return the minimum of `a`, `b`
|
1431 | ## - `min(list)` to return the least item in the `list`
|
1432 | ##
|
1433 | ## So, for example:
|
1434 | ##
|
1435 | ## min(2, 3) # => 2
|
1436 | ## max([1, 2, 3]) # => 1
|
1437 |
|
1438 | case (len(args)) {
|
1439 | (1) { return (__math_select(args[0], min)) }
|
1440 | (2) {
|
1441 | if (args[0] < args[1]) {
|
1442 | return (args[0])
|
1443 | } else {
|
1444 | return (args[1])
|
1445 | }
|
1446 | }
|
1447 | (else) { error "min expects 1 or 2 args" (code=3) }
|
1448 | }
|
1449 | }
|
1450 |
|
1451 | func abs(x) {
|
1452 | ## Compute the absolute (positive) value of a number (float or int).
|
1453 |
|
1454 | if (x < 0) {
|
1455 | return (-x)
|
1456 | } else {
|
1457 | return (x)
|
1458 | }
|
1459 | }
|
1460 | )zZXx");
|
1461 |
|
1462 | GLOBAL_STR(gStr45, R"zZXx(# stream.ysh
|
1463 | #
|
1464 | # Usage:
|
1465 | # source --builtin stream.ysh
|
1466 | #
|
1467 | # For reading lines, decoding, extracting, splitting
|
1468 |
|
1469 | # make this file a test server
|
1470 | source $LIB_OSH/byo-server.sh
|
1471 |
|
1472 | source $LIB_YSH/args.ysh
|
1473 |
|
1474 | proc slurp-by (; num_lines) {
|
1475 | # TODO: (stdin)
|
1476 | for line in (stdin) {
|
1477 | echo TODO
|
1478 | }
|
1479 | }
|
1480 |
|
1481 | # Note:
|
1482 | # - these are all the same algorithm
|
1483 | # - also word, block, etc. are all optional
|
1484 |
|
1485 | proc each-line (...words; template=null; ; block=null) {
|
1486 | # TODO:
|
1487 | # parse --j8 --max-jobs flag
|
1488 |
|
1489 | # parse template_str as string
|
1490 | # TODO: this is dangerous though ... because you can execute code
|
1491 | # I think you need a SAFE version
|
1492 |
|
1493 | # evaluate template string expression - I guess that allows $(echo hi) and so
|
1494 | # forth
|
1495 |
|
1496 | # evaluate block with _line binding
|
1497 | # block: execute in parallel with --max-jobs
|
1498 |
|
1499 | for line in (stdin) {
|
1500 | echo TODO
|
1501 | }
|
1502 | }
|
1503 |
|
1504 | proc test-each-line {
|
1505 | echo 'TODO: need basic test runner'
|
1506 |
|
1507 | # ysh-tool test stream.ysh
|
1508 | #
|
1509 | # Col
|
1510 |
|
1511 |
|
1512 | }
|
1513 |
|
1514 | proc each-row (; ; block) {
|
1515 | echo TODO
|
1516 | }
|
1517 |
|
1518 | proc split-by (; ifs=null; block) {
|
1519 | echo TODO
|
1520 | }
|
1521 |
|
1522 | proc if-split-by (; ifs=null; block) {
|
1523 | echo TODO
|
1524 | }
|
1525 |
|
1526 | proc chop () {
|
1527 | ### alias for if-split-by
|
1528 | echo TODO
|
1529 | }
|
1530 |
|
1531 | proc must-match (; pattern; block) {
|
1532 | echo TODO
|
1533 | }
|
1534 |
|
1535 | proc if-match (; pattern; block) {
|
1536 | echo TODO
|
1537 | }
|
1538 |
|
1539 | # Protocol:
|
1540 | #
|
1541 | # - The file lists its tests the "actions"
|
1542 | # - Then the test harness runs them
|
1543 | # - But should it be ENV vars
|
1544 | #
|
1545 | # - BYO_LIST_TESTS=1
|
1546 | # - BYO_RUN_TEST=foo
|
1547 | # - $PWD is a CLEAN temp dir, the process doesn't have to do anything
|
1548 |
|
1549 | # - silent on success, but prints file on output
|
1550 | # - OK this makes sense
|
1551 | #
|
1552 | # The trivial test in Python:
|
1553 | #
|
1554 | # from test import byo
|
1555 | # byo.maybe_main()
|
1556 | #
|
1557 | # bash library:
|
1558 | # source --builtin byo-server.sh
|
1559 | #
|
1560 | # byo-maybe-main # reads env variables, and then exits
|
1561 | #
|
1562 | # source --builtin assertions.ysh
|
1563 | #
|
1564 | # assert-ok 'echo hi'
|
1565 | # assert-stdout 'hi' 'echo -n hi'
|
1566 | #
|
1567 | # "$@"
|
1568 | #
|
1569 | # Run all tests
|
1570 | # util/byo-client.sh run-tests $YSH stdlib/table.ysh
|
1571 | # util/byo-client.sh run-tests -f x $YSH stdlib/table.ysh
|
1572 |
|
1573 | # Clean process
|
1574 | # Clean working dir
|
1575 |
|
1576 | #
|
1577 | # Stream Protocol:
|
1578 | # #.byo - is this she-dot, that's for a file
|
1579 | # Do we need metadata?
|
1580 | #
|
1581 |
|
1582 | # The harness
|
1583 | #
|
1584 | # It's process based testing.
|
1585 | #
|
1586 | # Test runner process: bash or OSH (unlike sharness!)
|
1587 | # Tested process: any language - bash,
|
1588 | #
|
1589 | # Key point: you don't have to quote shell code?
|
1590 |
|
1591 | list-byo-tests() {
|
1592 | echo TODO
|
1593 | }
|
1594 |
|
1595 | run-byo-tests() {
|
1596 | # source it
|
1597 | echo TODO
|
1598 | }
|
1599 |
|
1600 | byo-maybe-run
|
1601 | )zZXx");
|
1602 |
|
1603 | GLOBAL_STR(gStr46, R"zZXx(# table.ysh - Library for tables.
|
1604 | #
|
1605 | # Usage:
|
1606 | # source --builtin table.ysh
|
1607 |
|
1608 | # make this file a test server
|
1609 | source --builtin osh/byo-server.sh
|
1610 |
|
1611 | proc table (...words; place; ; block) {
|
1612 | var n = len(words)
|
1613 |
|
1614 | # TODO: parse flags
|
1615 | #
|
1616 | # --by-row
|
1617 | # --by-col
|
1618 | #
|
1619 | # Place is optional
|
1620 |
|
1621 | if (n === 0) {
|
1622 | echo TODO
|
1623 | return
|
1624 | }
|
1625 |
|
1626 | var action = words[0]
|
1627 |
|
1628 | # textual operations
|
1629 | case (action) {
|
1630 | cat {
|
1631 | echo todo
|
1632 | }
|
1633 | align {
|
1634 | echo todo
|
1635 | }
|
1636 | tabify {
|
1637 | echo todo
|
1638 | }
|
1639 | tabify {
|
1640 | echo todo
|
1641 | }
|
1642 | header {
|
1643 | echo todo
|
1644 | }
|
1645 | slice {
|
1646 | # this has typed args
|
1647 | # do we do some sort of splat?
|
1648 | echo todo
|
1649 | }
|
1650 | to-tsv {
|
1651 | echo todo
|
1652 | }
|
1653 | }
|
1654 |
|
1655 | echo TODO
|
1656 | }
|
1657 |
|
1658 | proc test-table {
|
1659 | return
|
1660 |
|
1661 | table (&files1) {
|
1662 | cols num_bytes path
|
1663 | type Int Str
|
1664 |
|
1665 | row 10 README.md
|
1666 | row 12 main.py
|
1667 |
|
1668 | row (12, 'lib.py')
|
1669 | row (num_bytes=12, path='util.py')
|
1670 | }
|
1671 |
|
1672 | # 2 columns - The default is by column?
|
1673 | assert ['Dict' === type(files1)]
|
1674 | assert [2 === len(files1)]
|
1675 |
|
1676 | # Same table
|
1677 | table --by-row (&files2) {
|
1678 | cols num_bytes path
|
1679 | type Int Str
|
1680 |
|
1681 | row 10 README.md
|
1682 | row 12 main.py
|
1683 |
|
1684 | row (12, 'lib.py')
|
1685 | row (num_bytes=12, path='util.py')
|
1686 | }
|
1687 |
|
1688 | # 4 rows
|
1689 | assert ['List' === type(files2)]
|
1690 | assert [4 === len(files2)]
|
1691 | }
|
1692 |
|
1693 | # "subcommands" of the dialect
|
1694 |
|
1695 | proc cols (...names) {
|
1696 | # cols name age
|
1697 | echo TODO
|
1698 | }
|
1699 |
|
1700 | proc types (...types) {
|
1701 | # types - Int? Str?
|
1702 | echo TODO
|
1703 | }
|
1704 |
|
1705 | proc attr (name; ...values) {
|
1706 | # attr units ('-', 'secs')
|
1707 | echo TODO
|
1708 | }
|
1709 |
|
1710 | # is this allowed outside table {} blocks too?
|
1711 | proc row {
|
1712 | echo TODO
|
1713 | }
|
1714 |
|
1715 | #
|
1716 | # dplyr names
|
1717 | #
|
1718 |
|
1719 | # TODO: can we parse select?
|
1720 |
|
1721 | proc where {
|
1722 | echo
|
1723 | }
|
1724 |
|
1725 | # TODO: should be able to test argv[0] or something
|
1726 | # Or pass to _mutate-transmute
|
1727 |
|
1728 | proc mutate {
|
1729 | echo TODO
|
1730 | }
|
1731 |
|
1732 | proc transmute {
|
1733 | echo TODO
|
1734 | }
|
1735 |
|
1736 | proc rename {
|
1737 | echo TODO
|
1738 | }
|
1739 |
|
1740 | proc group-by {
|
1741 | echo TODO
|
1742 | }
|
1743 |
|
1744 | proc sort-by {
|
1745 | echo TODO
|
1746 | }
|
1747 |
|
1748 | proc summary {
|
1749 | echo TODO
|
1750 | }
|
1751 |
|
1752 | byo-maybe-run
|
1753 | )zZXx");
|
1754 |
|
1755 | GLOBAL_STR(gStr47, R"zZXx(#!/usr/bin/env bash
|
1756 | #
|
1757 | # Testing library for bash and OSH.
|
1758 | #
|
1759 | # Capture status/stdout/stderr, and nq-assert those values.
|
1760 |
|
1761 | #module yblocks || return 0
|
1762 |
|
1763 | : ${LIB_OSH=stdlib/osh}
|
1764 | source $LIB_OSH/two.sh
|
1765 |
|
1766 | # There is no yb-run, because you can just use try { } and inspect _error.code
|
1767 | # There is no yb-redir, because you can just use try >$tmp { } and inspect _error.code
|
1768 |
|
1769 | proc yb-capture(; out; ; block) {
|
1770 | ### capture status and stderr
|
1771 |
|
1772 | var stdout = ''
|
1773 | try {
|
1774 | eval (block) | read --all (&stdout)
|
1775 | }
|
1776 | # TODO: if 'block' contains a pipeline, we lose this magic var
|
1777 | var result = {status: _pipeline_status[0], stdout}
|
1778 |
|
1779 | #echo 'result-1'
|
1780 | #pp test_ (result)
|
1781 |
|
1782 | call out->setValue(result)
|
1783 | }
|
1784 |
|
1785 | proc yb-capture-2(; out; ; block) {
|
1786 | ### capture status and stderr
|
1787 |
|
1788 | var stderr = ''
|
1789 | try {
|
1790 | eval (block) 2>&1 | read --all (&stderr)
|
1791 | }
|
1792 | #pp test_ (_pipeline_status)
|
1793 |
|
1794 | var result = {status: _pipeline_status[0], stderr}
|
1795 | #echo 'result-2'
|
1796 | #pp test_ (result)
|
1797 |
|
1798 | call out->setValue(result)
|
1799 | }
|
1800 | )zZXx");
|
1801 |
|
1802 |
|
1803 |
|
1804 | TextFile array[] = {
|
1805 | {.rel_path = "_devbuild/help/data-errors", .contents = gStr0},
|
1806 | {.rel_path = "_devbuild/help/data-front-end", .contents = gStr1},
|
1807 | {.rel_path = "_devbuild/help/data-j8-notation", .contents = gStr2},
|
1808 | {.rel_path = "_devbuild/help/help", .contents = gStr3},
|
1809 | {.rel_path = "_devbuild/help/oils-usage", .contents = gStr4},
|
1810 | {.rel_path = "_devbuild/help/osh-builtin-cmd", .contents = gStr5},
|
1811 | {.rel_path = "_devbuild/help/osh-chapters", .contents = gStr6},
|
1812 | {.rel_path = "_devbuild/help/osh-cmd-lang", .contents = gStr7},
|
1813 | {.rel_path = "_devbuild/help/osh-front-end", .contents = gStr8},
|
1814 | {.rel_path = "_devbuild/help/osh-mini-lang", .contents = gStr9},
|
1815 | {.rel_path = "_devbuild/help/osh-option", .contents = gStr10},
|
1816 | {.rel_path = "_devbuild/help/osh-osh-assign", .contents = gStr11},
|
1817 | {.rel_path = "_devbuild/help/osh-plugin", .contents = gStr12},
|
1818 | {.rel_path = "_devbuild/help/osh-special-var", .contents = gStr13},
|
1819 | {.rel_path = "_devbuild/help/osh-stdlib", .contents = gStr14},
|
1820 | {.rel_path = "_devbuild/help/osh-type-method", .contents = gStr15},
|
1821 | {.rel_path = "_devbuild/help/osh-usage", .contents = gStr16},
|
1822 | {.rel_path = "_devbuild/help/osh-word-lang", .contents = gStr17},
|
1823 | {.rel_path = "_devbuild/help/ysh-builtin-cmd", .contents = gStr18},
|
1824 | {.rel_path = "_devbuild/help/ysh-builtin-func", .contents = gStr19},
|
1825 | {.rel_path = "_devbuild/help/ysh-chapters", .contents = gStr20},
|
1826 | {.rel_path = "_devbuild/help/ysh-cmd-lang", .contents = gStr21},
|
1827 | {.rel_path = "_devbuild/help/ysh-expr-lang", .contents = gStr22},
|
1828 | {.rel_path = "_devbuild/help/ysh-front-end", .contents = gStr23},
|
1829 | {.rel_path = "_devbuild/help/ysh-mini-lang", .contents = gStr24},
|
1830 | {.rel_path = "_devbuild/help/ysh-option", .contents = gStr25},
|
1831 | {.rel_path = "_devbuild/help/ysh-plugin", .contents = gStr26},
|
1832 | {.rel_path = "_devbuild/help/ysh-special-var", .contents = gStr27},
|
1833 | {.rel_path = "_devbuild/help/ysh-stdlib", .contents = gStr28},
|
1834 | {.rel_path = "_devbuild/help/ysh-type-method", .contents = gStr29},
|
1835 | {.rel_path = "_devbuild/help/ysh-usage", .contents = gStr30},
|
1836 | {.rel_path = "_devbuild/help/ysh-word-lang", .contents = gStr31},
|
1837 | {.rel_path = "_devbuild/help/ysh-ysh-cmd", .contents = gStr32},
|
1838 | {.rel_path = "stdlib/funcs.ysh", .contents = gStr33},
|
1839 | {.rel_path = "stdlib/methods.ysh", .contents = gStr34},
|
1840 | {.rel_path = "stdlib/osh/bash-strict.sh", .contents = gStr35},
|
1841 | {.rel_path = "stdlib/osh/byo-server.sh", .contents = gStr36},
|
1842 | {.rel_path = "stdlib/osh/no-quotes.sh", .contents = gStr37},
|
1843 | {.rel_path = "stdlib/osh/task-five.sh", .contents = gStr38},
|
1844 | {.rel_path = "stdlib/osh/two.sh", .contents = gStr39},
|
1845 | {.rel_path = "stdlib/prelude.ysh", .contents = gStr40},
|
1846 | {.rel_path = "stdlib/testing.ysh", .contents = gStr41},
|
1847 | {.rel_path = "stdlib/ysh/args.ysh", .contents = gStr42},
|
1848 | {.rel_path = "stdlib/ysh/list.ysh", .contents = gStr43},
|
1849 | {.rel_path = "stdlib/ysh/math.ysh", .contents = gStr44},
|
1850 | {.rel_path = "stdlib/ysh/stream.ysh", .contents = gStr45},
|
1851 | {.rel_path = "stdlib/ysh/table.ysh", .contents = gStr46},
|
1852 | {.rel_path = "stdlib/ysh/yblocks.ysh", .contents = gStr47},
|
1853 |
|
1854 | {.rel_path = nullptr, .contents = nullptr},
|
1855 | };
|
1856 |
|
1857 | } // namespace embedded_file
|
1858 |
|
1859 | TextFile* gEmbeddedFiles = embedded_file::array; // turn array into pointer
|