def _GetOpts(spec, argv, optind, errfmt): optarg = '' # not set by default try: current = argv[optind-1] # 1-based indexing except IndexError: return 1, '?', optarg, optind if not current.startswith('-'): # The next arg doesn't look like a flag. return 1, '?', optarg, optind # It looks like an argument. Stop iteration by returning 1. if current not in spec: # Invalid flag optind += 1 return 0, '?', optarg, optind optind += 1 opt_char = current[-1] needs_arg = spec[current] if needs_arg: try: optarg = argv[optind-1] # 1-based indexing except IndexError: errfmt.Print('getopts: option %r requires an argument.', current) ui.Stderr('(getopts argv: %s)', ' '.join(pretty.String(a) for a in argv)) # Hm doesn't cause status 1? return 0, '?', optarg, optind optind += 1 return 0, opt_char, optarg, optind
def DisplayLine(self): # type: () -> str # NOTE: This is the format the Tracer uses. # bash displays sleep $n & (code) # but OSH displays sleep 1 & (argv array) # We could switch the former but I'm not sure it's necessary. return '[process] %s' % ' '.join(pretty.String(a) for a in self.cmd_val.argv)
def OnSimpleCommand(self, argv): # NOTE: I think tracing should be on by default? For post-mortem viewing. if not self.exec_opts.xtrace: return first_char, prefix = self._EvalPS4() cmd = ' '.join(pretty.String(a) for a in argv) self.f.log('%s%s%s', first_char, prefix, cmd)
def _TrySingleLine(node, f, max_chars): # type: (hnode_t, ColorOutput, int) -> bool """Try printing on a single line. Args: node: homogeneous tree node f: ColorOutput instance max_chars: maximum number of characters to print on THIS line indent: current indent level Returns: ok: whether it fit on the line of the given size. If False, you can't use the value of f. """ UP_node = node # for mycpp tag = node.tag_() if tag == hnode_e.Leaf: node = cast(hnode__Leaf, UP_node) f.PushColor(node.color) f.write(pretty.String(node.s)) f.PopColor() elif tag == hnode_e.External: node = cast(hnode__External, UP_node) f.PushColor(color_e.External) f.write(repr(node.obj)) f.PopColor() elif tag == hnode_e.Array: node = cast(hnode__Array, UP_node) # Can we fit the WHOLE array on the line? f.write('[') for i, item in enumerate(node.children): if i != 0: f.write(' ') if not _TrySingleLine(item, f, max_chars): return False f.write(']') elif tag == hnode_e.Record: node = cast(hnode__Record, UP_node) return _TrySingleLineObj(node, f, max_chars) else: raise AssertionError(hnode_str(tag)) # Take into account the last char. num_chars_so_far = f.NumChars() if num_chars_so_far > max_chars: return False return True
def PrintNode(self, node, f, indent): # type: (hnode_t, ColorOutput, int) -> None """Second step of printing: turn homogeneous tree into a colored string. Args: node: homogeneous tree node f: ColorOutput instance. max_col: don't print past this column number on ANY line NOTE: See asdl/run.sh line-length-hist for a test of this. It's approximate. TODO: Use the terminal width. """ ind = ' ' * indent # Try printing on a single line single_f = f.NewTempBuffer() single_f.write(ind) if _TrySingleLine(node, single_f, self.max_col - indent): f.WriteRaw(single_f.GetRaw()) return UP_node = node # for mycpp tag = node.tag_() if tag == hnode_e.Leaf: node = cast(hnode__Leaf, UP_node) f.PushColor(node.color) f.write(pretty.String(node.s)) f.PopColor() elif tag == hnode_e.External: node = cast(hnode__External, UP_node) f.PushColor(color_e.External) f.write(repr(node.obj)) f.PopColor() elif tag == hnode_e.Record: node = cast(hnode__Record, UP_node) self._PrintRecord(node, f, indent) else: raise AssertionError(hnode_str(tag))