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