|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "chap" |
| 4 | +description: "Utility methods for writing scripts for humans" |
| 5 | +category: bash |
| 6 | +tags: [color, bash, utility, utils, library] |
| 7 | +--- |
| 8 | +## Installation |
| 9 | + |
| 10 | +{% highlight bash %} |
| 11 | +$ bpkg install [-g] kirtfitzpatrick/chap |
| 12 | +{% endhighlight %} |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +{% highlight bash %} |
| 17 | +$ chap info_msg "Hello, world." |
| 18 | +{% endhighlight %} |
| 19 | + |
| 20 | +<span style="color:cyan">Info:</span> Hello, world. |
| 21 | + |
| 22 | +{% highlight bash %} |
| 23 | +$ chap -h |
| 24 | +Usage: |
| 25 | + chap [-hV] |
| 26 | +Options: |
| 27 | + -h|--help Print this help dialogue and exit |
| 28 | + -V|--version Print the current version and exit |
| 29 | + |
| 30 | +Logging: |
| 31 | + info_msg MESSAGE |
| 32 | + nominal_msg MESSAGE |
| 33 | + attention_msg MESSAGE |
| 34 | + warning_msg MESSAGE |
| 35 | + modification_msg MESSAGE |
| 36 | + |
| 37 | +Evaluate command: |
| 38 | + info_cmd COMMAND [ MESSAGE ] |
| 39 | + nominal_cmd COMMAND [ MESSAGE ] |
| 40 | + attention_cmd COMMAND [ MESSAGE ] |
| 41 | + warning_cmd COMMAND [ MESSAGE ] |
| 42 | + modification_cmd COMMAND [ MESSAGE ] |
| 43 | + echo_cmd COMMAND |
| 44 | + |
| 45 | +Internal: |
| 46 | + display_link FILE_LINK_OR_DIR_PATH |
| 47 | + brief_echo OUTPUT_STRING |
| 48 | + brief_eval COMMAND |
| 49 | + |
| 50 | +Iterate by line: |
| 51 | + begin_line_looping # PREV_IFS=$(chap begin_line_looping) |
| 52 | + end_line_looping # chap end_line_looping "${PREV_IFS}" |
| 53 | + |
| 54 | +Special purpose: |
| 55 | + print_header "$0 $*" |
| 56 | + verify_line_count LABEL COMPARISON_OP VALUE COMMAND |
| 57 | + confirm_cmd COMMAND [MESSAGE] |
| 58 | +{% endhighlight %} |
| 59 | + |
| 60 | + |
| 61 | +## Example |
| 62 | + |
| 63 | +{% highlight bash %} |
| 64 | +#!/usr/bin/env bash |
| 65 | +source deps/bin/chap |
| 66 | + |
| 67 | +chap print_header "$0 $*" |
| 68 | +chap info_msg "Let's verify line counts." |
| 69 | +chap verify_line_count \ |
| 70 | + "files" "-ge" 4 \ |
| 71 | + "find . -type f -or -type l" |
| 72 | +chap confirm_cmd "find . -name '*.sh'" \ |
| 73 | + "Prompt to confirm or skip." |
| 74 | +{% endhighlight %} |
| 75 | + |
| 76 | + |
| 77 | +--- |
| 78 | +<span style="color:cyan">Host:</span> Kirts-MacBook-Pro-2019.local<br /> |
| 79 | +<span style="color:cyan">Command:</span> ./demo.sh <br /> |
| 80 | +<span style="color:cyan">Working Dir:</span> /Users/kirt/Dropbox/Development/Bidpath/chap-demo<br /> |
| 81 | + |
| 82 | +<span style="color:cyan">Info:</span> Verify correct file count. <br /> |
| 83 | +<span style="color:green">Nominal:</span> 4 files found. <br /> |
| 84 | +<span style="color:blue">find . -type f -or -type l</span><br /> |
| 85 | +./demo.sh<br /> |
| 86 | +./deps/bin/chap -> <span style="color:cyan">../chap/chap.sh</span><br /> |
| 87 | +./deps/chap/chap.sh<br /> |
| 88 | +./deps/chap/package.json<br /> |
| 89 | +<span style="color:cyan">Info:</span> Prompt to confirm or skip.<br /> |
| 90 | +<span style="color:blue">find . -name '*.sh'</span><br /> |
| 91 | +<span style="color:purple">Execute (a=all, s=skip):</span> <br /> |
| 92 | +<span style="color:cyan">Initiated at:</span> 15:22:37<br /> |
| 93 | +./demo.sh<br /> |
| 94 | +./deps/chap/chap.sh<br /> |
| 95 | +<span style="color:cyan">Completed at:</span> 15:22:37<br /> |
| 96 | + |
| 97 | + |
| 98 | +## Links |
| 99 | +* [Source Code (GitHub)][https://github.com/kirtfitzpatrick/chap] |
| 100 | +* [Author: Kirt Fitzpatrick](https://twitter.com/KirtFitzpatrick) |
| 101 | +{% include JB/setup %} |
0 commit comments