This repository has been archived by the owner on Jul 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[slave-core] (#11) Added simple shell and module to parse arguments.
Signed-off-by: JeyM1 <[email protected]>
- Loading branch information
Showing
2 changed files
with
59 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import cmd | ||
import argparse | ||
import tempfile | ||
from slave_core.slave_plugin import * | ||
|
||
|
||
class ArgparseCmdWrapper: | ||
def __init__(self, parser): | ||
"""Init decorator with an argparse parser to be used in parsing cmd-line options""" | ||
self.parser = parser | ||
self.help_msg = "" | ||
|
||
def __call__(self, f): | ||
"""Decorate 'f' to parse 'line' and pass options to decorated function""" | ||
if not self.parser: # If no parser was passed to the decorator, get it from 'f' | ||
self.parser = f(None, None, None, True) | ||
|
||
def wrapped_f(*args): | ||
line = args[1].split() | ||
try: | ||
parsed = self.parser.parse_args(line) | ||
except SystemExit: | ||
return | ||
f(*args, parsed=parsed) | ||
|
||
return wrapped_f | ||
|
||
|
||
class PathfinderShell(cmd.Cmd): | ||
intro = 'Welcome to the turtle shell. Type help or ? to list commands.\n' | ||
prompt = '> ' | ||
|
||
def do_hello(self, args): | ||
"""Says hello. If you provide a name, it will greet you with it.""" | ||
if len(args) == 0: | ||
name = 'stranger' | ||
else: | ||
name = args | ||
print(f"Hello, {name}") | ||
|
||
__render_parser = argparse.ArgumentParser(prog="render", description="Runs plugin python script for rendering.") | ||
__render_parser.add_argument('--test', help="test help") | ||
|
||
@ArgparseCmdWrapper(parser=__render_parser) | ||
def do_render(self, line, parsed): | ||
"""Runs plugin python script.""" | ||
|
||
print(parsed) | ||
|
||
def help_render(self): | ||
self.__render_parser.print_help() |