mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-04 08:24:40 +01:00
Merge remote-tracking branch 'origin/main'
This commit is contained in:
commit
00c6d4042c
1 changed files with 36 additions and 31 deletions
|
@ -1339,37 +1339,13 @@ may have one of the following signatures:
|
||||||
def mycmd0():
|
def mycmd0():
|
||||||
"""This form takes no arguments but may return output or a return code.
|
"""This form takes no arguments but may return output or a return code.
|
||||||
"""
|
"""
|
||||||
return "some output."
|
|
||||||
|
|
||||||
def mycmd1(args):
|
|
||||||
"""This form takes a single argument, args. This is a list of strings
|
|
||||||
representing the arguments to this command. Feel free to parse them
|
|
||||||
however you wish!
|
|
||||||
"""
|
|
||||||
# perform some action.
|
|
||||||
return 0
|
|
||||||
|
|
||||||
def mycmd2(args, stdin=None):
|
|
||||||
"""This form takes two arguments. The args list like above, as a well
|
|
||||||
as standard input. stdin will be a file like object that the command
|
|
||||||
can read from, if the user piped input to this command. If no input
|
|
||||||
was provided this will be None.
|
|
||||||
"""
|
|
||||||
# do whatever you want! Anything you print to stdout or stderr
|
|
||||||
# will be captured for you automatically. This allows callable
|
|
||||||
# aliases to support piping.
|
|
||||||
print('I go to stdout and will be printed or piped')
|
|
||||||
|
|
||||||
# Note: that you have access to the xonsh
|
|
||||||
# built-ins if you 'import builtins'. For example, if you need the
|
|
||||||
# environment, you could do the following:
|
|
||||||
import builtins
|
|
||||||
env = builtins.__xonsh__.env
|
|
||||||
|
|
||||||
# The return value of the function can either be None,
|
# The return value of the function can either be None,
|
||||||
return
|
return
|
||||||
|
|
||||||
# a single string representing stdout
|
# a return code,
|
||||||
|
return 0
|
||||||
|
|
||||||
|
# a single string representing stdout,
|
||||||
return 'I am out of here'
|
return 'I am out of here'
|
||||||
|
|
||||||
# or you can build up strings for stdout and stderr and then
|
# or you can build up strings for stdout and stderr and then
|
||||||
|
@ -1385,6 +1361,37 @@ may have one of the following signatures:
|
||||||
# examples the return code would be 0/success.
|
# examples the return code would be 0/success.
|
||||||
return (None, "I failed", 2)
|
return (None, "I failed", 2)
|
||||||
|
|
||||||
|
# Anything you print to stdout or stderr
|
||||||
|
# will be captured for you automatically. This allows callable
|
||||||
|
# aliases to support piping.
|
||||||
|
print('I go to stdout and will be printed or piped')
|
||||||
|
|
||||||
|
# Note: that you have access to the xonsh
|
||||||
|
# built-ins if you 'import builtins'. For example, if you need the
|
||||||
|
# environment, you could do the following:
|
||||||
|
import builtins
|
||||||
|
env = builtins.__xonsh__.env
|
||||||
|
|
||||||
|
def mycmd1(args):
|
||||||
|
"""This form takes a single argument, args. This is a list of strings
|
||||||
|
representing the arguments to this command. Feel free to parse them
|
||||||
|
however you wish!
|
||||||
|
"""
|
||||||
|
# perform some action.
|
||||||
|
print(f"arg count: {len(args)}")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
def mycmd2(args, stdin=None):
|
||||||
|
"""This form takes two arguments. The args list like above, as a well
|
||||||
|
as standard input. stdin will be a file like object that the command
|
||||||
|
can read from, if the user piped input to this command. If no input
|
||||||
|
was provided this will be None.
|
||||||
|
"""
|
||||||
|
# Read input either from piped input or the terminal
|
||||||
|
stdin = stdin or sys.stdin
|
||||||
|
for line in stdin.readlines():
|
||||||
|
print(line.strip().upper() + '!')
|
||||||
|
|
||||||
def mycmd3(args, stdin=None, stdout=None):
|
def mycmd3(args, stdin=None, stdout=None):
|
||||||
"""This form has three parameters. The first two are the same as above.
|
"""This form has three parameters. The first two are the same as above.
|
||||||
The last argument represents the standard output. This is a file-like
|
The last argument represents the standard output. This is a file-like
|
||||||
|
@ -1406,10 +1413,8 @@ may have one of the following signatures:
|
||||||
for i in range(5):
|
for i in range(5):
|
||||||
time.sleep(i)
|
time.sleep(i)
|
||||||
print(i, file=stdout)
|
print(i, file=stdout)
|
||||||
|
stdout.flush() # flush output to terminal immediately
|
||||||
|
|
||||||
# In this form, the return value should be a single integer
|
|
||||||
# representing the "return code" of the alias (zero if successful,
|
|
||||||
# non-zero otherwise)
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def mycmd5(args, stdin=None, stdout=None, stderr=None, spec=None):
|
def mycmd5(args, stdin=None, stdout=None, stderr=None, spec=None):
|
||||||
|
|
Loading…
Add table
Reference in a new issue