Skip to content

Commit 1dc183d

Browse files
committed
add usage colors example
1 parent f4c8086 commit 1dc183d

File tree

18 files changed

+423
-13
lines changed

18 files changed

+423
-13
lines changed

Runfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ end
5959

6060
help "Append the content of bashly.yml to all example READMEs"
6161
action :examples do
62+
# Disable color output (for colors-usage example)
63+
ENV['NO_COLOR']='1'
64+
6265
# Patch the PATH to allow the extensible example to run properly
6366
ENV['PATH']="#{Dir.pwd}/examples/extensible:#{ENV['PATH']}"
6467
Example.all.each do |example|

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Each of these examples demonstrates one aspect or feature of bashly.
3939

4040
## Customization
4141

42+
- [colors-usage](colors-usage#readme) - adding colors to the usage text
4243
- [command-groups](command-groups#readme) - grouping sub-commands in logical sections
4344
- [custom-strings](custom-strings#readme) - configuring the script's error and usage texts
4445
- [custom-includes](custom-includes#readme) - adding and organizing your custom functions

examples/colors-usage/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cli

examples/colors-usage/README.md

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# Colors Usage Example
2+
3+
This example demonstrates how to apply colors to various usage elements.
4+
5+
This example was generated with:
6+
7+
```bash
8+
$ bashly init
9+
$ bashly add colors
10+
$ bashly add settings
11+
# ... now edit settings.yml to match the example ...
12+
$ bashly generate
13+
```
14+
15+
The `bashly add colors` command, simply created the [src/lib/colors.sh]
16+
(src/lib/colors.sh) file, with useful color functions that will be
17+
automatically included in the generated script.
18+
19+
The manually modified `settings.yml` file specifies which color function is
20+
used for which usage element.
21+
22+
Note that the output displayed in this README is not colored.
23+
24+
<!-- include: settings.yml -->
25+
26+
-----
27+
28+
## `bashly.yml`
29+
30+
```yaml
31+
name: cli
32+
help: Sample application
33+
version: 0.1.0
34+
35+
environment_variables:
36+
- name: api_key
37+
help: Set your API key
38+
39+
commands:
40+
- name: download
41+
alias: d
42+
help: Download a file
43+
44+
args:
45+
- name: source
46+
required: true
47+
help: URL to download from
48+
- name: target
49+
help: "Target filename (default: same as source)"
50+
51+
flags:
52+
- long: --force
53+
short: -f
54+
help: Overwrite existing files
55+
56+
examples:
57+
- cli download example.com
58+
- cli download example.com ./output -f
59+
60+
environment_variables:
61+
- name: default_target_location
62+
help: Set the default location to download to
63+
64+
- name: upload
65+
alias: u
66+
help: Upload a file
67+
args:
68+
- name: source
69+
required: true
70+
help: File to upload
71+
72+
flags:
73+
- long: --user
74+
short: -u
75+
arg: user
76+
help: Username to use for logging in
77+
required: true
78+
- long: --password
79+
short: -p
80+
arg: password
81+
help: Password to use for logging in
82+
```
83+
84+
## `settings.yml`
85+
86+
```yaml
87+
# Display various usage elements in color by providing the name of the color
88+
# function. The value for each property is a name of a function that is
89+
# available in your script, for example: `green` or `bold`.
90+
# You can run `bashly add colors` to add a standard colors library.
91+
# This option cannot be set via environment variables.
92+
usage_colors:
93+
caption: bold
94+
command: green
95+
arg: blue
96+
flag: magenta
97+
environment_variable: cyan
98+
```
99+
100+
## Generated script output
101+
102+
### `$ ./cli`
103+
104+
```shell
105+
cli - Sample application
106+
107+
Usage:
108+
cli COMMAND
109+
cli [COMMAND] --help | -h
110+
cli --version | -v
111+
112+
Commands:
113+
download Download a file
114+
upload Upload a file
115+
```
116+
117+
### `$ ./cli -h`
118+
119+
```shell
120+
cli - Sample application
121+
122+
Usage:
123+
cli COMMAND
124+
cli [COMMAND] --help | -h
125+
cli --version | -v
126+
127+
Commands:
128+
download Download a file
129+
upload Upload a file
130+
131+
Options:
132+
--help, -h
133+
Show this help
134+
135+
--version, -v
136+
Show version number
137+
138+
Environment Variables:
139+
API_KEY
140+
Set your API key
141+
```
142+
143+
### `$ ./cli download -h`
144+
145+
```shell
146+
cli download - Download a file
147+
148+
Alias: d
149+
150+
Usage:
151+
cli download SOURCE [TARGET] [OPTIONS]
152+
cli download --help | -h
153+
154+
Options:
155+
--help, -h
156+
Show this help
157+
158+
--force, -f
159+
Overwrite existing files
160+
161+
Arguments:
162+
SOURCE
163+
URL to download from
164+
165+
TARGET
166+
Target filename (default: same as source)
167+
168+
Environment Variables:
169+
DEFAULT_TARGET_LOCATION
170+
Set the default location to download to
171+
172+
Examples:
173+
cli download example.com
174+
cli download example.com ./output -f
175+
```

examples/colors-usage/settings.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Display various usage elements in color by providing the name of the color
2+
# function. The value for each property is a name of a function that is
3+
# available in your script, for example: `green` or `bold`.
4+
# You can run `bashly add colors` to add a standard colors library.
5+
# This option cannot be set via environment variables.
6+
usage_colors:
7+
caption: bold
8+
command: green
9+
arg: blue
10+
flag: magenta
11+
environment_variable: cyan
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: cli
2+
help: Sample application
3+
version: 0.1.0
4+
5+
environment_variables:
6+
- name: api_key
7+
help: Set your API key
8+
9+
commands:
10+
- name: download
11+
alias: d
12+
help: Download a file
13+
14+
args:
15+
- name: source
16+
required: true
17+
help: URL to download from
18+
- name: target
19+
help: "Target filename (default: same as source)"
20+
21+
flags:
22+
- long: --force
23+
short: -f
24+
help: Overwrite existing files
25+
26+
examples:
27+
- cli download example.com
28+
- cli download example.com ./output -f
29+
30+
environment_variables:
31+
- name: default_target_location
32+
help: Set the default location to download to
33+
34+
- name: upload
35+
alias: u
36+
help: Upload a file
37+
args:
38+
- name: source
39+
required: true
40+
help: File to upload
41+
42+
flags:
43+
- long: --user
44+
short: -u
45+
arg: user
46+
help: Username to use for logging in
47+
required: true
48+
- long: --password
49+
short: -p
50+
arg: password
51+
help: Password to use for logging in
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
echo "# this file is located in 'src/download_command.sh'"
2+
echo "# code for 'cli download' goes here"
3+
echo "# you can edit it freely and regenerate (it will not be overwritten)"
4+
inspect_args
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Code here runs inside the initialize() function
2+
## Use it for anything that you need to run before any other function, like
3+
## setting environment variables:
4+
## CONFIG_FILE=settings.ini
5+
##
6+
## Feel free to empty (but not delete) this file.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## Color functions [@bashly-upgrade colors]
2+
## This file is a part of Bashly standard library
3+
##
4+
## Usage:
5+
## Use any of the functions below to color or format a portion of a string.
6+
##
7+
## echo "before $(red this is red) after"
8+
## echo "before $(green_bold this is green_bold) after"
9+
##
10+
## Color output will be disabled if `NO_COLOR` environment variable is set
11+
## in compliance with https://no-color.org/
12+
##
13+
print_in_color() {
14+
local color="$1"
15+
shift
16+
if [[ -z ${NO_COLOR+x} ]]; then
17+
printf "$color%b\e[0m\n" "$*"
18+
else
19+
printf "%b\n" "$*"
20+
fi
21+
}
22+
23+
red() { print_in_color "\e[31m" "$*"; }
24+
green() { print_in_color "\e[32m" "$*"; }
25+
yellow() { print_in_color "\e[33m" "$*"; }
26+
blue() { print_in_color "\e[34m" "$*"; }
27+
magenta() { print_in_color "\e[35m" "$*"; }
28+
cyan() { print_in_color "\e[36m" "$*"; }
29+
bold() { print_in_color "\e[1m" "$*"; }
30+
underlined() { print_in_color "\e[4m" "$*"; }
31+
red_bold() { print_in_color "\e[1;31m" "$*"; }
32+
green_bold() { print_in_color "\e[1;32m" "$*"; }
33+
yellow_bold() { print_in_color "\e[1;33m" "$*"; }
34+
blue_bold() { print_in_color "\e[1;34m" "$*"; }
35+
magenta_bold() { print_in_color "\e[1;35m" "$*"; }
36+
cyan_bold() { print_in_color "\e[1;36m" "$*"; }
37+
red_underlined() { print_in_color "\e[4;31m" "$*"; }
38+
green_underlined() { print_in_color "\e[4;32m" "$*"; }
39+
yellow_underlined() { print_in_color "\e[4;33m" "$*"; }
40+
blue_underlined() { print_in_color "\e[4;34m" "$*"; }
41+
magenta_underlined() { print_in_color "\e[4;35m" "$*"; }
42+
cyan_underlined() { print_in_color "\e[4;36m" "$*"; }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
echo "# this file is located in 'src/upload_command.sh'"
2+
echo "# code for 'cli upload' goes here"
3+
echo "# you can edit it freely and regenerate (it will not be overwritten)"
4+
inspect_args

0 commit comments

Comments
 (0)