Skip to content

Commit

Permalink
Added support for helptext in setup()'s hinting. Valid formats are:
Browse files Browse the repository at this point in the history
  * `arg`
  * `arg: typename`
  * `arg: (typename, 'helptext')` (prefered)
  * `arg: [typename, 'helptext']`

also added support for reading the sketch module's typestring and inserting it into the argparse description
  • Loading branch information
IGBC authored and Ashley Brown committed Jul 17, 2017
1 parent aedc66b commit dd78846
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions sketches/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@ def __init__(self, filename: str, imports: List[Tuple[str, str]] = __default_pac
raise FileNotFoundError

self.__sketch = importlib.machinery.SourceFileLoader("sketch", module_path).load_module()
self.__parser = argparse.ArgumentParser();

self.__parser = argparse.ArgumentParser(description=self.__sketch.__doc__)
self.__parser.add_argument(filename, help="This sketchfile")

# Read setup() annotations and give them to argparse
if 'setup' in dir(self.__sketch):
fun = self.__sketch.setup
args = fun.__code__.co_varnames[:fun.__code__.co_argcount]
defaults = { n: v for n, v in zip(reversed(args), reversed(fun.__defaults__)) }
for n in args:
defaults = { n: v for n, v in zip(reversed(args), reversed(fun.__defaults__)) }
self.__parser.add_argument(n, type=fun.__annotations__.get(n,str), nargs=('?' if n in defaults else None),
default=defaults.get(n), help=("(default: %(default)s)" if n in defaults else ""))
annotation = fun.__annotations__.get(n,str)
if not isinstance(annotation, (tuple, list)):
annotation = (annotation, "")
helpstr = "{} {} {}".format(annotation[1],annotation[0], " (default: %(default)s)" if n in defaults else "")
self.__parser.add_argument(n, type=annotation[0], nargs=('?' if n in defaults else None), default=defaults.get(n), help=helpstr)

# Register library imports into sketch
for item in imports:
Expand Down Expand Up @@ -99,12 +102,12 @@ def main():
formatter = logging.Formatter('[%(levelname)s]: %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)

args = parser.parse_args().__dict__
try:
runner = SketchRunner(args['sketchfile'])
except:
except FileNotFoundError:
exit()

runner.run(args['sketch arguments'])

0 comments on commit dd78846

Please sign in to comment.