A (toy) language that compiles to bash.
Project description
A (toy) language that compiles to bash. You can think of bashup as just a little syntactic sugar sprinkled on top of bash; any valid bash script is also a valid bashup script.
Just a spoonful of sugar makes the bashisms go down…
#!/bin/bash
@fn hi greeting='Hello', target {
echo "${greeting}, ${target}!"
}
hi --target 'World'
Installation:
$ pip install bashup
Compile and run the above example:
$ bashup -i above_example.bashup -o above_example.sh
$ bash above_example.sh
Hello, World!
Or just run it directly:
$ bashup -r above_example.bashup
Hello, World!
Compiled code (above_example.sh):
#!/bin/bash
#
# usage: hi [--greeting <GREETING>] --target <TARGET> [ARGS]
#
function hi() {
local greeting='Hello'
local target
local target__set=0
local args=()
local i
for ((i = 1; i <= $#; i++)); do
if [ "${!i}" == "--greeting" ]; then
((i++))
greeting=${!i}
elif [ "${!i}" == "--target" ]; then
((i++))
target=${!i}
target__set=1
else
args+=("${!i}")
fi
done
if [ ${target__set} -eq 0 ]; then
echo "[ERROR] The --target parameter must be given."
return 1
fi
__hi "${greeting}" "${target}" "${args[@]}"
}
function __hi() {
local greeting=${1}
local target=${2}
shift 2
echo "${greeting}, ${target}!"
}
hi --target 'World'
Supported Bash Versions
The generated bash code works with bash 3.1 and above (tested against 3.1 to 4.3).
Nifty Features
Bashup tries its best to match the indentation of its compiled code against your hand-written bash. For example:
@fn hi greeting='Hello', target {
echo "${greeting}, ${target}!"
}
…compiles to:
#
# usage: hi [--greeting <GREETING>] --target <TARGET> [ARGS]
#
function hi() {
local greeting='Hello'
local target
local target__set=0
local args=()
local i
for ((i = 1; i <= $#; i++)); do
if [ "${!i}" == "--greeting" ]; then
((i++))
greeting=${!i}
...
Planned Improvements
Changelog
1.1.2
Badges now use shields.io.
Fixed - Issue #5: “Make compatible with latest “themattrix/tox” Docker baseimage.”
1.1.1
Tweaked the README.
1.1.0
Fixed - Issue #2: “Run generated bash code against multiple versions of bash.”
Feature - Issue #3: “Allow running of bashup scripts directly.”
Fixed - Issue #4: “Last positional parameter to @fn may not be passed to generated function.”
1.0.0
Initial release, supports @fn syntax.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for bashup-1.1.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f648fc0af02a410e63ba900fc6b7c7e23866f501cdbf87d46a284cd82f53e89b |
|
MD5 | 35dd6cc9b192207b20a4fdd02a34aaa1 |
|
BLAKE2b-256 | 3de12647530a42be21b1b625dd2e86a9b910036d48dfb34de85a4a3343914585 |