EN | JA
Magni is a framework for developing CLI tools with mruby. It includes the necessary files for building CLI tools with mruby, allowing you to build CLI tools with minimal configuration. It also provides an API similar to Thor, a well-known CLI framework for CRuby, so if you have experience with that tool, you can develop efficiently.
- Lightweight CLI framework running on mruby
- Thor-like API
- Includes code necessary for building CLI tools
A C compiler is required to build mruby. See the official mruby guide for details.
Create a project directory with any directory name. This directory name will be the executable file name after build by default.
mkdir mycli
cd mycliGet the mruby source code using one of the following methods.
git clone https://github.com/mruby/mruby.git
# or git submodule (recommended)
git submodule add https://github.com/mruby/mruby.gitDownload and extract the archive from the official mruby download page.
Create build_config.rb in the project root and add the Magni gem.
# build_config.rb
MRuby::Build.new do |conf|
conf.toolchain
conf.gembox 'default'
conf.gem github: 'buty4649/magni', branch: 'main'
conf.build_dir = File.join(__dir__, 'build')
endCreate a script. The name is arbitrary, but here we use cli.rb as an example. The script must define one class that inherits from the Magni class (here we use the Cli class).
# cli.rb
class Cli < Magni
package_name 'mycli'
desc 'hello', 'say hello'
def hello
puts 'hello'
end
endExecute the following in the project root.
rake -f mruby/RakefileA binary named ./build/bin/mycli will be generated (if the directory name is mycli).
$ ./build/bin/mycli
Usage:
mycli [command]
Commands:
hello say hello
help show this message
Options:
-h, --help show this message and exitYou can change the output binary file name by specifying bins when loading magni as follows. In the following example, it is changed to awesome-cli-tool.
MRuby::Build.new do |conf|
-- snip --
conf.gem github: 'buty4649/magni', branch: 'main' do |g|
g.bins = %w[awesome-cli-tool]
end
-- snip --
endIn mruby, require / require_relative cannot be used by default. However, all script files are loaded and built at build time. The files to be built are as follows:
./*.rb./app/**/*.rb./lib/**/*.rb./mrblib/**/*.rb
Tip
These paths are relative paths based on the directory where build_config.rb is located.
See the LICENSE file for details.