chap

Utility methods for writing scripts for humans

Installation

$ bpkg install [-g] kirtfitzpatrick/chap

Requirements

bash 4.0 or greater

Usage

$ chap info_msg "Hello, world."

Info: Hello, world.

$ chap -h
Usage:
  chap [-hV]
Options:
  -h|--help      Print this help dialogue and exit
  -V|--version   Print the current version and exit

Logging:
  info_msg           MESSAGE
  nominal_msg        MESSAGE
  attention_msg      MESSAGE
  warning_msg        MESSAGE
  modification_msg   MESSAGE

Evaluation:
  info_cmd           COMMAND [ MESSAGE ]
  nominal_cmd        COMMAND [ MESSAGE ]
  attention_cmd      COMMAND [ MESSAGE ]
  warning_cmd        COMMAND [ MESSAGE ]
  modification_cmd   COMMAND [ MESSAGE ]

Internal:
  echo_cmd           COMMAND
  display_link       FILE_LINK_OR_DIR_PATH
  brief_echo         OUTPUT_BUFFER
  brief_eval         COMMAND

Special purpose:
  print_header       "$0 $*"
  verify_line_count  LABEL COMPARISON_OP VALUE COMMAND
  confirm_cmd        COMMAND [ MESSAGE ]
  confirm_reset      # Reset auto-confirm

Example

#!/usr/bin/env bash
source deps/bin/chap

chap print_header "$0 $*"
chap info_msg "Let's verify line counts."
chap verify_line_count \
  "files" "-ge" 4 \ 
  "find . -type f -or -type l" 

echo ""
chap info_msg "FYI."
chap nominal_msg "As expected."
chap attention_msg "Hey old chap, pay attention."
chap warning_msg "Something's wrong."
chap modification_msg "We're making a change."

echo ""
chap info_cmd \
  "grep -A 5 'brief_eval {' deps/bin/chap" \
  "Implementation of brief_eval."

echo ""
chap confirm_cmd "find . -name '*.sh'" \ 
  "Prompt to confirm or skip."

Host: Kirts-MacBook-Pro-2019.local
Command: ./demo.sh
Working Dir: /Users/kirt/Dropbox/Development/Bidpath/chap-demo

Info: Verify correct file count.
Nominal: 4 files found.
find . -type f -or -type l
./demo.sh
./deps/bin/chap -> ../chap/chap.sh
./deps/chap/chap.sh
./deps/chap/package.json

Info: FYI.
Nominal: As expected.
Attention: Hey old chap, pay attention.
Warning: Something’s wrong.
Modification: We’re making a change.

Info: Implementation of brief_eval.
grep -A 5 ‘brief_eval {‘ deps/bin/chap
function chap_brief_eval {
  CMD=$1
  OUTPUT=$(eval “${CMD}”)
 
  chap_brief_echo “${OUTPUT}”
}

Info: Prompt to confirm or skip.
find . -name ‘*.sh’
Execute (a=all, s=skip):
Initiated at: 15:22:37
./demo.sh
./deps/chap/chap.sh
Completed at: 15:22:37

Published: May 06 2021

blog comments powered by Disqus