Skip to content

Commit

Permalink
correctly builds a CLI struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoey de Souza Pessanha committed Aug 26, 2023
1 parent ed4682a commit b4283d0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 24 deletions.
18 changes: 2 additions & 16 deletions lib/nexus.ex
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ defmodule Nexus do
"""
defmacro parse do
quote do
defstruct Enum.map(@commands, &{&1.name, nil})

def __commands__, do: @commands

def run(args) do
Expand Down Expand Up @@ -146,22 +148,6 @@ defmodule Nexus do
"""
end

def parse_to(:string, value) do
to_string(value)
end

def parse_to(:atom, value) do
String.to_existing_atom(value)
end

def parse_to(:integer, value) do
String.to_integer(value)
end

def parse_to(:float, value) do
String.to_float(value)
end

def __make_command__!(module, cmd_name, opts) do
opts
|> Keyword.put(:name, cmd_name)
Expand Down
14 changes: 6 additions & 8 deletions lib/nexus/cli.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ defmodule Nexus.CLI do
to be runned and also define helper functions to parse a single
command againts a raw input.
"""
alias Nexus.Command.Input
alias Nexus.Parser

@callback version :: String.t()
@callback banner :: String.t()
Expand All @@ -22,15 +24,11 @@ defmodule Nexus.CLI do

{cli, _raw} =
Enum.reduce(cmds, acc, fn spec, {cli, raw} ->
{:ok, value} = parse_command(spec, raw)
{Map.put(cli, spec.name, value), raw}
{value, raw} = Parser.command_from_raw!(spec, raw)
input = Input.parse!(value, raw)
{Map.put(cli, spec.name, input), raw}
end)

{:ok, cli}
end

def parse_command(spec, raw) do
value = Nexus.parse_to(spec.type, raw)
{:ok, value}
{:ok, struct(module, cli)}
end
end

0 comments on commit b4283d0

Please sign in to comment.