Glyph - Defining Custom Commands

Glyph relies on GLI for defining commands. This useful library provides a high-level framework for creating command-line interface similar to Git, its DSL takes care of pretty much everything, from managing command line arguments and options to providing an interactive help system.

Creating a 'glyph generate' command

Consider the custom task defined in Creating a ‘custom:generate’ task. Creating a custom command to call it is fairly straightforward.

First of all, create a lib/commands folder in your project directory. Then, create a .rb file within it, e.g. commands.rake.

Finally, here’s the source of the command:

 1GLI.desc 'Generates a specific file required for Glyph releases'
 2arg_name "file_name"
 3command :generate do |c|
 4  c.action do |global_options,options,args|
 5    if args.blank? then
 6      raise RuntimeError, "You must specify a file to generate"
 7    else
 8      Glyph.run 'custom:generate', args[0]
 9    end
10  end
11end

That’s it. If you try to run glyph help within your project directory, notice that there’s a new entry for the generate command:

 1$ glyph help
 2=====================================
 3Glyph v/0.5.3.1
 4=====================================
 5usage: glyph command [options]
 6
 7Options:
 8    -d, --debug - Enable debugging
 9
10Commands:
11    add      - Add a new text file to the project
12    compile  - Compile the project
13    config   - Get/set configuration settings
14    generate - Generates a specific file required for Glyph releases
15    help     - Shows list of commands or help for one command
16    init     - Create a new Glyph project
17    outline  - Display the document outline
18    stats    - Display statistics
19    todo     - Display all project TODO items

You can now run the Glyph command as expected:

1$ glyph -d generate changelog
2-- Generating CHANGELOG...
3-- Done.