Skip to content

Commit 5e58388

Browse files
committed
- Add show_examples_on_error setting
1 parent 5e9e322 commit 5e58388

File tree

7 files changed

+34
-3
lines changed

7 files changed

+34
-3
lines changed

lib/bashly/libraries/settings/settings.yml

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ env: development
5555
# The extension to use when reading/writing partial script snippets
5656
partials_extension: sh
5757

58+
# Show command examples (if any) whenever the user does not provide the
59+
# required arguments
60+
show_examples_on_error: false
61+
5862
# Display various usage elements in color by providing the name of the color
5963
# function. The value for each property is a name of a function that is
6064
# available in your script, for example: `green` or `bold`.

lib/bashly/libraries/strings/strings.yml

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ missing_required_argument: "missing required argument: %{arg}\\nusage: %{usage}"
3333
missing_required_flag: "missing required flag: %{usage}"
3434
missing_required_environment_variable: "missing required environment variable: %{var}"
3535
missing_dependency: "missing dependency: %{dependency}"
36+
examples_caption_on_error: 'examples:'
3637
disallowed_flag: "%{name} must be one of: %{allowed}"
3738
disallowed_argument: "%{name} must be one of: %{allowed}"
3839
disallowed_environment_variable: "%{name} environment variable must be one of: %{allowed}"

lib/bashly/settings.rb

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class << self
1010
:config_path,
1111
:lib_dir,
1212
:partials_extension,
13+
:show_examples_on_error,
1314
:source_dir,
1415
:strict,
1516
:tab_indent,
@@ -57,6 +58,10 @@ def production?
5758
env == :production
5859
end
5960

61+
def show_examples_on_error
62+
@show_examples_on_error ||= get :show_examples_on_error
63+
end
64+
6065
def source_dir
6166
@source_dir ||= get :source_dir
6267
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
if Settings.show_examples_on_error && examples.any?
2+
= view_marker
3+
4+
> printf "{{ strings[:examples_caption_on_error] }}\n" >&2
5+
examples.each do |example|
6+
> printf "{{ example.wrap(78).indent(2).sanitize_for_print }}\n" >&2
7+
end
8+
end

lib/bashly/views/command/required_args_filter.gtx

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ if required_args.any?
44
required_args.each do |arg|
55
> if [[ -z ${args['{{ arg.name }}']+x} ]]; then
66
> printf "{{ strings[:missing_required_argument] % { arg: arg.name.upcase, usage: usage_string } }}\n" >&2
7+
= render(:examples_on_error).indent 2
78
> exit 1
89
> fi
910
end
1011

1112
>
12-
13-
end
13+
end

schemas/settings.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
},
9898
"tab_indent": {
9999
"title": "tab indent",
100-
"description": "Whether to use tabs or spaces in the the generated script\nhttps://bashly.dannyb.co/usage/settings/#tab_indent",
100+
"description": "Whether to use tabs or spaces in the generated script\nhttps://bashly.dannyb.co/usage/settings/#tab_indent",
101101
"type": "boolean",
102102
"default": false
103103
},
@@ -113,6 +113,12 @@
113113
"type": "boolean",
114114
"default": true
115115
},
116+
"show_examples_on_error": {
117+
"title": "show examples on error",
118+
"description": "Whether to show command examples when the input line is missing required arguments\nhttps://bashly.dannyb.co/usage/settings/#show_examples_on_error",
119+
"type": "boolean",
120+
"default": true
121+
},
116122
"env": {
117123
"title": "env",
118124
"description": "Whether to include development related comments in the generated script\nhttps://bashly.dannyb.co/usage/settings/#env",

schemas/strings.json

+7
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@
102102
"minLength": 1,
103103
"default": "Allowed: %{values}"
104104
},
105+
"examples_caption_on_error": {
106+
"title": "examples caption on error",
107+
"description": "The string to show before the examples when show_examples_on_error is enabled\nhttps://bashly.dannyb.co/advanced/strings/#custom-strings",
108+
"type": "string",
109+
"minLength": 1,
110+
"default": "examples:"
111+
},
105112
"help_flag_text": {
106113
"title": "help flag text",
107114
"description": "The help message for --help\nhttps://bashly.dannyb.co/advanced/strings/#custom-strings",

0 commit comments

Comments
 (0)