1 | #!/usr/bin/env python2
|
2 | """Flag_def.py."""
|
3 | from __future__ import print_function
|
4 |
|
5 | from frontend import args
|
6 | from frontend.flag_spec import (FlagSpec, FlagSpecAndMore, _FlagSpecAndMore)
|
7 | from frontend import option_def
|
8 |
|
9 | #
|
10 | # Definitions for builtin_assign
|
11 | #
|
12 |
|
13 | EXPORT_SPEC = FlagSpec('export_')
|
14 | EXPORT_SPEC.ShortFlag('-n')
|
15 | EXPORT_SPEC.ShortFlag('-f') # stubbed
|
16 | EXPORT_SPEC.ShortFlag('-p')
|
17 |
|
18 | READONLY_SPEC = FlagSpec('readonly')
|
19 |
|
20 | # TODO: Check the consistency of -a and -A against values, here and below.
|
21 | READONLY_SPEC.ShortFlag('-a')
|
22 | READONLY_SPEC.ShortFlag('-A')
|
23 | READONLY_SPEC.ShortFlag('-p')
|
24 |
|
25 | NEW_VAR_SPEC = FlagSpec('new_var')
|
26 |
|
27 | # print stuff
|
28 | NEW_VAR_SPEC.ShortFlag('-f')
|
29 | NEW_VAR_SPEC.ShortFlag('-F')
|
30 | NEW_VAR_SPEC.ShortFlag('-p')
|
31 |
|
32 | NEW_VAR_SPEC.ShortFlag('-g') # Look up in global scope
|
33 |
|
34 | # Options +r +x +n
|
35 | NEW_VAR_SPEC.PlusFlag('x') # export
|
36 | NEW_VAR_SPEC.PlusFlag('r') # readonly
|
37 | NEW_VAR_SPEC.PlusFlag('n') # named ref
|
38 |
|
39 | # Common between readonly/declare
|
40 | NEW_VAR_SPEC.ShortFlag('-a')
|
41 | NEW_VAR_SPEC.ShortFlag('-A')
|
42 | NEW_VAR_SPEC.ShortFlag('-i') # no-op for integers
|
43 |
|
44 | UNSET_SPEC = FlagSpec('unset')
|
45 | UNSET_SPEC.ShortFlag('-v')
|
46 | UNSET_SPEC.ShortFlag('-f')
|
47 | #UNSET_SPEC.ShortFlag('-z', args.String)
|
48 |
|
49 | #
|
50 | # Definitions for builtin_meta
|
51 | #
|
52 |
|
53 | # Unused because there are no flags! Just --.
|
54 | EVAL_SPEC = FlagSpec('eval')
|
55 | SOURCE_SPEC = FlagSpec('source')
|
56 | SOURCE_SPEC.LongFlag('--builtin')
|
57 |
|
58 | COMMAND_SPEC = FlagSpec('command')
|
59 | COMMAND_SPEC.ShortFlag('-v')
|
60 | COMMAND_SPEC.ShortFlag('-V')
|
61 | COMMAND_SPEC.ShortFlag('-p')
|
62 |
|
63 | TYPE_SPEC = FlagSpec('type')
|
64 | TYPE_SPEC.ShortFlag('-f')
|
65 | TYPE_SPEC.ShortFlag('-t')
|
66 | TYPE_SPEC.ShortFlag('-p')
|
67 | TYPE_SPEC.ShortFlag('-P')
|
68 | TYPE_SPEC.ShortFlag('-a')
|
69 |
|
70 | #
|
71 | # Definitions for builtin_pure
|
72 | #
|
73 |
|
74 | ALIAS_SPEC = FlagSpec('alias') # no flags yet
|
75 | UNALIAS_SPEC = FlagSpec('unalias') # no flags yet
|
76 |
|
77 | SHOPT_SPEC = FlagSpec('shopt')
|
78 | SHOPT_SPEC.ShortFlag('-s', long_name='--set')
|
79 | SHOPT_SPEC.ShortFlag('-u', long_name='--unset')
|
80 | SHOPT_SPEC.ShortFlag('-o') # use 'set -o' names
|
81 | # TODO: --print could print in a verbose format. (Annoying: codegen conflicts
|
82 | # with Python keyword.)
|
83 | SHOPT_SPEC.ShortFlag('-p')
|
84 | SHOPT_SPEC.ShortFlag('-q') # query option settings
|
85 |
|
86 | HASH_SPEC = FlagSpec('hash')
|
87 | HASH_SPEC.ShortFlag('-r')
|
88 |
|
89 | ECHO_SPEC = FlagSpec('echo')
|
90 | ECHO_SPEC.ShortFlag('-e') # no backslash escapes
|
91 | ECHO_SPEC.ShortFlag('-n')
|
92 |
|
93 | #
|
94 | # osh/builtin_printf.py
|
95 | #
|
96 |
|
97 | PRINTF_SPEC = FlagSpec('printf')
|
98 | PRINTF_SPEC.ShortFlag('-v', args.String)
|
99 |
|
100 | #
|
101 | # osh/builtin_misc.py
|
102 | #
|
103 |
|
104 | READ_SPEC = FlagSpec('read')
|
105 | READ_SPEC.ShortFlag('-r')
|
106 | READ_SPEC.ShortFlag('-s') # silent
|
107 | READ_SPEC.ShortFlag('-u', args.Int) # file descriptor
|
108 | READ_SPEC.ShortFlag('-t', args.Float) # timeout
|
109 | READ_SPEC.ShortFlag('-n', args.Int)
|
110 | READ_SPEC.ShortFlag('-N', args.Int)
|
111 | READ_SPEC.ShortFlag('-a', args.String) # name of array to read into
|
112 | READ_SPEC.ShortFlag('-d', args.String)
|
113 | READ_SPEC.ShortFlag('-p', args.String) # prompt
|
114 |
|
115 | # YSH extensions
|
116 | READ_SPEC.ShortFlag('-0') # until NUL, like -r -d ''
|
117 | READ_SPEC.LongFlag('--all')
|
118 | READ_SPEC.LongFlag('--line')
|
119 | READ_SPEC.LongFlag('--num-bytes', args.Int)
|
120 | # don't strip the trailing newline
|
121 | READ_SPEC.LongFlag('--with-eol')
|
122 | READ_SPEC.LongFlag('--json',
|
123 | args.Bool,
|
124 | default=False,
|
125 | help='Read elements as JSON strings')
|
126 | READ_SPEC.LongFlag('--j8',
|
127 | args.Bool,
|
128 | default=False,
|
129 | help='Read elements as J8 strings')
|
130 |
|
131 | MAPFILE_SPEC = FlagSpec('mapfile')
|
132 | MAPFILE_SPEC.ShortFlag('-t')
|
133 |
|
134 | CD_SPEC = FlagSpec('cd')
|
135 | CD_SPEC.ShortFlag('-L')
|
136 | CD_SPEC.ShortFlag('-P')
|
137 |
|
138 | PUSHD_SPEC = FlagSpec('pushd')
|
139 |
|
140 | POPD_SPEC = FlagSpec('popd')
|
141 |
|
142 | DIRS_SPEC = FlagSpec('dirs')
|
143 | DIRS_SPEC.ShortFlag('-c')
|
144 | DIRS_SPEC.ShortFlag('-l')
|
145 | DIRS_SPEC.ShortFlag('-p')
|
146 | DIRS_SPEC.ShortFlag('-v')
|
147 |
|
148 | PWD_SPEC = FlagSpec('pwd')
|
149 | PWD_SPEC.ShortFlag('-L')
|
150 | PWD_SPEC.ShortFlag('-P')
|
151 |
|
152 | HELP_SPEC = FlagSpec('help')
|
153 | #HELP_SPEC.ShortFlag('-i') # show index
|
154 | # Note: bash has help -d -m -s, which change the formatting
|
155 |
|
156 | HISTORY_SPEC = FlagSpec('history')
|
157 | HISTORY_SPEC.ShortFlag('-a')
|
158 | HISTORY_SPEC.ShortFlag('-r')
|
159 | HISTORY_SPEC.ShortFlag('-c')
|
160 | HISTORY_SPEC.ShortFlag('-d', args.Int)
|
161 |
|
162 | #
|
163 | # osh/builtin_process.py
|
164 | #
|
165 |
|
166 | EXEC_SPEC = FlagSpec('exec')
|
167 |
|
168 | WAIT_SPEC = FlagSpec('wait')
|
169 | WAIT_SPEC.ShortFlag('-n')
|
170 |
|
171 | TRAP_SPEC = FlagSpec('trap')
|
172 | TRAP_SPEC.ShortFlag('-p')
|
173 | TRAP_SPEC.ShortFlag('-l')
|
174 |
|
175 | JOB_SPEC = FlagSpec('jobs')
|
176 | JOB_SPEC.ShortFlag('-l', help='long format')
|
177 | JOB_SPEC.ShortFlag('-p', help='prints PID only')
|
178 | JOB_SPEC.LongFlag('--debug', help='display debug info')
|
179 |
|
180 | #
|
181 | # FlagSpecAndMore
|
182 | #
|
183 |
|
184 | #
|
185 | # set and shopt
|
186 | #
|
187 |
|
188 |
|
189 | def _AddShellOptions(spec):
|
190 | # type: (_FlagSpecAndMore) -> None
|
191 | """Shared between 'set' builtin and the shell's own arg parser."""
|
192 | spec.InitOptions()
|
193 | spec.InitShopt()
|
194 |
|
195 | for opt in option_def.All():
|
196 | if opt.builtin == 'set':
|
197 | spec.Option(opt.short_flag, opt.name)
|
198 | # Notes:
|
199 | # - shopt option don't need to be registered; we validate elsewhere
|
200 | # - 'interactive' Has a cell for internal use, but isn't allowed to be
|
201 | # modified.
|
202 |
|
203 |
|
204 | MAIN_SPEC = FlagSpecAndMore('main')
|
205 |
|
206 | MAIN_SPEC.ShortFlag('-c', args.String,
|
207 | quit_parsing_flags=True) # command string
|
208 | MAIN_SPEC.LongFlag('--help')
|
209 | MAIN_SPEC.LongFlag('--version')
|
210 |
|
211 | # --tool ysh-ify, etc.
|
212 | # default is ''
|
213 | #
|
214 | # More ideas for tools
|
215 | # undefined-vars - a static analysis pass
|
216 | # parse-glob - to debug parsing
|
217 | # parse-printf
|
218 | MAIN_SPEC.LongFlag('--tool', [
|
219 | 'tokens', 'lossless-cat', 'syntax-tree', 'fmt', 'ysh-ify', 'deps', 'cat-em'
|
220 | ])
|
221 |
|
222 | MAIN_SPEC.ShortFlag('-i') # interactive
|
223 | MAIN_SPEC.ShortFlag('-l') # login - currently no-op
|
224 | MAIN_SPEC.LongFlag('--login') # login - currently no-op
|
225 | MAIN_SPEC.LongFlag('--headless') # accepts ECMD, etc.
|
226 |
|
227 | # TODO: -h too
|
228 | # the output format when passing -n
|
229 | MAIN_SPEC.LongFlag(
|
230 | '--ast-format',
|
231 | ['text', 'abbrev-text', 'html', 'abbrev-html', 'oheap', 'none'],
|
232 | default='abbrev-text')
|
233 |
|
234 | # Defines completion style.
|
235 | MAIN_SPEC.LongFlag('--completion-display', ['minimal', 'nice'], default='nice')
|
236 | # TODO: Add option for YSH prompt style? RHS prompt?
|
237 |
|
238 | MAIN_SPEC.LongFlag('--completion-demo')
|
239 |
|
240 | # Debugging feature only. $SH -n won't reparse a[x+1] and ``. Note that $SH
|
241 | # --tool automatically turns it on.
|
242 | MAIN_SPEC.LongFlag('--do-lossless')
|
243 |
|
244 | MAIN_SPEC.LongFlag('--print-status') # TODO: Replace with a shell hook
|
245 | MAIN_SPEC.LongFlag('--debug-file', args.String)
|
246 | MAIN_SPEC.LongFlag('--xtrace-to-debug-file')
|
247 |
|
248 | # This flag has is named like bash's equivalent. We got rid of --norc because
|
249 | # it can simply by --rcfile /dev/null.
|
250 | MAIN_SPEC.LongFlag('--rcfile', args.String)
|
251 | MAIN_SPEC.LongFlag('--rcdir', args.String)
|
252 | MAIN_SPEC.LongFlag('--norc')
|
253 |
|
254 | # e.g. to pass data on stdin but pretend that it came from a .hay file
|
255 | MAIN_SPEC.LongFlag('--location-str', args.String)
|
256 | MAIN_SPEC.LongFlag('--location-start-line', args.Int)
|
257 |
|
258 | _AddShellOptions(MAIN_SPEC)
|
259 |
|
260 | SET_SPEC = FlagSpecAndMore('set')
|
261 | _AddShellOptions(SET_SPEC)
|
262 |
|
263 | #
|
264 | # Types for completion
|
265 | #
|
266 |
|
267 |
|
268 | def _DefineCompletionFlags(spec):
|
269 | # type: (_FlagSpecAndMore) -> None
|
270 | spec.ShortFlag('-F', args.String, help='Complete with this function')
|
271 | spec.ShortFlag('-W', args.String, help='Complete with these words')
|
272 | spec.ShortFlag('-C',
|
273 | args.String,
|
274 | help='Complete with stdout lines of this command')
|
275 |
|
276 | spec.ShortFlag(
|
277 | '-P',
|
278 | args.String,
|
279 | help=
|
280 | 'Prefix is added at the beginning of each possible completion after '
|
281 | 'all other options have been applied.')
|
282 | spec.ShortFlag('-S',
|
283 | args.String,
|
284 | help='Suffix is appended to each possible completion after '
|
285 | 'all other options have been applied.')
|
286 | spec.ShortFlag('-X',
|
287 | args.String,
|
288 | help='''
|
289 | A glob pattern to further filter the matches. It is applied to the list of
|
290 | possible completions generated by the preceding options and arguments, and each
|
291 | completion matching filterpat is removed from the list. A leading ! in
|
292 | filterpat negates the pattern; in this case, any completion not matching
|
293 | filterpat is removed.
|
294 | ''')
|
295 |
|
296 |
|
297 | def _DefineCompletionOptions(spec):
|
298 | # type: (_FlagSpecAndMore) -> None
|
299 | """Common -o options for complete and compgen."""
|
300 | spec.InitOptions()
|
301 |
|
302 | # bashdefault, default, filenames, nospace are used in git
|
303 | spec.Option2('bashdefault',
|
304 | help='If nothing matches, perform default bash completions')
|
305 | spec.Option2(
|
306 | 'default',
|
307 | help="If nothing matches, use readline's default filename completion")
|
308 | spec.Option2(
|
309 | 'filenames',
|
310 | help="The completion function generates filenames and should be "
|
311 | "post-processed")
|
312 | spec.Option2('dirnames',
|
313 | help="If nothing matches, perform directory name completion")
|
314 | spec.Option2(
|
315 | 'nospace',
|
316 | help="Don't append a space to words completed at the end of the line")
|
317 | spec.Option2(
|
318 | 'plusdirs',
|
319 | help="After processing the compspec, attempt directory name completion "
|
320 | "and return those matches.")
|
321 |
|
322 |
|
323 | def _DefineCompletionActions(spec):
|
324 | # type: (_FlagSpecAndMore) -> None
|
325 | """Common -A actions for complete and compgen."""
|
326 |
|
327 | # NOTE: git-completion.bash uses -f and -v.
|
328 | # My ~/.bashrc on Ubuntu uses -d, -u, -j, -v, -a, -c, -b
|
329 | spec.InitActions()
|
330 | spec.Action('a', 'alias')
|
331 | spec.Action('b', 'binding')
|
332 | spec.Action('c', 'command')
|
333 | spec.Action('d', 'directory')
|
334 | spec.Action('f', 'file')
|
335 | spec.Action('j', 'job')
|
336 | spec.Action('u', 'user')
|
337 | spec.Action('v', 'variable')
|
338 | spec.Action(None, 'builtin')
|
339 | spec.Action(None, 'function')
|
340 | spec.Action(None, 'helptopic') # help
|
341 | spec.Action(None, 'setopt') # set -o
|
342 | spec.Action(None, 'shopt') # shopt -s
|
343 | spec.Action(None, 'signal') # kill -s
|
344 | spec.Action(None, 'stopped')
|
345 |
|
346 |
|
347 | COMPLETE_SPEC = FlagSpecAndMore('complete')
|
348 |
|
349 | _DefineCompletionFlags(COMPLETE_SPEC)
|
350 | _DefineCompletionOptions(COMPLETE_SPEC)
|
351 | _DefineCompletionActions(COMPLETE_SPEC)
|
352 |
|
353 | COMPLETE_SPEC.ShortFlag('-E', help='Define the compspec for an empty line')
|
354 | COMPLETE_SPEC.ShortFlag(
|
355 | '-D', help='Define the compspec that applies when nothing else matches')
|
356 |
|
357 | # I would like this to be less compatible
|
358 | # Field name conflicts with 'print' keyword
|
359 | #COMPLETE_SPEC.LongFlag(
|
360 | # '--print', help='Print spec')
|
361 |
|
362 | COMPGEN_SPEC = FlagSpecAndMore('compgen') # for -o and -A
|
363 |
|
364 | # TODO: Add -l for COMP_LINE. -p for COMP_POINT ?
|
365 | _DefineCompletionFlags(COMPGEN_SPEC)
|
366 | _DefineCompletionOptions(COMPGEN_SPEC)
|
367 | _DefineCompletionActions(COMPGEN_SPEC)
|
368 |
|
369 | COMPOPT_SPEC = FlagSpecAndMore('compopt') # for -o
|
370 | _DefineCompletionOptions(COMPOPT_SPEC)
|
371 |
|
372 | COMPADJUST_SPEC = FlagSpecAndMore('compadjust')
|
373 |
|
374 | COMPADJUST_SPEC.ShortFlag(
|
375 | '-n',
|
376 | args.String,
|
377 | help=
|
378 | 'Do NOT split by these characters. It omits them from COMP_WORDBREAKS.')
|
379 | COMPADJUST_SPEC.ShortFlag('-s',
|
380 | help='Treat --foo=bar and --foo bar the same way.')
|
381 |
|
382 | COMPEXPORT_SPEC = FlagSpecAndMore('compexport')
|
383 |
|
384 | COMPEXPORT_SPEC.ShortFlag('-c',
|
385 | args.String,
|
386 | help='Shell string to complete, like sh -c')
|
387 |
|
388 | COMPEXPORT_SPEC.LongFlag('--begin',
|
389 | args.Int,
|
390 | help='Simulate readline begin index into line buffer')
|
391 |
|
392 | COMPEXPORT_SPEC.LongFlag('--end',
|
393 | args.Int,
|
394 | help='Simulate readline end index into line buffer')
|
395 |
|
396 | # jlines is an array of strings with NO header line
|
397 | # TSV8 has a header line. It can have flag descriptions and other data.
|
398 | COMPEXPORT_SPEC.LongFlag('--format', ['jlines', 'tsv8'],
|
399 | default='jlines',
|
400 | help='Output format')
|
401 |
|
402 | #
|
403 | # Pure YSH
|
404 | #
|
405 |
|
406 | TRY_SPEC = FlagSpec('try_')
|
407 | TRY_SPEC.LongFlag('--assign',
|
408 | args.String,
|
409 | help='Assign status to this variable, and return 0')
|
410 |
|
411 | ERROR_SPEC = FlagSpec('error')
|
412 |
|
413 | BOOLSTATUS_SPEC = FlagSpec('boolstatus')
|
414 |
|
415 | # Future directions:
|
416 | # run --builtin, run --command, run --proc:
|
417 | # to "replace" 'builtin' and # 'command'
|
418 |
|
419 | APPEND_SPEC = FlagSpec('append')
|
420 |
|
421 | SHVAR_SPEC = FlagSpec('shvar')
|
422 | #SHVAR_SPEC.Flag('-temp', args.String,
|
423 | # help='Push a NAME=val binding')
|
424 | #SHVAR_SPEC.Flag('-env', args.String,
|
425 | # help='Push a NAME=val binding and set the -x flag')
|
426 |
|
427 | CTX_SPEC = FlagSpec('ctx')
|
428 |
|
429 | PP_SPEC = FlagSpec('pp')
|
430 |
|
431 | SHVM_SPEC = FlagSpec('shvm')
|
432 |
|
433 | # --verbose?
|
434 | FORK_SPEC = FlagSpec('fork')
|
435 | FORKWAIT_SPEC = FlagSpec('forkwait')
|
436 |
|
437 | # Might want --list at some point
|
438 | MODULE_SPEC = FlagSpec('module')
|
439 |
|
440 | RUNPROC_SPEC = FlagSpec('runproc')
|
441 | RUNPROC_SPEC.ShortFlag('-h', args.Bool, help='Show all procs')
|
442 |
|
443 | WRITE_SPEC = FlagSpec('write')
|
444 | WRITE_SPEC.LongFlag('--sep',
|
445 | args.String,
|
446 | default='\n',
|
447 | help='Characters to separate each argument')
|
448 | WRITE_SPEC.LongFlag('--end',
|
449 | args.String,
|
450 | default='\n',
|
451 | help='Characters to terminate the whole invocation')
|
452 | WRITE_SPEC.ShortFlag('-n',
|
453 | args.Bool,
|
454 | help="Omit newline (synonym for -end '')")
|
455 | # Do we need these two?
|
456 | WRITE_SPEC.LongFlag('--json',
|
457 | args.Bool,
|
458 | default=False,
|
459 | help='Write elements as JSON strings(lossy)')
|
460 | WRITE_SPEC.LongFlag('--j8',
|
461 | args.Bool,
|
462 | default=False,
|
463 | help='Write elements as J8 strings')
|
464 | # TODO: --jlines for conditional j"" prefix? Like maybe_shell_encode()
|
465 |
|
466 | # Legacy that's not really needed with J8 notation. The = operator might use a
|
467 | # separate pretty printer that shows \u{3bc}
|
468 | #
|
469 | # x means I want \x00
|
470 | # u means I want \u{1234}
|
471 | # raw is utf-8
|
472 | if 0:
|
473 | WRITE_SPEC.LongFlag(
|
474 | '--unicode', ['raw', 'u', 'x'],
|
475 | default='raw',
|
476 | help='Encode QSN with these options. '
|
477 | 'x assumes an opaque byte string, while raw and u try to '
|
478 | 'decode UTF-8.')
|
479 |
|
480 | PUSH_REGISTERS_SPEC = FlagSpec('push-registers')
|
481 |
|
482 | FOPEN_SPEC = FlagSpec('fopen')
|
483 |
|
484 | #
|
485 | # JSON
|
486 | #
|
487 |
|
488 | JSON_WRITE_SPEC = FlagSpec('json_write')
|
489 |
|
490 | # TODO: --compact is probably better
|
491 | # --pretty=F is like JSON.stringify(d, null, 0)
|
492 | JSON_WRITE_SPEC.LongFlag('--pretty',
|
493 | args.Bool,
|
494 | default=True,
|
495 | help='Whitespace in output (default true)')
|
496 |
|
497 | # Unused:
|
498 | # JSON has the questionable decision of allowing (unpaired) surrogate like
|
499 | # \udc00.
|
500 | # When encoding, we try to catch the error on OUR side, rather than letting it
|
501 | # travel over the wire. But you can disable this.
|
502 | JSON_WRITE_SPEC.LongFlag(
|
503 | '--surrogate-ok',
|
504 | args.Bool,
|
505 | default=False,
|
506 | help='Invalid UTF-8 can be encoded as surrogate like \\udc00')
|
507 |
|
508 | JSON_WRITE_SPEC.LongFlag('--indent',
|
509 | args.Int,
|
510 | default=2,
|
511 | help='Indent JSON by this amount')
|
512 |
|
513 | JSON_READ_SPEC = FlagSpec('json_read')
|