Coder Social home page Coder Social logo

docopt_ng's Introduction

Docopt โ€“ command line option parser, that will make you smile

Gem Version Build Status Maintainability


This is a detached fork of the original docopt.rb.

  • For a drop-in replacement, simply require 'docopt_ng/docopt'
  • Otherwise, use require 'docopt_ng' and DocoptNG instead of Docopt.

This is the ruby port of docopt, the awesome option parser written originally in python.

Isn't it awesome how optparse and argparse generate help messages based on your code?!

Hell no! You know what's awesome? It's when the option parser is generated based on the beautiful help message that you write yourself! This way you don't need to write this stupid repeatable parser-code, and instead can write only the help message--the way you want it.

docopt helps you create most beautiful command-line interfaces easily:

require "docopt_ng/docopt"

doc = <<DOCOPT
Naval Fate.

Usage:
  #{__FILE__} ship new <name>...
  #{__FILE__} ship <name> move <x> <y> [--speed=<kn>]
  #{__FILE__} ship shoot <x> <y>
  #{__FILE__} mine (set|remove) <x> <y> [--moored|--drifting]
  #{__FILE__} -h | --help
  #{__FILE__} --version

Options:
  -h --help     Show this screen.
  --version     Show version.
  --speed=<kn>  Speed in knots [default: 10].
  --moored      Moored (anchored) mine.
  --drifting    Drifting mine.

DOCOPT

begin
  pp Docopt.docopt(doc)
rescue Docopt::Exit => e
  puts e.message
  exit e.exit_code
end

Beat that! The option parser is generated based on the docstring above that is passed to docopt function. docopt parses the usage pattern (Usage: ...) and option descriptions (lines starting with dash "-") and ensures that the program invocation matches the usage pattern; it parses options, arguments and commands based on that. The basic idea is that a good help message has all necessary information in it to make a parser.

Installation

$ gem install docopt_ng

API

Docopt takes 1 required and 1 optional argument:

  • doc should be a string that describes options in a human-readable format, that will be parsed to create the option parser. The simple rules of how to write such a docstring (in order to generate option parser from it successfully) are given in the next section. Here is a quick example of such a string:

      Usage: your_program.rb [options]
    
      -h --help     Show this.
      -v --verbose  Print more text.
      --quiet       Print less text.
      -o FILE       Specify output file [default: ./test.txt].
    

    The optional second argument contains a hash of additional data to influence docopt. The following keys are supported:

  • help, by default true, specifies whether the parser should automatically print the usage-message (supplied as doc) in case -h or --help options are encountered. After showing the usage-message, the program will terminate. If you want to handle -h or --help options manually (as all other options), set help=false.

  • version, by default nil, is an optional argument that specifies the version of your program. If supplied, then, if the parser encounters --version option, it will print the supplied version and terminate. version could be any printable object, but most likely a string, e.g. '2.1.0rc1'.

Note, when docopt is set to automatically handle -h, --help and --version options, you still need to mention them in the options description (doc) for your users to know about them.

The return value is just a hash with options, arguments and commands, with keys spelled exactly like in a help message (long versions of options are given priority). For example, if you invoke the top example as:

$ naval_fate.rb ship Guardian move 100 150 --speed=15

the return hash will be::

{
  "ship" => true,
  "new" => false,
  "<name>" => ["Guardian"],
  "move" => true,
  "<x>" => "100",
  "<y>" => "150",
  "--speed" => "15",
  "shoot" => false,
  "mine" => false,
  "set" => false,
  "remove" => false,
  "--moored" => false,
  "--drifting" => false,
  "--help" => false,
  "--version" => false
}

Help message format

This port of docopt follows the docopt help message format. You can find more details at official docopt git repo

Examples

The examples directory contains several examples which cover most aspects of the docopt functionality.

docopt_ng's People

Contributors

alexspeller avatar dannyben avatar djmetzle avatar johari avatar ryanartecona avatar saltedcoffii avatar shabbyrobe avatar

Stargazers

 avatar

Watchers

 avatar  avatar

docopt_ng's Issues

Why was this made?

As I am relying heavily on docopt in Runfile, Mister Bin and all the tools that use these tools, I could not afford having docopt in an unmaintained state.

Since getting a cooperative response from the docopt org proved difficult, I decided to create this separate, suitable to my needs.

Notable changes

  • CI on GitHub Actions
  • Rspec instead of a minitest that calls a python script
  • Runfile instead of Rakefile
  • Compliance with Rubocop standards (wherever possible without full refactoring)
  • Separate files to different classes
  • Fix docopt/docopt.rb#28 which is needed for DannyBen/runfile#62

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.