Credo.Plugin.register_cli_switch
You're seeing just the function
register_cli_switch
, go back to Credo.Plugin module for more information.
Link to this function
register_cli_switch(exec, name, type, alias_name \\ nil, convert_to_param \\ true)
View SourceAdds a CLI switch to Credo.
For demo purposes, we are writing a command called demo
(see register_command/3
):
# credo_demo_plugin/demo_command.ex
defmodule CredoDemoPlugin do
import Credo.Plugin
def init(exec) do
exec
|> register_command("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
Since Plugins can be configured by params in .credo.exs
, we can add the :castle
param:
# .credo.exs
{CredoDemoPlugin, [castle: "Grayskull"]}
And get the following output:
$ mix credo demo
By the power of Grayskull!
Plugins can provide custom CLI options as well, so we can do something like:
$ mix credo demo --castle Winterfell
Unknown switch: --castle
Registering a custom CLI switch for this is easy:
defmodule CredoDemoPlugin do
import Credo.Plugin
def init(exec) do
exec
|> register_command("demo", CredoDemoPlugin.DemoCommand)
|> register_cli_switch(:castle, :string, :X)
end
end
Every registered CLI switch is automatically converted into a plugin param of the same name, which is why we get the following output:
$ mix credo demo --castle Winterfell
By the power of Winterfell!
$ mix credo demo -X Camelot
By the power of Camelot!
Plugin authors can also provide a function to control the plugin param's name and value more granularly:
defmodule CredoDemoPlugin do
import Credo.Plugin
def init(exec) do
register_cli_switch(exec, :kastle, :string, :X, fn(switch_value) ->
{:castle, String.upcase(switch_value)}
end)
end
end
And get the following output:
$ mix credo demo --kastle Winterfell
By the power of WINTERFELL!