Skip to content

Commit 39a978e

Browse files
committed
- Add support for allowed values for flags
1 parent fd629f9 commit 39a978e

File tree

4 files changed

+14
-1
lines changed

4 files changed

+14
-1
lines changed

lib/bashly/models/base.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ class Base
66
attr_reader :options
77

88
OPTION_KEYS = %i[
9+
allowed
910
arg
11+
default
1012
dependencies
1113
description
12-
default
1314
environment_variables
1415
examples
1516
flags

lib/bashly/templates/strings.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ command_shortcut: "Shortcut: %{short}"
1515
default_command_summary: "%{summary} (default)"
1616
required: "(required)"
1717
default: "Default: %{value}"
18+
allowed: "Allowed: %{values}"
1819

1920
# Fixed flags help text
2021
help_flag_text: Show this help
@@ -28,3 +29,4 @@ missing_required_argument: "missing required argument: %{arg}\\nusage: %{usage}"
2829
missing_required_flag: "missing required flag: %{usage}"
2930
missing_required_environment_variable: "missing required environment variable: %{var}"
3031
missing_dependency: "missing dependency: %{dependency}"
32+
disallowed_flag: "%{name} must be one of: %{allowed}"

lib/bashly/views/flag/case.erb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
args[<%= name %>]="$2"
66
shift
77
shift
8+
<%- if allowed -%>
9+
allowed="<%= allowed.join '|' %>"
10+
if [[ ! ${args[<%= name %>]} =~ $allowed ]]; then
11+
printf "%s\n" "<%= strings[:disallowed_flag] % { name: name, allowed: allowed.join(', ') } %>\n"
12+
exit 1
13+
fi
14+
<%- end -%>
815
else
916
printf "%s\n" "<%= strings[:flag_requires_an_argument] % { name: name, usage: usage_string } %>"
1017
exit 1

lib/bashly/views/flag/usage.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# :flag.usage
22
echo " <%= usage_string extended: true %>"
33
printf "<%= help.wrap(76).indent(4).sanitize_for_print %>\n"
4+
<%- if allowed -%>
5+
printf " <%= strings[:allowed] % { values: allowed.join(', ') } -%>\n"
6+
<%- end -%>
47
<%- if default -%>
58
printf " <%= strings[:default] % { value: default } -%>\n"
69
<%- end -%>

0 commit comments

Comments
 (0)