Skip to content

Commit

Permalink
lots of cleanup and breaking changes :)
Browse files Browse the repository at this point in the history
  • Loading branch information
thatstoasty committed Oct 9, 2024
1 parent 50373aa commit 60cade3
Show file tree
Hide file tree
Showing 24 changed files with 288 additions and 513 deletions.
4 changes: 2 additions & 2 deletions examples/aliases.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ fn tool_func(ctx: Context) -> None:
fn main() -> None:
var root = Command(
name="my",
description="This is a dummy command!",
usage="This is a dummy command!",
run=test,
)

var print_tool = Arc(
Command(
name="tool", description="This is a dummy command!", run=tool_func, aliases=List[String]("object", "thing")
name="tool", usage="This is a dummy command!", run=tool_func, aliases=List[String]("object", "thing")
)
)

Expand Down
15 changes: 8 additions & 7 deletions examples/arg_validators.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,33 @@ fn hello(ctx: Context) -> None:
fn main() -> None:
var root = Command(
name="hello",
description="This is a dummy command!",
usage="This is a dummy command!",
run=test,
)

var no_args_command = Arc(Command(name="no_args", description="This is a dummy command!", run=hello))
var no_args_command = Arc(Command(name="no_args", usage="This is a dummy command!", run=hello))
no_args_command[].arg_validator = no_args

var valid_args_command = Arc(
Command(
name="valid_args",
description="This is a dummy command!",
usage="This is a dummy command!",
run=hello,
valid_args=List[String]("Pineapple")
)
)
valid_args_command[].arg_validator = valid_args

var minimum_n_args_command = Arc(Command(name="minimum_n_args", description="This is a dummy command!", run=hello))
var minimum_n_args_command = Arc(Command(name="minimum_n_args", usage="This is a dummy command!", run=hello))
minimum_n_args_command[].arg_validator = minimum_n_args[4]()

var maximum_n_args_command = Arc(Command(name="maximum_n_args", description="This is a dummy command!", run=hello))
var maximum_n_args_command = Arc(Command(name="maximum_n_args", usage="This is a dummy command!", run=hello))
maximum_n_args_command[].arg_validator = maximum_n_args[1]()

var exact_args_command = Arc(Command(name="exact_args", description="This is a dummy command!", run=hello))
var exact_args_command = Arc(Command(name="exact_args", usage="This is a dummy command!", run=hello))
exact_args_command[].arg_validator = exact_args[1]()

var range_args_command = Arc(Command(name="range_args", description="This is a dummy command!", run=hello))
var range_args_command = Arc(Command(name="range_args", usage="This is a dummy command!", run=hello))
range_args_command[].arg_validator = range_args[0, 1]()

root.add_subcommand(no_args_command)
Expand Down
4 changes: 2 additions & 2 deletions examples/chromeria.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ fn hello(ctx: Context) -> None:
fn main() -> None:
var root = Command(
name="hello",
description="This is a dummy command!",
usage="This is a dummy command!",
run=test,
)

var hello_command = Arc(Command(name="chromeria", description="This is a dummy command!", run=hello))
var hello_command = Arc(Command(name="chromeria", usage="This is a dummy command!", run=hello))

root.add_subcommand(hello_command)
root.execute()
4 changes: 2 additions & 2 deletions examples/fg_child.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ fn tool_func(ctx: Context) -> None:
fn main() -> None:
var root = Command(
name="my",
description="This is a dummy command!",
usage="This is a dummy command!",
run=test,
)
root.persistent_flags.bool_flag(name="required", shorthand="r", usage="Always required.")
root.persistent_flags.string_flag(name="host", shorthand="h", usage="Host")
root.persistent_flags.string_flag(name="port", shorthand="p", usage="Port")
root.mark_persistent_flag_required("required")

var print_tool = Arc(Command(name="tool", description="This is a dummy command!", run=tool_func))
var print_tool = Arc(Command(name="tool", usage="This is a dummy command!", run=tool_func))
print_tool[].flags.bool_flag(name="also", shorthand="a", usage="Also always required.")
print_tool[].flags.string_flag(name="uri", shorthand="u", usage="URI")
root.add_subcommand(print_tool)
Expand Down
12 changes: 6 additions & 6 deletions examples/fg_parent.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ from memory import Arc
from prism import Command, Context


fn test(ctx: Context) -> None:
fn test(ctx: Context) raises -> None:
var host = ctx.command[].flags.get_string("host")
var port = ctx.command[].flags.get_string("port")
var uri = ctx.command[].flags.get_string("uri")

if uri:
print("URI: ", uri.value())
if uri != "":
print("URI:", uri)
else:
print(host.value(), ":", port.value())
print(host + ":" + port)


fn tool_func(ctx: Context) -> None:
Expand All @@ -20,8 +20,8 @@ fn tool_func(ctx: Context) -> None:
fn main() -> None:
var root = Command(
name="my",
description="This is a dummy command!",
run=test,
usage="This is a dummy command!",
raising_run=test,
)
root.persistent_flags.bool_flag(name="required", shorthand="r", usage="Always required.")
root.persistent_flags.string_flag(name="host", shorthand="h", usage="Host")
Expand Down
15 changes: 9 additions & 6 deletions examples/hello_world.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn build_printer_command() -> Arc[Command]:
var cmd = Arc(
Command(
name="printer",
description="Print the first arg.",
usage="Print the first arg.",
run=printer,
)
)
Expand All @@ -42,7 +42,7 @@ fn build_say_command() -> Arc[Command]:
return Arc(
Command(
name="say",
description="Say something to someone",
usage="Say something to someone",
run=say,
)
)
Expand All @@ -52,7 +52,7 @@ fn build_hello_command() -> Arc[Command]:
var cmd = Arc(
Command(
name="hello",
description="Say hello to someone",
usage="Say hello to someone",
run=say_hello,
)
)
Expand All @@ -63,15 +63,18 @@ fn build_goodbye_command() -> Arc[Command]:
var cmd = Arc(
Command(
name="goodbye",
description="Say goodbye to someone",
usage="Say goodbye to someone",
run=say_goodbye,
)
)
return cmd


fn test(ctx: Context) -> None:
print(ctx.command[].flags.get_string("env").value())
try:
print(ctx.command[].flags.get_string("env"))
except:
print("No env flag provided.")
for item in ctx.command[].flags.flags:
if item[].value:
print(item[].name, item[].value.value())
Expand All @@ -84,7 +87,7 @@ fn test(ctx: Context) -> None:
fn main() -> None:
var root = Command(
name="tones",
description="This is a dummy command!",
usage="This is a dummy command!",
run=test,
)
root.flags.string_flag(name="env", shorthand="e", usage="Environment.", default="")
Expand Down
2 changes: 1 addition & 1 deletion examples/printer/mojoproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ version = "0.1.0"
[dependencies]
max = ">=24.5.0,<25"
mist = ">=0.1.8,<0.2"
prism = ">=0.1.6,<0.2"
prism = ">=0.1.7,<0.2"
28 changes: 9 additions & 19 deletions examples/printer/printer.mojo
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
from memory import Arc
from prism import Command, Context, exact_args
from mist import Style
import mist


fn printer(ctx: Context) -> None:
fn printer(ctx: Context) raises -> None:
if len(ctx.args) <= 0:
print("No text to print! Pass in some text as a positional argument.")
return None

var color = ctx.command[].flags.get_uint32("color")
var formatting = ctx.command[].flags.get_string("formatting")
var style = Style()

if not color:
color = 0xFFFFFF
if not formatting:
formatting = String("")

if color:
style = style.foreground(color.value())
var style = mist.Style().foreground(color)

var formatting_value = formatting.or_else("")
if formatting_value == "":
if formatting == "":
print(style.render(ctx.args[0]))
return None

if formatting.value() == "bold":
if formatting == "bold":
style = style.bold()
elif formatting.value() == "underline":
elif formatting == "underline":
style = style.underline()
elif formatting.value() == "italic":
elif formatting == "italic":
style = style.italic()

print(style.render(ctx.args[0]))
Expand All @@ -49,8 +39,8 @@ fn post_hook(ctx: Context) -> None:
fn main() -> None:
var root = Command(
name="printer",
description="Base command.",
run=printer,
usage="Base command.",
raising_run=printer,
pre_run=pre_hook,
post_run=post_hook,
)
Expand Down
2 changes: 1 addition & 1 deletion examples/requests/mojoproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ version = "0.1.0"

[dependencies]
max = ">=24.5.0,<25"
prism = ">=0.1.6,<0.2"
prism = ">=0.1.7,<0.2"
requests = ">=2.32.3,<3"
16 changes: 8 additions & 8 deletions examples/requests/nested.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn print_information(ctx: Context) -> None:
fn get_cat_fact(ctx: Context) raises -> None:
var flags = ctx.command[].flags
var lover = flags.get_bool("lover")
if lover and lover.value():
if lover:
print("Hello fellow cat lover!")

var requests = Python.import_module("requests")
Expand All @@ -29,7 +29,7 @@ fn get_cat_fact(ctx: Context) raises -> None:
if not count:
raise Error("Count flag was not found.")

for _ in range(count.value()):
for _ in range(count):
var response = requests.get(url)

# Check if the request was successful (status code 200)
Expand Down Expand Up @@ -57,21 +57,21 @@ fn get_dog_breeds(ctx: Context) raises -> None:


fn main() -> None:
var root = Command(name="nested", description="Base command.", run=base)
var root = Command(name="nested", usage="Base command.", run=base)

var get_command = Arc(
Command(
name="get",
description="Base command for getting some data.",
usage="Base command for getting some data.",
run=print_information,
)
)

var cat_command = Arc(
Command(
name="cat",
description="Get some cat facts!",
erroring_run=get_cat_fact,
usage="Get some cat facts!",
raising_run=get_cat_fact,
)
)
cat_command[].flags.int_flag(name="count", shorthand="c", usage="Number of facts to get.", default=1)
Expand All @@ -80,8 +80,8 @@ fn main() -> None:
var dog_command = Arc(
Command(
name="dog",
description="Get some dog breeds!",
erroring_run=get_dog_breeds,
usage="Get some dog breeds!",
raising_run=get_dog_breeds,
)
)

Expand Down
18 changes: 9 additions & 9 deletions examples/requests/persistent_flags.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn print_information(ctx: Context) -> None:
fn get_cat_fact(ctx: Context) raises -> None:
var flags = ctx.command[].flags
var lover = flags.get_bool("lover")
if lover and lover.value():
if lover:
print("Hello fellow cat lover!")

var requests = Python.import_module("requests")
Expand All @@ -29,7 +29,7 @@ fn get_cat_fact(ctx: Context) raises -> None:
if not count:
raise Error("Count flag was not found.")

for _ in range(count.value()):
for _ in range(count):
var response = requests.get(url)

# Check if the request was successful (status code 200)
Expand All @@ -43,7 +43,7 @@ fn get_cat_fact(ctx: Context) raises -> None:
fn get_dog_breeds(ctx: Context) raises -> None:
var flags = ctx.command[].flags
var lover = flags.get_bool("lover")
if lover and lover.value():
if lover:
print("Hello fellow dog lover!")

var requests = Python.import_module("requests")
Expand All @@ -69,12 +69,12 @@ fn post_hook(ctx: Context) -> None:


fn main() -> None:
var root = Command(name="nested", description="Base command.", run=base)
var root = Command(name="nested", usage="Base command.", run=base)

var get_command = Arc(
Command(
name="get",
description="Base command for getting some data.",
usage="Base command for getting some data.",
run=print_information,
persistent_pre_run=pre_hook,
persistent_post_run=post_hook,
Expand All @@ -85,17 +85,17 @@ fn main() -> None:
var cat_command = Arc(
Command(
name="cat",
description="Get some cat facts!",
erroring_run=get_cat_fact,
usage="Get some cat facts!",
raising_run=get_cat_fact,
)
)
cat_command[].flags.int_flag(name="count", shorthand="c", usage="Number of facts to get.")

var dog_command = Arc(
Command(
name="dog",
description="Get some dog breeds!",
erroring_run=get_dog_breeds,
usage="Get some dog breeds!",
raising_run=get_dog_breeds,
)
)

Expand Down
18 changes: 9 additions & 9 deletions scripts/examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ magic run mojo build $TEMP_DIR/arg_validators.mojo -o $TEMP_DIR/validators
echo "[INFO] Running examples..."
# Need to run these first examples as part of a mojo project as they have external dependencies.
# printer is a portable binary, but nested and persistent_flags are not because they depend on a python library.
cd examples/printer
magic run mojo build printer.mojo
./printer "sample-text" --formatting=underline
# cd examples/printer
# magic run mojo build printer.mojo
# ./printer "sample-text" --formatting=underline

cd ../requests
magic run mojo build nested.mojo
magic run nested get cat --count 3 -l
magic run mojo build persistent_flags.mojo -o persistent
magic run persistent get cat --count 2 --lover
magic run persistent get dog
# cd ../requests
# magic run mojo build nested.mojo
# magic run nested get cat --count 3 -l
# magic run mojo build persistent_flags.mojo -o persistent
# magic run persistent get cat --count 2 --lover
# magic run persistent get dog

cd ../..

Expand Down
4 changes: 2 additions & 2 deletions src/prism/__init__.mojo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .command import (
Command,
CommandFunction,
ArgValidator,
CmdFn,
ArgValidatorFn,
)
from .args import no_args, valid_args, arbitrary_args, minimum_n_args, maximum_n_args, exact_args, range_args, match_all
from .flag import Flag
Expand Down
Loading

0 comments on commit 60cade3

Please sign in to comment.