-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnu_plugin_plus
More file actions
executable file
·57 lines (40 loc) · 1.82 KB
/
nu_plugin_plus
File metadata and controls
executable file
·57 lines (40 loc) · 1.82 KB
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
#!/usr/bin/env python3
# Copyright (C) 2019 Vanessa Sochat.
# This Source Code Form is subject to the terms of the
# Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
# with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
from nushell.filter import FilterPlugin
# Your filter function will be called by the FilterPlugin, and should
# accept the plugin and the dictionary of params
def runFilter(plugin, params):
'''runFilter will be executed by the calling FilterPlugin (method filter)
and should be able to parse the dictionary of params and respond
appropriately. Useful functions:
plugin.logger.<level>
plugin.get_string_primitive()
plugin.get_int_primitive()
plugin.print_int_response()
plugin.print_string_response()
'''
# don't care about type, can also be get_string_primitive or get_int_primitive
value = plugin.get_primitive()
# Get the string primitive passed from the filter (should be Int)
try:
value = int(value)
# Add to the required positional argument
total = value + params['_positional'][0]
# Print an integer response (can also be print_string_response)
plugin.print_string_response(str(total))
except:
plugin.print_string_response("%s is not a number" % value)
# The main function is where you create your plugin and run it.
def main():
# Initialize a new plugin
plugin = FilterPlugin(name="plus",
usage="Add a number to what is passed to the filter.")
# Add positional arguments (print help if not provided)
plugin.add_positional_argument("number", "Optional", "Int", usage="number to parse")
# Run the plugin by passing your filter function
plugin.run(runFilter)
if __name__ == '__main__':
main()