Parse

This is the frontend @cast and @main of Comonicon.

The @main command will use generate a few functions in the module:

  1. the entry function for CLI command_main.
  2. comonicon_install: for command build and installation.
  3. comonicon_install_path: for path build and installation.
  4. julia_main: for building standalone applications.

References

Comonicon.Parse.cachefileFunction
cache_file([file=Base.PROGRAM_FILE])

Return the files that will be cached:

  1. cmd.jl: a julia script that contains the generated CLI code.
  2. checksum: a checksum file for checking if the generated CLI code matches the original file.
source
Comonicon.Parse.commandFunction
command(module; name="", doc=docstring(m))

Convert a module to a CLI command NodeCommand.

command(f::Function, args, kwargs; name="")

Convert a function to a CLI command LeafCommand. It requires a Vector of function arguments and a Vector of function kwargs.

The element of arguments vector is a tuple (name, type, require):

  • name::String is the name of argument
  • type::DataType is the type of this argument, can be Any if you don't want to specify
  • require::Bool indicates the whether this argument is required.
  • vararg::Bool indicates whether this argument is a vararg.

The element of kwargs vector is also a tuple (name, type, short):

  • name::String is the name of kwarg
  • type::DataType is the type of this kwarg, can be Any if you don't want to specify
  • short::Bool is a flag that indicates whether this kwarg has short option.
source
Comonicon.Parse.get_versionMethod
get_version(m::Module)

Get the version of a given module. It will try to find the version of Project.toml if the given module is a project module. If fails, it returns v"0.0.0".

source
Comonicon.Parse.set_cmd!Function
set_cmd!(cmds::Dict, cmd)

register cmd in the command registry cmds, which usually is a constant CASTED_COMMANDS under given module.

source
Comonicon.Parse.@castMacro
@cast <function expr>
@cast <module expr>
@cast <function name>
@cast <module name>

Cast a Julia object to a command object. @cast will always execute the given expression, and only create and register an command object via command after analysing the given expression.

Example

The most basic way is to use @cast on a function as your command entry.

@cast function your_command(arg1, arg2::Int)
    # your processing code
end

This will create a LeafCommand object and register it to the current module's CASTED_COMMANDS constant. You can access this object via MODULE_NAME.CASTED_COMMANDS["your_command"].

Note this will not create a functional CLI, to create a function CLI you need to create an entry point, which can be declared by @main.

source
Comonicon.Parse.@mainMacro
@main <function expr>
@main

Create an EntryCommand and use it as the entry of the entire CLI. If you only have one function to cast to a CLI, you can use @main instead of @cast so it will create both the command object and the entry.

source