-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell_example.py
executable file
·60 lines (46 loc) · 1.29 KB
/
shell_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/python3
if __name__ == '__main__':
import _extend_path
import sys
import rich
from mash.shell.function import ShellFunction as Function
from mash.shell.shell import Shell, has_input, set_cli_args, sh_to_py, main
from mash.io_util import has_output
def f(x: int): return x
def g(x: int, y=1): return x + y
def h(x: int, y: float, z): return x + y * z
def example(a: int, b, c: float = 3.):
"""An example of a function with a docstring
Parameters
----------
a: positive number
b: object
"""
return a
def inspect(func_name):
"""Inspect a function
based on rich.inspect
"""
func = Shell.get_method(func_name)
if func is None:
return
rich.inspect(func)
functions = {
'a_long_function': f,
'another_function': f,
'f': f,
'g': g,
'h': h,
'example': example,
'inspect': inspect,
'ls': Function(sh_to_py('ls'), args={'-latr': 'flags', '[file]': ''}),
'vi': Function(sh_to_py('vi'), args={'[file]': ''})}
if __name__ == '__main__':
# setting cli args is a requirement for shell.has_input
set_cli_args()
if has_output(sys.stdin) or has_input():
main(functions=functions)
else:
from mash import cli
# use_shell_with_history:
cli.main(functions=functions)