Parse
This is the frontend @cast
and @main
of Comonicon
.
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::String
is the name of argumenttype::DataType
is the type of this argument, can beAny
if you don't want to specifyrequire::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 kwargtype::DataType
is the type of this kwarg, can beAny
if you don't want to specifyshort::Bool
is 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.default_name
— Methoddefault_name(x)
Return the lowercase of nameof(x)
in String
.
Comonicon.Parse.default_name
— Methoddefault_name(x)
Return the lowercase of a given package name. It will ignore the suffix if it ends with ".jl".
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
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
.
Comonicon.Parse.@main
— Macro@main <function expr>
@main [options...]
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.
If you have declared commands via @cast
, you can create the entry via @main [options...]
, available options are:
name
: default is the current module name in lowercase.version
: default is the current project version orv"0.0.0"
. If it'sv"0.0.0"
, the version will not be printed.doc
: a description of the entry command.