Skip to content

Commit

Permalink
Add a 'repr' builtin for debugging.
Browse files Browse the repository at this point in the history
It's a lot easier to type than:

argv.py "${a[@]}"
  • Loading branch information
Andy Chu committed Sep 22, 2018
1 parent f264a83 commit 1413294
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
20 changes: 20 additions & 0 deletions core/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@
"unalias": builtin_e.UNALIAS,

"help": builtin_e.HELP,

# OSH only
"repr": builtin_e.REPR,
}


Expand Down Expand Up @@ -1340,6 +1343,23 @@ def Help(argv, loader):
return 0


def Repr(argv, mem):
"""Given a list of variable names, print their values.
'repr a' is a lot easier to type than 'argv.py "${a[@]}"'.
"""
status = 0
for name in argv:
# TODO: Should we print flags too?
val = mem.GetVar(name)
if val.tag == value_e.Undef:
print('%r is not defined' % name, file=sys.stderr)
status = 1
else:
print('%s = %s' % (name, val))
return status


def main(argv):
# Localization: Optionally use GNU gettext()? For help only. Might be
# useful in parser error messages too. Good thing both kinds of code are
Expand Down
3 changes: 3 additions & 0 deletions core/cmd_exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,9 @@ def _RunBuiltin(self, builtin_id, argv, span_id):
loader = util.GetResourceLoader()
status = builtin.Help(argv, loader)

elif builtin_id == builtin_e.REPR:
status = builtin.Repr(argv, self.mem)

else:
raise AssertionError('Unhandled builtin: %s' % builtin_id)

Expand Down
1 change: 1 addition & 0 deletions core/runtime.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ module runtime
| COMMAND | TYPE | HELP
| DECLARE | TYPESET | ALIAS | UNALIAS
| PWD
| REPR

-- word_eval.py: SliceParts is for ${a-} and ${a+}, Error is for ${a?}, and
-- SliceAndAssign is for ${a=}.
Expand Down
13 changes: 13 additions & 0 deletions spec/osh-only.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ set -o debug-completion
$SH -o debug-completion
## status: 0

#### repr
x=42
repr x
echo status=$?
repr nonexistent
echo status=$?
## STDOUT:
x = (Str s:42)
status=0
status=1
## END


#### crash dump
rm -f $TMP/*.json
OSH_CRASH_DUMP_DIR=$TMP $SH -c '
Expand Down

0 comments on commit 1413294

Please sign in to comment.