Skip to content

docopts docopt.sh comatible examples

Sylvain303 edited this page Aug 9, 2019 · 7 revisions

Alternative implementation of using docopt CLI option parser language

Following #35, could we provide a set of examples compatibles with both docopts and docopt.sh?

Do not confuse, docopt.sh command line generator for docopt CLI language with our script docopts.sh (with a final S) which is an helper for simplify reuse of docopts binary.

Example format

Main part of example could be:

  • Usage $DOC assignment (as docopts read it from -h "$VARIABLE")
  • docopt parser switcher (at main script code to allow global scope variable)
  • main script code call (normal expected code based on parsed OPTIONS)

prototype

Here is a non tested code, that could be the format for example

#!/usr/bin/env bash

DOC="Argument parser
Usage: arguments_example.sh [-vqrh] [FILE] ...
       arguments_example.sh (--left | --right) CORRECTION FILE

Process FILE and optionally apply correction to either left-hand side or
right-hand side.

Arguments:
  FILE        optional input file
  CORRECTION  correction angle, needs FILE, --left or --right to be present

Options:
  -h --help
  -v       verbose mode
  -q       quiet mode
  -r       make report
  --left   use left-hand side
  --right  use right-hand side"

main_arguments()
{
  # main function for this script
  # use parsed arguments 
  return 0
}

# incomplete: some code here to detect the parser
case $DOCOPT_PARSER in
  docopts)
    eval "$(docopts --docopt_sh -h "$DOC" : "$@")"
    ;;
  docopt.sh)
    eval "$(docopt "$@")"
    ;;
esac

main_arguments "$@"

Clone this wiki locally