Coder Social home page Coder Social logo

Splitting commands up? about cri HOT 5 CLOSED

rchady avatar rchady commented on July 26, 2024
Splitting commands up?

from cri.

Comments (5)

denisdefreyne avatar denisdefreyne commented on July 26, 2024

Hi,

The approach that I use in Nanoc is the following.

To define a command, read it and call .define with the code and filename (the filename is useful because it ensures any back traces are sensible):

code = File.read(filename, encoding: 'UTF-8')
cmd = Cri::Command.define(code, filename)

I realised that .define used this way is not documented — will fix.

You can infer the name of the command from the filename, if you wish:

command_name = File.basename(filename, '.rb')
cmd.modify { name command_name }

You can stick that code in a helper function e.g. load_command, and then use it as follows to load subcommands:

root_cmd = load_command('commands/nanoc.rb')
root_cmd.add_cmd(load_command('commands/compile.rb'))
root_cmd.add_cmd(load_command('commands/view.rb'))
root_cmd.add_cmd(load_command('commands/deploy.rb'))

You can additionally use something like Dir[…] to remove the repetition, if you want.

Does this solve your concern?

from cri.

denisdefreyne avatar denisdefreyne commented on July 26, 2024

The upcoming release of Cri will add .load_file, so you’ll be able to do

root_cmd = Cri::Command.load_file('commands/nanoc.rb')
root_cmd.add_command(Cri::Command.load_file('commands/comile.rb'))
root_cmd.add_command(Cri::Command.load_file('commands/view.rb'))
root_cmd.add_command(Cri::Command.load_file('commands/check.rb'))

It is a convenient wrapper around Cri::Command.define with a string containing code.

from cri.

denisdefreyne avatar denisdefreyne commented on July 26, 2024

… and an infer_name option as well, e.g.

cmd = Cri::Command.load_file('commands/check.rb', infer_name: true)

cmd.name will be check.

from cri.

denisdefreyne avatar denisdefreyne commented on July 26, 2024

This is now released in v2.14.0.

from cri.

rchady avatar rchady commented on July 26, 2024

Perfect, thanks!

from cri.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.