Skip to content

Commit 5a6066c

Browse files
authored
Inject help commands and flags into the CLI spec (#22)
* bump versions * wip * feat: help flags * fix
1 parent 13fab22 commit 5a6066c

File tree

10 files changed

+573
-196
lines changed

10 files changed

+573
-196
lines changed

examples/escript/example.ex

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ defmodule Escript.Example do
1313
from the `main/1` escript funciton, as can seen below.
1414
"""
1515

16-
use Nexus.CLI
16+
use Nexus.CLI, otp_app: :nexus_cli
1717

18-
defcommand :foo do
18+
defcommand :echo do
1919
description "Command that receives a string as argument and prints it."
2020

2121
value :string, required: true
@@ -24,7 +24,7 @@ defmodule Escript.Example do
2424
defcommand :fizzbuzz do
2525
description "Fizz bUZZ"
2626

27-
value {:enum, ~w(fizz buzz)a}, required: true
27+
value :integer, required: true
2828
end
2929

3030
defcommand :foo_bar do
@@ -47,27 +47,28 @@ defmodule Escript.Example do
4747
def version, do: "0.1.0"
4848

4949
@impl true
50-
def handle_input(:foo, input) do
51-
IO.puts(inspect(input))
50+
def handle_input(:echo, %{value: value}) do
51+
IO.puts(value)
5252
end
5353

54-
def handle_input(:fizzbuzz, %{value: :fizz}) do
55-
IO.puts("buzz")
56-
end
57-
58-
def handle_input(:fizzbuzz, %{value: :buzz}) do
59-
IO.puts("fizz")
54+
def handle_input(:fizzbuzz, %{value: value}) when is_integer(value) do
55+
cond do
56+
rem(value, 3) == 0 and rem(value, 5) == 0 -> IO.puts("fizzbuzz")
57+
rem(value, 3) == 0 -> IO.puts("fizz")
58+
rem(value, 5) == 0 -> IO.puts("buzz")
59+
true -> IO.puts value
60+
end
6061
end
6162

62-
def handle_input(:foo_bar, %{value: _, subcommand: :foo}) do
63-
# do something wth "foo" value
63+
def handle_input([:foo_bar, :foo], %{value: _}) do
64+
IO.puts("Issued foo")
6465
:ok
6566
end
6667

67-
def handle_input(:foo_bar, %{value: _, subcommand: :bar}) do
68-
# do something wth "bar" value
68+
def handle_input([:foo_bar, :bar], %{value: _}) do
69+
IO.puts("Issued bar")
6970
:ok
7071
end
7172

72-
# defdelegate main(args), to: __MODULE__, as: :run
73+
defdelegate main(args), to: __MODULE__, as: :execute
7374
end

examples/file_management.ex

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,32 @@ defmodule MyCLI do
55

66
use Nexus.CLI, otp_app: :nexus_cli
77

8+
defcommand :version do
9+
description "Shows the version of the CLI"
10+
end
11+
12+
defcommand :folder do
13+
description "Performs folder operations like merging"
14+
15+
subcommand :merge do
16+
description "Merges two or more directories"
17+
18+
value {:list, :string}, required: true, as: :targets
19+
20+
flag :level do
21+
description "The level of the folder that will be merged"
22+
value :integer, required: false
23+
short :l
24+
end
25+
26+
flag :recursive do
27+
description "IF the merge should operate recursively"
28+
value :boolean, required: false, default: false
29+
short :rc
30+
end
31+
end
32+
end
33+
834
defcommand :file do
935
description "Performs file operations such as copy, move, and delete."
1036

@@ -69,6 +95,28 @@ defmodule MyCLI do
6995
end
7096

7197
@impl Nexus.CLI
98+
def handle_input(:version, _) do
99+
# `version/0` comes from Nexus.CLI or the callback this module defined
100+
vsn = version()
101+
IO.puts(vsn)
102+
end
103+
104+
def handle_input([:folder, :merge], %{args: args, flags: flags}) do
105+
if flags.recursive do
106+
IO.puts("Recursive merging enabled")
107+
end
108+
109+
if level = flags.level do
110+
IO.puts("Set level of merging to #{level}")
111+
end
112+
113+
Enum.each(args.targets, fn target ->
114+
IO.puts("Merged #{target}")
115+
end)
116+
117+
:ok
118+
end
119+
72120
def handle_input([:file, :copy], %{args: args, flags: flags}) do
73121
if flags.verbose do
74122
IO.puts("Copying from #{args.source} to #{args.dest}")
@@ -117,11 +165,4 @@ defmodule MyCLI do
117165

118166
:ok
119167
end
120-
121-
def handle_input(command, input) do
122-
IO.puts("Unknown command or invalid parameters")
123-
IO.inspect(command, label: "CMD")
124-
IO.inspect(input, label: "INPUT")
125-
:error
126-
end
127168
end

examples/mix/tasks/example.ex

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
defmodule Mix.Tasks.Example do
22
@moduledoc """
33
This is a Mix Task example using Nexus.
4-
Basically, you can `use` both `Mix.Task` and `Nexus`
4+
Basically, you can `use` both `Mix.Task` and `Nexus.CLI`
55
modules, define your commands as usual with `defcommand/2`
66
and implement others callbacks.
77
8-
Then you need to call `Nexus.parse/0`, that will inject
9-
both `parse/1` and `run/1` functions into your module.
108
In a `Mix.Task` module, the `run/1` function will supply
119
the behaviour, so you don't need to define it yourself.
1210
@@ -16,9 +14,12 @@ defmodule Mix.Tasks.Example do
1614
"""
1715

1816
use Mix.Task
19-
use Nexus
17+
use Nexus.CLI, otp_app: :nexus_cli
2018

21-
defcommand :foo, type: :string, required: false, default: "bar", doc: "This is a foo command"
19+
defcommand :foo do
20+
description "This is a foo command"
21+
value :string, required: false, default: "bar"
22+
end
2223

2324
@impl Nexus.CLI
2425
def version, do: "0.1.0"
@@ -31,6 +32,6 @@ defmodule Mix.Tasks.Example do
3132
IO.puts("Running :foo command...")
3233
end
3334

34-
Nexus.help()
35-
Nexus.parse()
35+
@impl Mix.Task
36+
defdelegate run(argv), to: __MODULE__, as: :execute
3637
end

flake.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
mkShell {
3030
name = "nexus";
3131
packages = with pkgs;
32-
[beam.elixir_1_17]
32+
[beam.elixir_1_17 erlang_27]
3333
++ lib.optional stdenv.isLinux [inotify-tools]
3434
++ lib.optional stdenv.isDarwin [
3535
darwin.apple_sdk.frameworks.CoreServices

0 commit comments

Comments
 (0)