Skip to content

Commit

Permalink
improved CLI
Browse files Browse the repository at this point in the history
Used argparse to create an improved CLI, now supports starting the repl from the greenberry CLI.
  • Loading branch information
0x32767 authored and Abdur-rahmaanJ committed Nov 18, 2023
1 parent ed5c6e9 commit 86163ed
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 27 deletions.
40 changes: 34 additions & 6 deletions src/greenberry/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,46 @@
@author: ARJ
"""

import os
from argparse import ArgumentParser, Namespace
import sys
import os

from greenberry.gb import greenberry_eval
from greenberry.repl import run_repl


def main():
if len(sys.argv) == 2:
with open(os.path.join(os.getcwd(), sys.argv[1])) as f:
x = f.read()

greenberry_eval(x)
"""
The command line will take in two arguments, any combination of either a script name or a `repl` argument.
First any provided scripts are executed, then the repl is run (if the `-repl` argument has been provided)
If it has, the repl will operate in the same namespace as the executed program.
"""
arg_parser = ArgumentParser(
prog="Green Berry",
description="A one line statement programming language.",
epilog="https://github.com/Abdur-rahmaanJ/greenberry",
)
arg_parser.add_argument(
"scriptname",
nargs="*",
type=str,
help="The name of the script you want to run",
)
arg_parser.add_argument(
"-repl",
action="store_true",
help="Will start a repl after the program has completed, with the program namepsace.",
)

namespace: Namespace = arg_parser.parse_args(sys.argv[1:])

if namespace.scriptname:
with open(namespace.scriptname, "r") as f:
for line in f.readlines():
greenberry_eval(line)

if namespace.repl:
run_repl()


if __name__ == "__main__":
Expand Down
54 changes: 33 additions & 21 deletions src/greenberry/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,36 @@
"""
from greenberry.gb import greenberry_eval

print(
"""
---greenberry(c)---
welcome to the .gb REPL
---greenberry(c)---
"""
)
isSessionOn = 1
while isSessionOn == 1:
x = input("---> ")
greenberry_eval(x)
if x == "berry exit":
isSessionOn = 0
print()
print(
"""
---greenberry(c)---
.gb REPL exited. see you soon _ _
---greenberry(c)---
"""
)

def run_repl():
print(
"""
---greenberry(c)---
welcome to the .gb REPL
---greenberry(c)---
"""
)
try:
while True:
x = input("---> ")
if x == "berry exit":
break

greenberry_eval(x)

print()

except KeyboardInterrupt:
pass

print(
"""
---greenberry(c)---
.gb REPL exited. see you soon _ _
---greenberry(c)---
"""
)


if __name__ == "__main__":
run_repl()

0 comments on commit 86163ed

Please sign in to comment.