Parse
This is the frontend @cast and @main of Comonicon.
The @main command will use generate a few functions in the module:
- the entry function for CLI
command_main. comonicon_install: for command build and installation.comonicon_install_path: for path build and installation.julia_main: for building standalone applications.
References
Comonicon.Parse.cachedir — Functioncachedir([file=Base.PROGRAM_FILE])Return the cache directory.
Comonicon.Parse.cachefile — Functioncache_file([file=Base.PROGRAM_FILE])Return the files that will be cached:
cmd.jl: a julia script that contains the generated CLI code.checksum: a checksum file for checking if the generated CLI code matches the original file.
Comonicon.Parse.command — Functioncommand(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::Stringis the name of argumenttype::DataTypeis the type of this argument, can beAnyif you don't want to specifyrequire::Boolindicates the whether this argument is required.vararg::Boolindicates whether this argument is a vararg.
The element of kwargs vector is also a tuple (name, type, short):
name::Stringis the name of kwargtype::DataTypeis the type of this kwarg, can beAnyif you don't want to specifyshort::Boolis a flag that indicates whether this kwarg has short option.
Comonicon.Parse.create_cache — Functioncreate_cache(cmd[, file=Base.PROGRAM_FILE])Create cache for given command cmd at file location file.
Comonicon.Parse.disable_cache — Methoddisable_cache()Disable command compile cache. See also enable_cache.
Comonicon.Parse.enable_cache — Methodenable_cache()Enable command compile cache. See also disable_cache.
Comonicon.Parse.get_version — Methodget_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".
Comonicon.Parse.iscached — Functioniscached([file=Base.PROGRAM_FILE])Check if the given file is cached or not.
Comonicon.Parse.read_doc — Methodread_doc(markdown)Read CLI documentation from markdown format doc strings.
Comonicon.Parse.rm_format — Functionrm_format(md)Remove Markdown DOM and flatten to strings.
Comonicon.Parse.set_cmd! — Functionset_cmd!(cmds::Dict, cmd)register cmd in the command registry cmds, which usually is a constant CASTED_COMMANDS under given module.
Comonicon.Parse.@cast — Macro@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
endThis 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.
Comonicon.Parse.@main — Macro@main <function expr>
@mainCreate 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.