Skip to content

Commit 6bad95c

Browse files
committed
add support for default values for flags and args
1 parent 5697540 commit 6bad95c

File tree

6 files changed

+25
-0
lines changed

6 files changed

+25
-0
lines changed

lib/bashly/models/command.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,22 @@ def deep_commands
5555
result
5656
end
5757

58+
# Returns an array of all the default Args
59+
def default_args
60+
args.select &:default
61+
end
62+
5863
# If any of this command's subcommands has the default option set to
5964
# true, this default command will be returned, nil otherwise.
6065
def default_command
6166
commands.find { |c| c.default }
6267
end
6368

69+
# Returns an array of all the default Flags
70+
def default_flags
71+
flags.select &:default
72+
end
73+
6474
# Returns an array of EnvironmentVariables
6575
def environment_variables
6676
return [] unless options["environment_variables"]

lib/bashly/templates/strings.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ group: "%{group} Commands:"
1414
command_shortcut: "Shortcut: %{short}"
1515
default_command_summary: "%{summary} (default)"
1616
required: "(required)"
17+
default: "Default: %{value}"
1718

1819
# Fixed flags help text
1920
help_flag_text: Show this help
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# :argument.usage
22
echo " <%= name.upcase %>"
33
printf "<%= help.wrap(76).indent(4).sanitize_for_print %>\n"
4+
<%- if default -%>
5+
printf " <%= strings[:default] % { value: default } -%>\n"
6+
<%- end -%>
47
echo
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# :command.default_assignments
2+
<%- default_args.each do |arg| -%>
3+
[[ -n ${args[<%= arg.name %>]} ]] || args[<%= arg.name %>]="<%= arg.default %>"
4+
<%- end -%>
5+
<%- default_flags.each do |flag| -%>
6+
[[ -n ${args[<%= flag.long %>]} ]] || args[<%= flag.long %>]="<%= flag.default %>"
7+
<%- end -%>

lib/bashly/views/command/parse_requirements.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ parse_requirements() {
1111
<%= render(:required_args_filter).indent 2 %>
1212
<%= render(:required_flags_filter).indent 2 %>
1313
<%= render(:parse_requirements_while).indent 2 %>
14+
<%= render(:default_assignments).indent 2 %>
1415
}
1516

1617
<%- commands.each do |command| %>

lib/bashly/views/flag/usage.erb

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

0 commit comments

Comments
 (0)