Skip to content

Commit 30f3339

Browse files
authored
Merge branch 'master' into add/validations
2 parents 353b102 + 7187c48 commit 30f3339

File tree

15 files changed

+142
-47
lines changed

15 files changed

+142
-47
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ a [docker image](https://hub.docker.com/r/dannyben/bashly).
4040
file for you ([example](https://github.com/DannyBen/bashly/tree/master/examples/minimal#bashlyyml)).
4141
2. Bashly then automatically generates a bash script (when you run
4242
`bashly generate`) that can parse and validate user input, provide help
43-
messages, and run your code for each command ([example](https://github.com/DannyBen/bashly/blob/master/examples/minimal/download)).
43+
messages, and run your code for each command.
4444
3. Your code for each command is kept in a separate file, and can be merged
4545
again if you change it ([example](https://github.com/DannyBen/bashly/blob/master/examples/minimal/src/root_command.sh)).
4646

examples/colors/src/lib/colors.sh

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,42 @@
44
#
55
# Usage:
66
# Use any of the functions below to color or format a portion of a string.
7-
#
7+
#
88
# echo "before $(red this is red) after"
99
# echo "before $(green_bold this is green_bold) after"
1010
#
11+
# Color output will be disabled if `NO_COLOR` environment variable is set
12+
# in compliance with https://no-color.org/
13+
#
1114
# ---
1215

13-
red() { printf "\e[31m%b\e[0m\n" "$*"; }
14-
green() { printf "\e[32m%b\e[0m\n" "$*"; }
15-
yellow() { printf "\e[33m%b\e[0m\n" "$*"; }
16-
blue() { printf "\e[34m%b\e[0m\n" "$*"; }
17-
magenta() { printf "\e[35m%b\e[0m\n" "$*"; }
18-
cyan() { printf "\e[36m%b\e[0m\n" "$*"; }
19-
bold() { printf "\e[1m%b\e[0m\n" "$*"; }
20-
underlined() { printf "\e[4m%b\e[0m\n" "$*"; }
21-
red_bold() { printf "\e[1;31m%b\e[0m\n" "$*"; }
22-
green_bold() { printf "\e[1;32m%b\e[0m\n" "$*"; }
23-
yellow_bold() { printf "\e[1;33m%b\e[0m\n" "$*"; }
24-
blue_bold() { printf "\e[1;34m%b\e[0m\n" "$*"; }
25-
magenta_bold() { printf "\e[1;35m%b\e[0m\n" "$*"; }
26-
cyan_bold() { printf "\e[1;36m%b\e[0m\n" "$*"; }
27-
red_underlined() { printf "\e[4;31m%b\e[0m\n" "$*"; }
28-
green_underlined() { printf "\e[4;32m%b\e[0m\n" "$*"; }
29-
yellow_underlined() { printf "\e[4;33m%b\e[0m\n" "$*"; }
30-
blue_underlined() { printf "\e[4;34m%b\e[0m\n" "$*"; }
31-
magenta_underlined() { printf "\e[4;35m%b\e[0m\n" "$*"; }
32-
cyan_underlined() { printf "\e[4;36m%b\e[0m\n" "$*"; }
16+
print_in_color() {
17+
local color="$1"
18+
shift
19+
if [[ -z ${NO_COLOR+x} ]]; then
20+
printf "$color%b\e[0m\n" "$*";
21+
else
22+
printf "%b\n" "$*";
23+
fi
24+
}
25+
26+
red() { print_in_color "\e[31m" "$*"; }
27+
green() { print_in_color "\e[32m" "$*"; }
28+
yellow() { print_in_color "\e[33m" "$*"; }
29+
blue() { print_in_color "\e[34m" "$*"; }
30+
magenta() { print_in_color "\e[35m" "$*"; }
31+
cyan() { print_in_color "\e[36m" "$*"; }
32+
bold() { print_in_color "\e[1m" "$*"; }
33+
underlined() { print_in_color "\e[4m" "$*"; }
34+
red_bold() { print_in_color "\e[1;31m" "$*"; }
35+
green_bold() { print_in_color "\e[1;32m" "$*"; }
36+
yellow_bold() { print_in_color "\e[1;33m" "$*"; }
37+
blue_bold() { print_in_color "\e[1;34m" "$*"; }
38+
magenta_bold() { print_in_color "\e[1;35m" "$*"; }
39+
cyan_bold() { print_in_color "\e[1;36m" "$*"; }
40+
red_underlined() { print_in_color "\e[4;31m" "$*"; }
41+
green_underlined() { print_in_color "\e[4;32m" "$*"; }
42+
yellow_underlined() { print_in_color "\e[4;33m" "$*"; }
43+
blue_underlined() { print_in_color "\e[4;34m" "$*"; }
44+
magenta_underlined() { print_in_color "\e[4;35m" "$*"; }
45+
cyan_underlined() { print_in_color "\e[4;36m" "$*"; }

examples/colors/test.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
set -x
44

5+
bashly add colors --force
56
bashly generate
67

78
### Try Me ###
89

910
./colorly
11+
NO_COLOR=1 ./colorly

lib/bashly/templates/lib/colors.sh

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,42 @@
44
#
55
# Usage:
66
# Use any of the functions below to color or format a portion of a string.
7-
#
7+
#
88
# echo "before $(red this is red) after"
99
# echo "before $(green_bold this is green_bold) after"
1010
#
11+
# Color output will be disabled if `NO_COLOR` environment variable is set
12+
# in compliance with https://no-color.org/
13+
#
1114
# ---
1215

13-
red() { printf "\e[31m%b\e[0m\n" "$*"; }
14-
green() { printf "\e[32m%b\e[0m\n" "$*"; }
15-
yellow() { printf "\e[33m%b\e[0m\n" "$*"; }
16-
blue() { printf "\e[34m%b\e[0m\n" "$*"; }
17-
magenta() { printf "\e[35m%b\e[0m\n" "$*"; }
18-
cyan() { printf "\e[36m%b\e[0m\n" "$*"; }
19-
bold() { printf "\e[1m%b\e[0m\n" "$*"; }
20-
underlined() { printf "\e[4m%b\e[0m\n" "$*"; }
21-
red_bold() { printf "\e[1;31m%b\e[0m\n" "$*"; }
22-
green_bold() { printf "\e[1;32m%b\e[0m\n" "$*"; }
23-
yellow_bold() { printf "\e[1;33m%b\e[0m\n" "$*"; }
24-
blue_bold() { printf "\e[1;34m%b\e[0m\n" "$*"; }
25-
magenta_bold() { printf "\e[1;35m%b\e[0m\n" "$*"; }
26-
cyan_bold() { printf "\e[1;36m%b\e[0m\n" "$*"; }
27-
red_underlined() { printf "\e[4;31m%b\e[0m\n" "$*"; }
28-
green_underlined() { printf "\e[4;32m%b\e[0m\n" "$*"; }
29-
yellow_underlined() { printf "\e[4;33m%b\e[0m\n" "$*"; }
30-
blue_underlined() { printf "\e[4;34m%b\e[0m\n" "$*"; }
31-
magenta_underlined() { printf "\e[4;35m%b\e[0m\n" "$*"; }
32-
cyan_underlined() { printf "\e[4;36m%b\e[0m\n" "$*"; }
16+
print_in_color() {
17+
local color="$1"
18+
shift
19+
if [[ -z ${NO_COLOR+x} ]]; then
20+
printf "$color%b\e[0m\n" "$*";
21+
else
22+
printf "%b\n" "$*";
23+
fi
24+
}
25+
26+
red() { print_in_color "\e[31m" "$*"; }
27+
green() { print_in_color "\e[32m" "$*"; }
28+
yellow() { print_in_color "\e[33m" "$*"; }
29+
blue() { print_in_color "\e[34m" "$*"; }
30+
magenta() { print_in_color "\e[35m" "$*"; }
31+
cyan() { print_in_color "\e[36m" "$*"; }
32+
bold() { print_in_color "\e[1m" "$*"; }
33+
underlined() { print_in_color "\e[4m" "$*"; }
34+
red_bold() { print_in_color "\e[1;31m" "$*"; }
35+
green_bold() { print_in_color "\e[1;32m" "$*"; }
36+
yellow_bold() { print_in_color "\e[1;33m" "$*"; }
37+
blue_bold() { print_in_color "\e[1;34m" "$*"; }
38+
magenta_bold() { print_in_color "\e[1;35m" "$*"; }
39+
cyan_bold() { print_in_color "\e[1;36m" "$*"; }
40+
red_underlined() { print_in_color "\e[4;31m" "$*"; }
41+
green_underlined() { print_in_color "\e[4;32m" "$*"; }
42+
yellow_underlined() { print_in_color "\e[4;33m" "$*"; }
43+
blue_underlined() { print_in_color "\e[4;34m" "$*"; }
44+
magenta_underlined() { print_in_color "\e[4;35m" "$*"; }
45+
cyan_underlined() { print_in_color "\e[4;36m" "$*"; }

lib/bashly/views/command/parse_requirements_case.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
% if args.any?
33
% condition = "if"
44
% args.each do |arg|
5-
<%= condition %> [[ ! ${args[<%= arg.name %>]} ]]; then
5+
<%= condition %> [[ -z ${args[<%= arg.name %>]+x} ]]; then
66
<%= arg.render(:validations).indent 2 %>
77
args[<%= arg.name %>]=$1
88
shift

lib/bashly/views/command/required_args_filter.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# :command.required_args_filter
22
% required_args.each do |arg|
3-
if [[ $1 && $1 != -* ]]; then
3+
if [[ -n ${1+x} && $1 != -* ]]; then
44
<%= arg.render(:validations).indent 2 %>
55
args[<%= arg.name %>]=$1
66
shift

lib/bashly/views/flag/case.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# :flag.case
22
<%= aliases.join " | " %> )
33
% if arg
4-
if [[ $2 ]]; then
4+
if [[ -n ${2+x} ]]; then
55
<%= render(:validations).indent 4 %>
66
args[<%= name %>]="$2"
77
shift

lib/bashly/views/flag/validations.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ if [[ -n $(validate_<%= validate %> "$2") ]]; then
55
exit 1
66
fi
77

8-
% end
8+
% end

spec/approvals/examples/colors

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
+ bashly add colors --force
2+
created src/lib/colors.sh
13
+ bashly generate
24
creating user files in src
35
skipped src/initialize.sh (exists)
@@ -11,3 +13,11 @@ Message Recevied:
1113
==> hello colors
1214
===> hello colors
1315

16+
+ NO_COLOR=1
17+
+ ./colorly
18+
Message Recevied:
19+
20+
=> hello colors
21+
==> hello colors
22+
===> hello colors
23+

spec/approvals/examples/empty-args

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
creating user files in src
2+
skipped src/initialize.sh (exists)
3+
skipped src/test_command.sh (exists)
4+
created ./cli
5+
run ./cli --help to test your bash script
6+
+ ./cli test a b --flag c
7+
args:
8+
- ${args[--flag]} = c
9+
- ${args[optional_arg]} = b
10+
- ${args[required_arg]} = a
11+
+ ./cli test ''
12+
args:
13+
- ${args[required_arg]} =
14+
+ ./cli test '' --flag ''
15+
args:
16+
- ${args[--flag]} =
17+
- ${args[required_arg]} =
18+
+ ./cli test '' '' --flag ''
19+
args:
20+
- ${args[--flag]} =
21+
- ${args[optional_arg]} =
22+
- ${args[required_arg]} =

0 commit comments

Comments
 (0)