jekyll/mercenary

Name: mercenary

Owner: Jekyll

Description: An easier way to build your command-line scripts in Ruby.

Created: 2013-11-06 19:45:55.0

Updated: 2018-01-16 09:07:16.0

Pushed: 2018-01-03 19:06:15.0

Homepage:

Size: 102

Language: Ruby

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Mercenary

Lightweight and flexible library for writing command-line apps in Ruby.

Build Status

Installation

Add this line to your application's Gemfile:

gem 'mercenary'

And then execute:

$ bundle

Or install it yourself as:

$ gem install mercenary

Note: Mercenary may not work with Ruby < 1.9.3.

Usage

Creating programs and commands with Mercenary is easy:

enary.program(:jekyll) do |p|
version Jekyll::VERSION
description 'Jekyll is a blog-aware, static site generator in Ruby'
syntax "jekyll <subcommand> [options]"

command(:new) do |c|
c.syntax "new PATH" # do not include the program name or super commands
c.description "Creates a new Jekyll site scaffold in PATH"
c.option 'blank', '--blank', 'Initialize the new site without any content.'

c.action do |args, options|
  Jekyll::Commands::New.process(args, blank: options['blank'])
end
d

command(:build) do |c|
c.syntax "build [options]"
c.description "Builds your Jekyll site"

c.option 'safe', '--safe', 'Run in safe mode'
c.option 'source', '--source DIR', 'From where to collect the source files'
c.option 'destination', '--dest DIR', 'To where the compiled files should be written'

c.action do |_, options|
  Jekyll::Commands::Build.process(options)
end
d

Bring in command bundled in external gem
gin
require "jekyll-import"
JekyllImport.init_with_program(p)
scue LoadError
d

default_command(:build)

All commands have the following default options:

API
Mercenary
.program

Creates and executes a program. Accepts two arguments:

Example is above, under the heading Usage.

Program

Program is a subclass of Command, so it has all of the methods documented below as well as those for Command.

#config

Fetches the program configuration hash.

Command
#new

Create a new command. Accepts one argument:

#version

Sets or gets the version of the command. Accepts an optional argument:

#syntax

Sets or gets the syntax of the command. Built on parent syntaxes if a parent exists. Accepts one optional argument:

When a parent command exists, say supercommand, with syntax set as supercommand <SUBCOMMAND> [OPTIONS], the syntax of the command in question will be supercommand command_name <SUBCOMMAND> [OPTIONS] with both <SUBCOMMAND> and [OPTIONS] stripped out. Any text between < and > or between [ and ] will be stripped from parent command syntaxes. The purpose of this chaining is to reduce redundancy.

#description

Sets or gets the description of the command. Accepts one optional argument:

#default_command

Sets or gets the default subcommand of the command to execute in the event no subcommand is passed during execution. Accepts one optional argument:

#option

Adds a new option to the command. Accepts many arguments:

Valid option calls:

option 'config_key', '-c', 'Sets the "config" flag'
option 'config_key', '--config', 'Sets the "config" flag'
option 'config_key', '-c', '--config', 'Sets the "config" flag.'
option 'config_key', '-c FILE', '--config FILE', 'The config file.'
option 'config_key', '-c FILE1[,FILE2[,FILE3...]]', '--config FILE1[,FILE2[,FILE3...]]', Array, 'The config files.'

Notice that you can specify either a short switch, a long switch, or both. If you want to accept an argument, you have to specify it in the switch strings. The class of the argument defaults to String, but you can optionally set a different class to create, e.g. Array, if you are expecting a particular class in your code from this option's value. The description is also optional, but it's highly recommended to include a description.

#alias

Specifies an alias for this command such that the alias may be used in place of the command during execution. Accepts one argument:

Example:

alias(:my_alias)
w `cmd` is now also executable via "my_alias"
#action

Specifies a block to be executed in the event the command is specified at runtime. The block is given two arguments:

Note that actions are additive, meaning any new call to #action will result in another action to be executed at runtime. Actions will be executed in the order they are specified in.

Example:

action do |args, options|
do something!

#logger

Access the logger for this command. Useful for outputting information to STDOUT. Accepts one optional argument:

Log level defaults to Logger::INFO.

Examples:

logger(Logger::DEBUG)
logger.debug "My debug message."
logger.info "My informative message."
logger.warn "ACHTUNG!!"
logger.error "Something terrible has happened."
logger.fatal "I can't continue doing what I'm doing."
#command

Creates a new subcommand for the current command. Accepts two arguments:

Example:

ommand.command(:my_subcommand) do |subcmd|
bcmd.description 'My subcommand'
bcmd.syntax 'my_subcommand [OPTIONS]'
...

Contributing
  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.