Skip to content
This repository has been archived by the owner on Mar 11, 2019. It is now read-only.
/ docopt.clj Public archive

docopt's Clojure (unnoficial) implementation.

License

Notifications You must be signed in to change notification settings

carocad/docopt.clj

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

74 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

deprecated

  • use some of the available libraries for Java and Javascript from docopt's organization. Clojure has wonderful interop so there is no need to reinvent the wheel :)

docopt.clj

GitHub license Build Status

Clojars Project

Creates beautiful command-line interfaces using only your docstring. After all, a good help message has all necessary information in it to make a parser

Clojure (unnoficial) implementation of docopt,

Usage

docopt exposes a single function in docopt.core called parse. Example usage:

(ns example.core
  (:require [docopt.core :as docopt]))

(defn -main
 "Naval Fate.

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

  Options:
  -h --help     Show this screen.
  --version     Show version.
  --speed=<kn>  Speed in knots [default: 10].
  --moored      Moored (anchored) mine.
  --drifting    Drifting mine."
  [& args]
  (docopt/parse (:doc (meta #'-main)) args))

you can test your cli parser by passing a sequence of string arguments to the parse function. The same way that you get from the console.

(-main "mine" "set" "10" "20" "--drifting")
; => {:mine "mine", :set "set", :<x> "10", :<y> "20", :--drifting "--drifting"}

Tests

Run lein test to validate all tests.

Notes

This project does NOT completely follow the original docopt Python implementation. Some of them are yet to be implemented and some are not going to be implemented at all due to a difference of opinion in the ambiguities that the docopt language should accept.

Enjoy it :)

Copyright (c) 2015 Camilo Roca

differences with the original implementation

  • default values are not implemented yet
  • short options cannot be grouped yet
  • arguments (positional and option's arguments) MUST be wrapped in <>
  • repeated elements MUST be declared with (...), i.e. repeating the same name isn't valid e.g. example <port> <port> invalid !! reported as a single <port>
  • [options],[-] [--] special meaning not supported
  • every option MUST be declared in the options: section
  • option's arguments MUST be declared in the Options: section
  • option's arguments should be used with either : or =, e.g. --speed=10