Skip to content

Commit

Permalink
improve UX
Browse files Browse the repository at this point in the history
  • Loading branch information
thatstoasty committed Apr 24, 2024
1 parent e046dd5 commit 97ebcf7
Show file tree
Hide file tree
Showing 7 changed files with 460 additions and 223 deletions.
8 changes: 3 additions & 5 deletions examples/hello_world/root.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ from examples.hello_world.printer import build_printer_command
from memory._arc import Arc


# TODO: Using CommandArc instead of Arc[Command] works. But using Arc[Command] causes a recursive relationship error?
fn test(command: CommandArc, args: List[String]) -> None:
for item in command[].get_all_flags()[].flags:
print(item[].name, item[].value.value())
for item in command[].flag_list():
print(item[][].name, item[][].value.value())

return None

Expand All @@ -24,8 +23,7 @@ fn init() -> None:
description="This is a dummy command!",
run=test,
)

root_command.flags.add_string_flag[name="env", shorthand="e", usage="Environment."]()
root_command.add_string_flag(name="env", shorthand="e", usage="Environment.")

var say_command = build_say_command()
var hello_command = build_hello_command()
Expand Down
6 changes: 2 additions & 4 deletions examples/logging/root.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ from examples.logging.log import logger, default_logger, json_logger


fn handler(command: CommandArc, args: List[String]) -> None:
var print_type = command[].get_all_flags()[].get_as_string("type").value()
var print_type = command[].flags[].get_as_string("type").value()
if print_type == "json":
for arg in args:
json_logger.info(arg[])
Expand All @@ -21,9 +21,7 @@ fn init() -> None:
var root_command = Command(
name="logger", description="Base command.", run=handler, arg_validator=minimum_n_args[1]()
)
root_command.flags.add_string_flag[
name="type", shorthand="t", usage="Formatting type: [json, custom]", default="json"
]()
root_command.add_string_flag(name="type", shorthand="t", usage="Formatting type: [json, custom]", default="json")

root_command.execute()

Expand Down
6 changes: 3 additions & 3 deletions examples/nested/nested.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn print_information(command: CommandArc, args: List[String]) -> None:


fn get_cat_fact(command: CommandArc, args: List[String]) -> Error:
var flags = command[].get_all_flags()[]
var flags = command[].flags[]
var lover = flags.get_as_bool("lover")
if lover and lover.value():
print("Hello fellow cat lover!")
Expand Down Expand Up @@ -77,8 +77,8 @@ fn init() -> None:
description="Get some cat facts!",
erroring_run=get_cat_fact,
)
cat_command.flags.add_int_flag[name="count", shorthand="c", usage="Number of facts to get."]()
cat_command.flags.add_bool_flag[name="lover", shorthand="l", usage="Are you a cat lover?"]()
cat_command.add_int_flag(name="count", shorthand="c", usage="Number of facts to get.")
cat_command.add_bool_flag(name="lover", shorthand="l", usage="Are you a cat lover?")

var dog_command = Command(
name="dog",
Expand Down
15 changes: 8 additions & 7 deletions examples/printer/printer.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ fn printer(command: CommandArc, args: List[String]) -> None:
print("No text to print! Pass in some text as a positional argument.")
return None

var flags = command[].get_all_flags()[]
var color = flags.get_as_string("color")
var formatting = flags.get_as_string("formatting")
var color = command[].flags[].get_as_string("color")
var formatting = command[].flags[].get_as_string("formatting")
var style = TerminalStyle()

if not color:
Expand Down Expand Up @@ -43,15 +42,17 @@ fn post_hook(command: CommandArc, args: List[String]) -> None:

fn init() -> None:
var start = now()
var root_command = Command.new[
var root_command = Command(
name="printer",
description="Base command.",
run=printer,
pre_run=pre_hook,
post_run=post_hook,
](arg_validator=exact_args[1]())
root_command.flags.add_string_flag[name="color", shorthand="c", usage="Text color", default="#3464eb"]()
root_command.flags.add_string_flag[name="formatting", shorthand="f", usage="Text formatting"]()
arg_validator=exact_args[1](),
)

root_command.add_string_flag(name="color", shorthand="c", usage="Text color", default="#3464eb")
root_command.add_string_flag(name="formatting", shorthand="f", usage="Text formatting")

root_command.execute()
print("duration", (now() - start) / 1e9)
Expand Down
8 changes: 4 additions & 4 deletions examples/read_csv/root.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ from os.path import exists


fn handler(command: CommandArc, args: List[String]) -> Error:
var file_path = command[].get_all_flags()[].get_as_string("file").value()
var file_path = command[].flags[].get_as_string("file").value()
if not exists(file_path):
return Error("File does not exist.")

try:
var file = FileWrapper(file_path, "r")
var reader = CSVReader(file^)
var lines = command[].get_all_flags()[].get_as_int("lines").value()
var lines = command[].flags[].get_as_int("lines").value()
var csv = reader.read_lines(lines, "\n", 3)
for i in range(csv.row_count()):
print(csv.get(i, 0))
Expand All @@ -25,8 +25,8 @@ fn init() -> None:
var root_command = Command(
name="read_csv", description="Base command.", erroring_run=handler, arg_validator=no_args
)
root_command.flags.add_string_flag[name="file", shorthand="f", usage="CSV file to read."]()
root_command.flags.add_int_flag[name="lines", shorthand="l", usage="Lines to print.", default=3]()
root_command.add_string_flag(name="file", shorthand="f", usage="CSV file to read.")
root_command.add_int_flag(name="lines", shorthand="l", usage="Lines to print.", default=3)

root_command.execute()

Expand Down
Loading

0 comments on commit 97ebcf7

Please sign in to comment.