just a piece of shcript
Developing marginally complex command line utilities using bash in a consistent manner presents challenges.
This project aims at providing a generic library facilitating that task, driven by personal needs.
Specifically, it takes care of argument parsing and validation, logging, http, string manipulation and other commodities in a consistent fashion.
On mac: brew install sh_art
You can then use one of the example binaries to get a taste (named dc-*
).
Or start your own foobar
script:
#!/usr/bin/env bash
. "$(brew --prefix)/lib/dc-sh-art"
# Information about your `foobar` script
readonly CLI_VERSION="0.0.1"
readonly CLI_LICENSE="MIT License"
readonly CLI_DESC="super top dupper awesome"
# Declare flags and arguments
dc::commander::initialize
dc::commander::declare::flag myflag "^(foo|bar)$" "a flag that does foo or bar" optional
dc::commander::declare::arg 1 "[0-9]+" "somearg" "first mandatory argument, that must be an integer"
dc::commander::boot
# State that you need the `find` binary
dc::require find
# Test if the optional flag `myflag` was set
if [ "$DC_ARGE_MYFLAG" ]; then
dc::logger::info "Hey! You used --myflag, and the value was $DC_ARGV_MYFLAG. Did you try --help and --version as well?"
fi
# HEAD something over http
dc::logger::debug "Now, let's query something"
dc::http::request "https://www.google.com" HEAD
# Output the result
dc::logger::warning "We got something!"
dc::http::dump::body
# ... Now go do something useful (like, looking at other cli for inspiration, or reading the docs)
Right now this is tested on:
- macOS 10.14
- Ubuntu 16.04, 18.04, 19.04, 19.10
- Debian stretch, buster, and testing
- Alpine 3.10 and edge
If that wasn't clear, this is meant to be used with bash.
Parts of the core library (dc:http
) requires jq
and curl
, or shasum
.
Library extensions require sqlite
.
Specific binaries (the dc-tooling-*
) may also require additional binaries like git
, shellcheck
, hadolint
, and make
.
- emphasize use of json for cli output (you should really learn
jq
) - don't pollute stdout, use stderr for all logging
- aim for correctness (eg:
shellcheck
), but not true POSIX-ness (too boring)
- explore some of the example clis, as one of them may turn out to be useful
- read about the core library details
- or extensions library details
- or developing sh-art
- it's a portementeau: "sh" (short for "shell") + "art" (short for "I like it"), which somewhat makes sense
- if it was powershell instead of bash, it would probably have been named
phart
, which doesn't really sound right