Skip to content

Commit

Permalink
feat: refactor cli into using click
Browse files Browse the repository at this point in the history
  • Loading branch information
hhamud committed Jul 31, 2023
1 parent 8906129 commit c4b52e2
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 26 deletions.
1 change: 1 addition & 0 deletions dasy/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import hy
2 changes: 1 addition & 1 deletion dasy/cli/commands.hy → dasy/cli/main.hy
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(import click)
(import pathlib [Path])
(import helpers *)
(import dasy.cli.helpers *)


(defn
Expand Down
38 changes: 38 additions & 0 deletions dasy/cli/vyper.hy
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
(import sys)
(import click)
(import dasy [compiler parser])
(import vyper.compiler [OUTPUT_FORMATS as VYPER_OUTPUT_FORMATS])
(import dasy.parser.output [get_external_interface])



(setv format_help = """
Format to print, one or more of:
bytecode (default) - Deployable bytecode
bytecode_runtime - Bytecode at runtime
abi - ABI in JSON format
abi_python - ABI in python format
source_map - Vyper source map
method_identifiers - Dictionary of method signature to method identifier
userdoc - Natspec user documentation
devdoc - Natspec developer documentation
combined_json - All of the above format options combined as single JSON output
layout - Storage layout of a Vyper contract
ast - AST in JSON format
external_interface - External (Dasy) interface of a contract, used for outside contract calls
vyper_interface - External (Vyper) interface of a contract, used for outside contract calls
opcodes - List of opcodes as a string
opcodes_runtime - List of runtime opcodes as a string
ir - Intermediate representation in list format
ir_json - Intermediate representation in JSON format
hex-ir - Output IR and assembly constants in hex instead of decimal
no-optimize - Do not optimize (don't use this for production code)
""")

(setv OUTPUT_FORMATS (VYPER_OUTPUT_FORMATS.copy))
(setv )


OUTPUT_FORMATS = VYPER_OUTPUT_FORMATS.copy()
OUTPUT_FORMATS["vyper_interface"] = OUTPUT_FORMATS["external_interface"]
OUTPUT_FORMATS["external_interface"] = get_external_interface
45 changes: 20 additions & 25 deletions dasy/main.py → dasy/cli/vyper.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import sys
import click
from dasy import compiler, parser
from vyper.compiler import OUTPUT_FORMATS as VYPER_OUTPUT_FORMATS
from vyper import compiler as vyper_compiler
import argparse
import sys

from dasy.parser.output import get_external_interface

format_help = """Format to print, one or more of:
format_help = """
Format to print, one or more of:
bytecode (default) - Deployable bytecode
bytecode_runtime - Bytecode at runtime
abi - ABI in JSON format
Expand All @@ -29,33 +28,29 @@
"""

OUTPUT_FORMATS = VYPER_OUTPUT_FORMATS.copy()

OUTPUT_FORMATS["vyper_interface"] = OUTPUT_FORMATS["external_interface"]
OUTPUT_FORMATS["external_interface"] = get_external_interface


def main():
parser = argparse.ArgumentParser(
prog="dasy",
description="Lispy Smart Contract Language for the EVM",
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument("filename", type=str, nargs="?", default="")
parser.add_argument("-f", help=format_help, default="bytecode", dest="format")

@click.command(
help="Lispy Smart Contract Language for the EVM",
context_settings=dict(help_option_names=["-h", "--help"]),
width=120,
)
@click.argument("filename", type=click.Path(exists=True), default="")
@click.option("-f", "--format", help=format_help, default="bytecode")
def main(filename, format):
src = ""

args = parser.parse_args()

if args.filename != "":
with open(args.filename, "r") as f:
if filename:
with open(filename, "r") as f:
src = f.read()
if args.filename.endswith(".vy"):
if filename.endswith(".vy"):
data = compiler.CompilerData(
src, contract_name=args.filename.split("/")[-1].split(".")[0]
src, contract_name=filename.split("/")[-1].split(".")[0]
)
else:
data = compiler.compile(src, name=args.filename.split(".")[0])
data = compiler.compile(src, name=filename.split(".")[0])
else:
for line in sys.stdin:
src += line
Expand All @@ -68,12 +63,12 @@ def main():
"ir_json": "ir_dict",
"interface": "external_interface",
}
output_format = translate_map.get(args.format, args.format)
output_format = translate_map.get(format, format)
if output_format in OUTPUT_FORMATS:
print(OUTPUT_FORMATS[output_format](data))
else:
raise Exception(
f"Unrecognized Output Format {args.format}. Must be one of {OUTPUT_FORMATS.keys()}"
raise click.ClickException(
f"Unrecognized Output Format {format}. Must be one of {OUTPUT_FORMATS.keys()}"
)


Expand Down

0 comments on commit c4b52e2

Please sign in to comment.