项目作者: xwmx

项目描述 :
A runner and framework for command-centric Bash scripts.
高级语言: Shell
项目地址: git://github.com/xwmx/bask.git
创建时间: 2015-03-21T03:53:36Z
项目社区:https://github.com/xwmx/bask

开源协议:GNU General Public License v2.0

下载


  1. ___ ___ ___
  2. _____ / /\ / /\ /__/|
  3. / /::\ / /::\ / /:/_ | |:|
  4. / /:/\:\ / /:/\:\ / /:/ /\ | |:|
  5. / /:/~/::\ / /:/~/::\ / /:/ /::\ __| |:|
  6. /__/:/ /:/\:/__/:/ /:/\:/__/:/ /:/\:/__/\_|:|____
  7. \ \:\/:/~/:\ \:\/:/__\\ \:\/:/~/:\ \:\/:::::/
  8. \ \::/ /:/ \ \::/ \ \::/ /:/ \ \::/~~~~
  9. \ \:\/:/ \ \:\ \__\/ /:/ \ \:\
  10. \ \::/ \ \:\ /__/:/ \ \:\
  11. \__\/ \__\/ \__\/ \__\/

bask

A framework for command-centric Bash scripts.

Features

Some basic features available automatically:

  • Strict Mode,
  • Help template, printable with -h or --help,
  • _debug printing with --debug flag,
  • _exit_1 and _warn functions with error message printing,
  • Option normalization (eg, -ab -c -> -a -b -c) and option parsing,
  • Automatic arbitrary command loading,
  • A simple approach for specifying per-command help with describe,
  • Built-in commands for help, version, and command listing,
  • Conventions for distinguishing between functions and program commands,
  • Useful utility functions.

Installation

Homebrew

To install with Homebrew:

  1. brew tap xwmx/taps
  2. brew install bask

npm

To install with npm:

  1. npm install --global bask.sh

bpkg

To install with bpkg:

  1. bpkg install xwmx/bask

Manual

To install manually, simply add the bask script to your $PATH. If
you already have a ~/bin directory, you can use the following command:

  1. curl -L https://raw.github.com/xwmx/bask/master/bask \
  2. -o ~/bin/bask && chmod +x ~/bin/bask

Usage

bask can be used primarily in two ways: with with scripts that source (or,
in other words, inherit from) the bask program, or with Baskfiles defining
functions for the current context.

Bask Scripts

To generate a new bask script, meaning a script that
inherits the bask foundation, use add an argument to the new
command specifying the script name:

  1. bask new <script name>

This generates a script that sources the bask command. You can add
bash functions in this script and they will be automatically set as
sub-commands available as arguments to the program. Additionally, you
can easily document the programs using the built-in describe function.
The help / usage / description information set here is available in the
via the built-in help command.

Baskfiles

A Baskfile is a file containing bash functions and optional
descriptions that can be run using the bask command directly, and can
be defined on a project-by-project basis. This can be useful for defining
task-centric commands within a particular scope where a full program
would be unnecessary.

A Baskfile is similar to a Makefile
or a Rakefile) and looks like
this:

  1. # Baskfile
  2. describe "hello" <<HEREDOC
  3. Usage:
  4. bask hello
  5. Description:
  6. Print a greeting.
  7. HEREDOC
  8. hello() {
  9. echo "Hello from bask!"
  10. }

To generate a new Baskfile, use bask new with no arguments:

  1. bask new

When you run the bask program, it first looks in the
current directory for a Baskfile and sources it if one is present. If it
doesn’t find a Baskfile in the current directory, it traverses the
parent directories, sourcing the first Baskfile it encounters.

Commands

Commands in bask are simply Bash functions with optional descriptions.
Defined functions will be automatically loaded and displayed as part of
the usage information when the parent command is run. Command-specific
usage information can be set with the describe function, and this usage
information will be made automatically available to the parent program’s
help command.

Example command group structure:

  1. describe example "" # Optional. A short description for the command.
  2. example() { : } # The command called by the user.

For usage formatting conventions see:

Example Command Groups

Micro Example
  1. describe micro "Usage: $_ME micro"
  2. micro() {
  3. echo "Hello, World!"
  4. }
Simple Example
  1. describe simple <<HEREDOC
  2. Usage:
  3. $_ME simple [<name>]
  4. Description:
  5. Print the greeting, "Hello, World!"
  6. HEREDOC
  7. simple() {
  8. local _name="${1:-World}"
  9. printf "Hello, %s!\n" "${_name}"
  10. }
Complex Example
  1. describe complex <<HEREDOC
  2. Usage:
  3. $_ME complex [<name>] [--farewell]
  4. Options:
  5. --farewell Print "Goodbye, World!"
  6. Description:
  7. Print the greeting, "Hello, World!"
  8. HEREDOC
  9. complex() {
  10. local _greeting="Hello"
  11. local _name="World"
  12. for __arg in "${@:-}"
  13. do
  14. case "${__arg}" in
  15. --farewell)
  16. _greeting="Goodbye"
  17. ;;
  18. -*)
  19. _exit_1 printf "Unexpected option: %s\n" "${__arg}"
  20. ;;
  21. *)
  22. if [[ "${_name}" == "World" ]] && [[ -n "${__arg:-}" ]]
  23. then
  24. _name="${__arg}"
  25. fi
  26. ;;
  27. esac
  28. done
  29. printf "%s, %s!\n" "${_greeting}" "${_name}"
  30. }

Optional Vim Configuration

In order to enable Baskfile syntax highlighting in Vim, add the
following line to your .vimrc.

  1. autocmd BufRead,BufNewFile Baskfile call SetFileTypeSH("bash")