-
Notifications
You must be signed in to change notification settings - Fork 5
/
jxml
executable file
·114 lines (91 loc) · 2.48 KB
/
jxml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/bin/bash
#
# jxml -- Transforms JSON to XML
#
# Joan Ordinas <jordinas@gmail.com>
# Script version
declare -r VERSION='0.1'
# This script name
declare -r SELF="${0##*/}"
# JQ script
declare -r JQ="${0}.jq"
# Data and modules directory: edit if necessary
declare -r JBOL='/usr/local/share/jbol'
########################################################################
# Bash options
set -o errexit -o noglob -o nounset -o pipefail
shopt -s expand_aliases
# Print text on screen
alias print='echo 1>&2 -e'
alias message='cat 1>&2'
# Abort with message
function fatal {
print "Error: $*"
exit 1
}
# Show help
function help {
message <<EOF
${SELF} -- Transforms JSON to XML
Usage: ${SELF} [-h | --help | -v | --version]
${SELF} [options...] [file]
${SELF} transforms JSON values to XML documents.
Options:
-h, --help Show this help
-r, --root Set the root element name
-e, --element Set the array elements name
-t, --tab=size Set the whitespace string for indentation
-v, --version Print version information
EOF
exit $(( $# == 0 ))
}
# Print usage message and exit
function usage {
message <<EOF
Unknown option: $1
Usage: ${SELF} [-h | --help | -v | --version]
${SELF} [options...] [file]
EOF
exit 1
}
# Print version and exit
function version {
print "${SELF} ${VERSION}"
exit 0
}
########################################################################
function main {
local root="root" element="element" tab=" "
local opt
while getopts :hvr:e:t:-: opt; do
case $opt in
h) help ;;
r) root=$OPTARG ;;
e) element=$OPTARG ;;
t) tab=$OPTARG ;;
v) version ;;
-) case $OPTARG in
help) help ;;
root=*)root=${OPTARG#root=} ;;
element=*)root=${OPTARG#element=} ;;
tab=*)root=${OPTARG#tab=} ;;
version) version ;;
*) usage "--${OPTARG}";;
esac
;;
?) usage "-${OPTARG}";;
esac
done
shift $((OPTIND-1))
jq -L $JBOL \
--from-file $JQ \
--raw-output \
--arg opt_root "$root" \
--arg opt_element "$element" \
--arg opt_tab "$tab" \
"$@" 2>&1
}
# Call main and exit
main "$@"
exit
# vim:syntax=sh:ai:sw=4:ts=4:et