I have a function in Xonsh that I'm trying to use like a command (ie: without parens). The function should optionally take arguments, however whenever I call the function without arguments, I just get the function address. How do you call a function with optional parameters?
Here's the example:
def _up(args, stdin=None):
# go up any number of directories
if not args or len(args) < 1:
args[0] = 1
balloons = ('../' * int(args[0]))
# echo @(balloons)
cd @(balloons)
aliases['up'] = _up
When I call up
with no parameters, I get <function __main__.up>
. When I call it like this, it works: up 2
.
I could do a function like this that works, but then I can't call it without using parentheses (ie: as a command) which is what I'd prefer:
def up(dirs=1):
# go up any number of directories
balloons = ('../' * dirs)
# echo @(balloons)
cd @(balloons)
Calling up()
and up(2)
both work this way, but is more cumbersome than just calling up
or up 2
. What's the right way to accomplish what I'm trying to do in Xonsh?
I'm not certain why you're getting the function repr
when you aren't passing in an argument, but a tweaked version of your function works:
def _up(args): # don't need stdin
# go up any number of directories
if not args or len(args) < 1:
args = [1] # if args is None you can't index to it
balloons = ('../' * int(args[0]))
# echo @(balloons)
cd @(balloons)
aliases['up'] = _up
on current-ish main
of xonsh
@ c2f862df
this works to go up one level with just an up
or you can specify many levels with up 3
, etc.