Credo.Plugin.register_command
You're seeing just the function
register_command
, go back to Credo.Plugin module for more information.
Registers and initializes a Command module with a given name
.
Add new commands
Commands are just modules with a call function and adding new commands is easy.
# credo_demo_plugin.ex
defmodule CredoDemoPlugin do
import Credo.Plugin
def init(exec) do
register_command(exec, "demo", CredoDemoPlugin.DemoCommand)
end
end
# credo_demo_plugin/demo_command.ex
defmodule CredoDemoPlugin.DemoCommand do
alias Credo.CLI.Output.UI
alias Credo.Execution
def call(exec, _) do
castle = Execution.get_plugin_param(exec, CredoDemoPlugin, :castle)
UI.puts("By the power of #{castle}!")
exec
end
end
Users can use this command by typing
$ mix credo demo
By the power of !
Override an existing command
Since commands are just modules with a call function, overriding existing commands is easy.
defmodule CredoDemoPlugin do
import Credo.Plugin
def init(exec) do
register_command(exec, "explain", CredoDemoPlugin.MyBetterExplainCommand)
end
end
This example would have the effect that typing mix credo lib/my_file.ex:42
would no longer run the built-in Explain
command, but rather our plugin's MyBetterExplain
command.