Class: Toys::StandardMiddleware::ShowHelp
- Inherits:
-
Object
- Object
- Toys::StandardMiddleware::ShowHelp
- Includes:
- Middleware
- Defined in:
- lib/toys/standard_middleware/show_help.rb
Overview
A middleware that shows help text for the tool when a flag (typically
--help
) is provided. It can also be configured to show help by
default if the tool is a namespace that is not runnable.
If a tool is not runnable, this middleware can also add a
--[no-]recursive
flag, which, when set to true
(the default), shows
all subtools recursively rather than only immediate subtools. This
middleware can also search for keywords in its subtools.
Constant Summary collapse
- DEFAULT_HELP_FLAGS =
Default help flags
["-?", "--help"].freeze
- DEFAULT_USAGE_FLAGS =
Default usage flags
["--usage"].freeze
- DEFAULT_RECURSIVE_FLAGS =
Default recursive flags
["-r", "--[no-]recursive"].freeze
- DEFAULT_SEARCH_FLAGS =
Default search flags
["-s WORD", "--search=WORD"].freeze
- SHOW_HELP_KEY =
Key set when the show help flag is present
Object.new.freeze
- SHOW_USAGE_KEY =
Key set when the show usage flag is present
Object.new.freeze
- RECURSIVE_SUBTOOLS_KEY =
Key for the recursive setting
Object.new.freeze
- SEARCH_STRING_KEY =
Key for the search string
Object.new.freeze
- TOOL_NAME_KEY =
Key for the tool name
Object.new.freeze
Instance Method Summary collapse
-
#config(tool_definition, loader) ⇒ Object
Configure flags and default data.
-
#initialize(help_flags: false, usage_flags: false, recursive_flags: false, search_flags: false, default_recursive: false, fallback_execution: false, allow_root_args: false, show_source_path: false, use_less: false, stream: $stdout, styled_output: nil) ⇒ ShowHelp
constructor
Create a ShowHelp middleware.
-
#run(tool) ⇒ Object
Display help text if requested.
Constructor Details
#initialize(help_flags: false, usage_flags: false, recursive_flags: false, search_flags: false, default_recursive: false, fallback_execution: false, allow_root_args: false, show_source_path: false, use_less: false, stream: $stdout, styled_output: nil) ⇒ ShowHelp
Create a ShowHelp middleware.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/toys/standard_middleware/show_help.rb', line 157 def initialize(help_flags: false, usage_flags: false, recursive_flags: false, search_flags: false, default_recursive: false, fallback_execution: false, allow_root_args: false, show_source_path: false, use_less: false, stream: $stdout, styled_output: nil) @help_flags = help_flags @usage_flags = usage_flags @recursive_flags = recursive_flags @search_flags = search_flags @default_recursive = default_recursive ? true : false @fallback_execution = fallback_execution @allow_root_args = allow_root_args @show_source_path = show_source_path @stream = stream @styled_output = styled_output @use_less = use_less end |
Instance Method Details
#config(tool_definition, loader) ⇒ Object
Configure flags and default data.
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/toys/standard_middleware/show_help.rb', line 184 def config(tool_definition, loader) unless tool_definition.argument_parsing_disabled? help_flags = add_help_flags(tool_definition) usage_flags = add_usage_flags(tool_definition) if @allow_root_args && (!help_flags.empty? || !usage_flags.empty?) if tool_definition.root? && tool_definition.arg_definitions.empty? tool_definition.set_remaining_args(TOOL_NAME_KEY, display_name: "TOOL_NAME", desc: "The tool for which to display help") end end if (!help_flags.empty? || @fallback_execution) && loader.has_subtools?(tool_definition.full_name) add_recursive_flags(tool_definition) add_search_flags(tool_definition) end end yield end |
#run(tool) ⇒ Object
Display help text if requested.
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/toys/standard_middleware/show_help.rb', line 207 def run(tool) if tool[SHOW_USAGE_KEY] help_text = get_help_text(tool) str = help_text.usage_string(wrap_width: terminal.width) terminal.puts(str) elsif @fallback_execution && !tool[Tool::Keys::TOOL_DEFINITION].runnable? || tool[SHOW_HELP_KEY] help_text = get_help_text(tool) str = help_text.help_string(recursive: tool[RECURSIVE_SUBTOOLS_KEY], search: tool[SEARCH_STRING_KEY], show_source_path: @show_source_path, wrap_width: terminal.width) output_help(str) else yield end end |