forked from gleam-lang/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command-line-reference.sh
129 lines (99 loc) · 3.41 KB
/
command-line-reference.sh
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/sh
# This script uses the gleam compiler CLI to generate markdown
# from its own help output.
set -e
trim() {
sed 's/^[[:space:]]*//' | sed 's/[[:space:]]*$//'
}
printerr() {
echo >&2 "$(basename "$0"): $*"
}
gleam_help="$(gleam help)"
if ! echo "$gleam_help" | grep -q 'SUBCOMMANDS:'; then
# Updating should be easy, just change the patterns to match the new output.
# Try it, remove this error, and see what the diff looks like!
printerr "Looks like gleam is using a clap version other than 3, please update this script."
exit 1
fi
printerr "Assuming clap version 3 when parsing help output."
HEADING_PATTERN='^[A-Z][A-Z]*:'
SUBCOMMAND_HEADING='^SUBCOMMANDS:'
USAGE_HEADING='^USAGE:'
OPTIONS_HEADING='^OPTIONS:'
drop_headings() {
grep -v "$HEADING_PATTERN"
}
find_description() {
# all lines up to the first heading, except the first line
sed -n "2,/$HEADING_PATTERN/p" | drop_headings | trim
}
find_subcommands() {
# all lines between the SUBCOMMANDS heading and the next heading
sed -n "/$SUBCOMMAND_HEADING/,/$HEADING_PATTERN/p" | drop_headings | grep '^ \w' | trim | cut -d' ' -f1
}
find_usage() {
# all lines between the USAGE heading and the next heading
sed -n "/$USAGE_HEADING/,/$HEADING_PATTERN/p" | drop_headings | trim
}
find_options() {
# grep returns 1 if no matches, we need to ignore that for the pipeline to work.
set +e
# All lines between the OPTIONS heading and the next heading,
# except tje --help option as it is not useful in these docs.
# Option descriptions can span multiple lines, hence the funky '§' business to join them.
sed -n "/$OPTIONS_HEADING/,/$HEADING_PATTERN/p" | drop_headings | tr '\n' '§' | sed -E 's/§ *-/\n-/g' | sed 's/§ */ /g' | trim | grep -v -- '--help'
set -e
}
# Render markdown help for a subcommand, or a subcommand under it.
show_docs() {
subcommand="$1"
subsubcommand="$2"
help="$(cat)"
if [ -z "$subsubcommand" ]; then
heading="## \`$subcommand\`"
else
heading="### \`$subcommand $subsubcommand\`"
subsubsubcommand=$(echo "$help" | find_subcommands)
if [ -n "$subsubsubcommand" ]; then
printerr "Subcommand \`$subcommand $subsubcommand\` has subcommands, this is not supported"
exit 1
fi
fi
description=$(echo "$help" | find_description)
usage=$(echo "$help" | find_usage)
options=$(echo "$help" | find_options)
echo
echo "$heading"
echo
echo \`"$usage"\`
echo
echo "$description"
echo
if [ -n "$options" ]; then
echo "| Option | Description |"
echo "| ------ | ----------- |"
echo "$options" | sed 's/^/| \`/' | sed -E 's/( +|$)/\`| /'
fi
}
cat <<EOF
---
title: Command line reference
subtitle: Getting Gleam things done in the terminal
layout: page
---
<!-- This file is automatically generated by \`writing-gleam/command-line-reference.sh\` -->
The \`gleam\` command uses subcommands to access different parts of the functionality:
EOF
# Note: lsp is a "hidden" command so it will not be shown by `gleam help`
subcommands=$(echo "$gleam_help" | find_subcommands)
for subcommand in $subcommands; do
command_help=$(gleam help "$subcommand")
echo "$command_help" | show_docs "$subcommand"
for subsubcommand in $(echo "$command_help" | find_subcommands); do
if [ "$subsubcommand" = "help" ]; then
continue
fi
subcommand_help=$(gleam help "$subcommand" "$subsubcommand")
echo "$subcommand_help" | show_docs "$subcommand" "$subsubcommand"
done
done