• Stars
    star
    210
  • Rank 187,538 (Top 4 %)
  • Language
    R
  • License
    Other
  • Created almost 11 years ago
  • Updated over 3 years ago

Reviews

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

Repository Details

Command-line interface description language for R (http://docopt.org)

version downloads R build status AppVeyor Build Status Depsy status

docopt

docopt helps you to:

  • define an interface for your command-line application, and
  • automatically generate a parser for it.

R package docopt is an implementation of docopt in the R programming language. See my presentation on the useR! 2014 for more details.

For more information see docopt.org

To try docopt in your browser visit try.docopt.org

Installation

The easiest way to get docopt is to install from CRAN:

install.packages("docopt")
library(docopt)

Development version

The latest version of docopt can be installed from GitHub using devtools:

library(devtools)  # make sure to have devtools 1.4!
install_github("docopt/docopt.R")

Testing

It is tested against the tests defined for the reference implementation. It passes most tests. It currently fails tests that

  • count arguments: my_prog.R -v -v should return list(v=2)

The tests can be run using devtools::test() and can be found in "tests" directory.

library(devtools)
devtools::test()

Usage

docopt uses the description of the command-line interface (i.e. help message docstring) to parse command-line arguments.

'Naval Fate.

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

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

' -> doc

library(docopt)
arguments <- docopt(doc, version = 'Naval Fate 2.0')
print(arguments)

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.

To execute your command-line application you need to provide path to command-line executable file (i.e. naval_fate.R in our case) and provide relevant command-line arguments/options/commands.

For example

  • To print command-line application help message:
$ Rscript path/to/naval_fate.R --help
Naval Fate.

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

Options:
  -h --help     Show this screen.
  --version     Show version.
  --speed=<kn>  Speed in knots [default: 10].
  --moored      Moored (anchored) mine.
  --drifting    Drifting mine.
  • To print command-line application version information:
$ Rscript path/to/naval_fate.R --version
Naval Fate 2.0
  • docopt function returns a list of command-line parameters and their corresponding values that can be accessed via $ within your command-line application.
$ Rscript path/to/naval_fate.R ship Guardian move 10 50 --speed=20
List of 23
 $ --help    : logi FALSE
 $ --version : logi FALSE
 $ --speed   : chr "20"
 $ --moored  : logi FALSE
 $ --drifting: logi FALSE
 $ ship      : logi TRUE
 $ new       : logi FALSE
 $ <name>    : chr "Guardian"
 $ move      : logi TRUE
 $ <x>       : chr "10"
 $ <y>       : chr "50"
 $ shoot     : logi FALSE
 $ mine      : logi FALSE
 $ set       : logi FALSE
 $ remove    : logi FALSE
 $ help      : logi FALSE
 $ version   : logi FALSE
 $ speed     : chr "20"
 $ moored    : logi FALSE
 $ drifting  : logi FALSE
 $ name      : chr "Guardian"
 $ x         : chr "10"
 $ y         : chr "50"
  • In case if provided command-line parameters are inconsistent with the "Usage: ..." pattern the error message will be printed along with usage pattern examples.
$ Rscript path/to/naval_fate.R ship mine
Error: 
 usage: naval_fate.R ship new <name>...
  
 usage: naval_fate.R ship <name> move <x> <y> [--speed=<kn>]
  
 usage: naval_fate.R ship shoot <x> <y>
  
 usage: naval_fate.R mine (set|remove) <x> <y> [--moored | --drifting]
  
 usage: naval_fate.R (-h | --help)
  
 usage: naval_fate.R --version
Execution halted

More Repositories

1

docopt

Create *beautiful* command-line interfaces with Python
Python
7,939
star
2

docopt.go

A command-line arguments parser that will make you smile.
Go
1,430
star
3

docopt.cpp

C++11 port of docopt
C++
1,041
star
4

docopt.rs

Docopt for Rust (command line argument parser).
Rust
754
star
5

docopts

Shell interpreter for docopt, the command-line interface description language.
Shell
500
star
6

docopt.rb

Parse command line arguments from nothing more than a usage message
Ruby
461
star
7

docopt.net

Port of docopt to .net
C#
352
star
8

docopt.c

C-code generator for docopt language.
Python
320
star
9

docopt.php

Command line argument parser
PHP
297
star
10

docopt.nim

Command line arguments parser that will make you smile (port of docopt to Nim)
Nim
212
star
11

docopt.java

Java port of docopt
Java
159
star
12

docopt.coffee

docopt - A command line option parser that will make you smile.
CoffeeScript
147
star
13

docopt.hs

A command-line interface description language and parser that will make you smile
Haskell
119
star
14

DocOpt.jl

command line arguments parser
Julia
90
star
15

docopt.clj

Clojure implementation of the docopt language.
Clojure
65
star
16

try.docopt.org

Try out docopt in browser (Flask app running on Heroku)
CSS
61
star
17

docopt.swift

A command-line interface description language and parser that will make you smile http://docopt.org/
Swift
52
star
18

docopt.lua

Lua
37
star
19

docopt.scala

Scala implementation of docopt language
Scala
37
star
20

docopt.fs

Docopt for F#
F#
34
star
21

docopt.d

D
30
star
22

docopt.org

The website
Tcl
25
star
23

docopt.tcl

docopt.org for TCL
Tcl
4
star
24

challenge.docopt.org

Challenge docopt site
Python
3
star
25

docopt.rs-old

New Rust port is here:
Rust
2
star