• Stars
    star
    150
  • Rank 247,323 (Top 5 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 11 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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:

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

  p.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
  end

  p.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
  end

  # Bring in command bundled in external gem
  begin
    require "jekyll-import"
    JekyllImport.init_with_program(p)
  rescue LoadError
  end

  p.default_command(:build)
end

All commands have the following default options:

  • -h/--help - show a help message
  • -v/--version - show the program version
  • -t/--trace - show the full backtrace when an error occurs

API

Mercenary

.program

Creates and executes a program. Accepts two arguments:

  • name - program name as a Symbol
  • block - the specification for the program, passed the program instance as an argument.

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:

  • name - the name of your command, as a symbol

#version

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

  • version - (optional) the version to set for the command. If present, this becomes the new version for the command and persists.

#syntax

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

  • syntax - (optional) the syntax to set for the command. Will inherit from the parent commands or program. Usually in the form of "command_name <SUBCOMMAND> [OPTIONS]"

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:

  • desc - (optional) the description to set for the command. If provided, will override any previous description set for the command.

#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:

  • command_name - (optional) the Symbol name of the subcommand to be executed. Raises an ArgumentError if the subcommand doesn't exist. Overwrites previously-set default commands.

#option

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

  • config_key - the configuration key that the value of this option maps to.
  • *options - all the options, globbed, to be passed to OptionParser, namely the switches and the option description. Usually in the format "-s", "--switch", "Sets the 'switch' flag".

Valid option calls:

cmd.option 'config_key', '-c', 'Sets the "config" flag'
cmd.option 'config_key', '--config', 'Sets the "config" flag'
cmd.option 'config_key', '-c', '--config', 'Sets the "config" flag.'
cmd.option 'config_key', '-c FILE', '--config FILE', 'The config file.'
cmd.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:

  • cmd_name - the alias name for this command as a Symbol

Example:

cmd.alias(:my_alias)
# Now `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:

  • args - the non-switch arguments given from the command-line
  • options - the options hash built via the switches passed

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:

cmd.action do |args, options|
  # do something!
end

#logger

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

  • level - (optional) the severity threshold at which to begin logging. Uses Ruby's built-in Logger levels.

Log level defaults to Logger::INFO.

Examples:

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

#command

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

  • cmd_name - the command name, as a Symbol
  • block - the specification of the subcommand in a block

Example:

my_command.command(:my_subcommand) do |subcmd|
  subcmd.description 'My subcommand'
  subcmd.syntax 'my_subcommand [OPTIONS]'
  # ...
end

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

More Repositories

1

jekyll

๐ŸŒ Jekyll is a blog-aware static site generator in Ruby
Ruby
48,894
star
2

minima

Minima is a one-size-fits-all Jekyll theme for writers.
SCSS
3,381
star
3

jekyll-admin

A Jekyll plugin that provides users with a traditional CMS-style graphical interface to author content and administer Jekyll sites.
JavaScript
2,827
star
4

jekyll-seo-tag

A Jekyll plugin to add metadata tags for search engines and social networks to better index and display your site's content.
Ruby
1,655
star
5

jekyll-sitemap

Jekyll plugin to silently generate a sitemaps.org compliant sitemap for your Jekyll site
Ruby
958
star
6

jekyll-feed

๐Ÿ“ A Jekyll plugin to generate an Atom (RSS-like) feed of your Jekyll posts
Ruby
834
star
7

jekyll-redirect-from

๐Ÿ”€ Seamlessly specify multiple redirections URLs for your pages and posts.
Ruby
781
star
8

jekyll-compose

๐Ÿ“ Streamline your writing in Jekyll with these commands.
Ruby
683
star
9

jemoji

GitHub-flavored emoji plugin for Jekyll
Ruby
555
star
10

classifier-reborn

A general classifier module to allow Bayesian and other types of classifications. A fork of cardmagic/classifier.
Ruby
549
star
11

jekyll-import

๐Ÿ“ฅ The "jekyll import" command for importing from various blogs to Jekyll format.
Ruby
512
star
12

jekyll-archives

๐Ÿ“š Archive pages for your Jekyll tags and categories.
Ruby
435
star
13

github-metadata

Jekyll plugin to propagate the `site.github` namespace and set default values for use with GitHub Pages.
Ruby
277
star
14

jekyll-gist

๐Ÿ“ƒ Liquid tag for displaying GitHub Gists in Jekyll sites.
Ruby
265
star
15

jekyll-mentions

๐Ÿ‘ฅ @mention support for your Jekyll site
Ruby
199
star
16

jekyll-sass-converter

A Sass converter for Jekyll.
Ruby
186
star
17

jekyll-help

NO LONGER MAINTAINED. USE JEKYLL TALK INSTEAD.
146
star
18

jekyll-paginate

NO LONGER UNDER ACTIVE DEVELOPMENT as of Jekyll 3: Pagination Generator for Jekyll
Ruby
112
star
19

jekyll-watch

๐Ÿ‘€ Rebuild your Jekyll site when a file changes with the `--watch` switch.
Ruby
93
star
20

jekyll-avatar

A Jekyll plugin for rendering GitHub avatars
Ruby
89
star
21

jekyll-coffeescript

A CoffeeScript converter for Jekyll.
Ruby
52
star
22

brand

Logo files for Jekyll.
40
star
23

dashboard

A dashboard for at-a-glance knowledge of the health of the Jekyll ecosystem.
Go
40
star
24

jekyll-docs

Offline usage documentation for Jekyll. Requires Jekyll 3 and above.
Ruby
38
star
25

example

Jekyll example Project site. Do not clone.
CSS
38
star
26

jekyll-opal

Let Jekyll convert your Ruby into JavaScript using Opal
Ruby
36
star
27

directory

Plugins and Themes discovery for Jekyll, built with Jekyll.
SCSS
34
star
28

jekyll-commonmark

CommonMark generator for Jekyll
Ruby
34
star
29

atom-jekyll

An editor built on top of a web browser? How 'bout some static site previewing?
26
star
30

jekyll-textile-converter

Textile converter for Jekyll.
Ruby
20
star
31

Utterson

CI benchmarking suite for Jekyll
Shell
13
star
32

dns

DNS records for Jekyll properties. Uses octodns to sync.
Shell
13
star
33

hyde

Our Campfire Hubot. Helps us maintain Jekyll even better. โค๏ธ
CoffeeScript
13
star
34

benchmarking

Benchmarking tools for Jekyll
Shell
12
star
35

hubot-pr-status

Determine the status of a given pull request on GitHub.
CoffeeScript
9
star
36

jekyllbot

The code that runs @jekyllbot
Go
8
star
37

acceptance

Daily acceptance tests for Jekyll. Builds 10 complex websites.
Shell
8
star
38

themes-site

A list of third-party themes.
8
star
39

teams

The code behind teams.jekyllrb.com
HTML
7
star
40

screencast

A collection of Jekyll screencast ideas.
7
star
41

rubocop-jekyll

A RuboCop extension to enforce common code style in Jekyll and Jekyll plugins
Ruby
7
star
42

jekyll-test

Testing helpers for Jekyll.
Ruby
6
star
43

test-site

A very, very simple, barebones test site for Jekyll.
CSS
6
star
44

omnibus-jekyll

NOT UNDER ACTIVE DEVELOPMENT: Build standalone installers for Jekyll & its dependencies.
Ruby
6
star
45

jekyll-test-gem-plugin

Wouldn't it be cool if you could ship Jekyll plugins as gems?
Ruby
5
star
46

mojobot

The #jekyll IRC bot.
CoffeeScript
4
star
47

cases

Test cases to aid in exploring bugs with Jekyll.
CSS
4
star
48

jekyll-test-plugin-malicious

A MALICIOUS I WILL EAT ALL YOUR CODE plugin. Use at your own risk.
Ruby
4
star
49

common-theme

HTML
3
star
50

carbon-copy-content

Clone these files from here to other repos using probot.
3
star
51

jekyll-test-theme-malicious

A malicious theme for Jekyll (for testing)
Ruby
3
star
52

.github

Jekyll organization github metadata
2
star
53

profiling

Profiling the build time of various types of sites.
Shell
2
star