Jacob v0.1.1 Jacob.Command.Concerns.HasInitializers behaviour View Source

Provides the “initializers” functionnality to your commands.

This module is automatically used by your commands when using Jacob.Command

Link to this section Summary

Callbacks

This callback will be used by Jacob in order to initialize the argv

Link to this section Functions

Link to this macro add_initializer(initializer, options \\ nil) View Source (macro)

Register an initializer.

The first argument may either be a module or a tuple containing a module folowed by a function name.

If you only provide a module name this module has to define an initializer function which is __jacob_initializer__/2

every initializer will receive two arguments. The first one being the argv the second one being the options or nil in case no options have been set.

Examples

Module initializer

add_initializer(MyModule)

Module initializer with options

add_initializer(MyModule, %{some: options})

Module function initializer with options

add_initializer({MyModule, :my_function}, %{some: options})

Link to this section Callbacks

Link to this callback initialize(argv) View Source
initialize(argv :: list()) :: any()

This callback will be used by Jacob in order to initialize the argv.

It will remove the first argument in the argv list as it always is the command name.

e.g. When calling ./jacob foo bar the argv are ["foo", "bar"] and Jacob turns that into ["bar"]

Then the modified argv will be sent through all the initializer that your command defines.

You may define an initializer by anotating an existing funtion with the @initializer attribute, or by using the add_initializer/2 macro.

Initializers have to be defined as public functions.

Examples

Anotation without options

@initializer true
def my_initializer(argv, _opts), do: argv

Anotation with options

@initializer %{my: option}
def my_initializer(argv, opts = %{my: option}), do: argv