Module: GLI::AppSupport
- Included in:
- App
- Defined in:
- lib/gli/app_support.rb
Overview
Internals for make App work
Class Method Summary collapse
Instance Method Summary collapse
-
#accepts ⇒ Object
:nodoc:.
- #around_blocks ⇒ Object
-
#clear_nexts ⇒ Object
:nodoc:.
-
#commands ⇒ Object
:nodoc:.
-
#config_file_name ⇒ Object
Return the name of the config file; mostly useful for generating help docs.
- #context_description ⇒ Object
-
#copy_options_to_aliased_versions(global_options, command, options) ⇒ Object
Copies all options in both global_options and options to keys for the aliases of those flags.
-
#error_device=(e) ⇒ Object
Override the device of stderr; exposed only for testing.
-
#flags ⇒ Object
:nodoc:.
-
#get_default_command ⇒ Object
Get the default command for the entire app.
- #override_command_defaults(command_list, config) ⇒ Object
- #override_default(tokens, config) ⇒ Object
-
#override_defaults_based_on_config(config) ⇒ Object
Sets the default values for flags based on the configuration.
-
#parse_config ⇒ Object
:nodoc:.
- #post_block ⇒ Object
- #pre_block ⇒ Object
-
#reset ⇒ Object
Reset the GLI module internal data structures; mostly useful for testing.
-
#run(args) ⇒ Object
Runs whatever command is needed based on the arguments.
- #stderr ⇒ Object
-
#switches ⇒ Object
:nodoc:.
-
#version_string ⇒ Object
Get the version string.
Class Method Details
.included(klass) ⇒ Object
109 110 111 |
# File 'lib/gli/app_support.rb', line 109 def self.included(klass) @stderr = $stderr end |
Instance Method Details
#accepts ⇒ Object
:nodoc:
75 76 77 |
# File 'lib/gli/app_support.rb', line 75 def accepts #:nodoc: @accepts ||= {} end |
#around_blocks ⇒ Object
136 137 138 |
# File 'lib/gli/app_support.rb', line 136 def around_blocks @around_blocks || [] end |
#clear_nexts ⇒ Object
:nodoc:
98 99 100 101 102 103 |
# File 'lib/gli/app_support.rb', line 98 def clear_nexts # :nodoc: super @skips_post = false @skips_pre = false @skips_around = false end |
#commands ⇒ Object
:nodoc:
121 122 123 |
# File 'lib/gli/app_support.rb', line 121 def commands # :nodoc: @commands ||= { :help => GLI::Commands::Help.new(self), :_doc => GLI::Commands::Doc.new(self) } end |
#config_file_name ⇒ Object
Return the name of the config file; mostly useful for generating help docs
71 72 73 |
# File 'lib/gli/app_support.rb', line 71 def config_file_name #:nodoc: @config_file end |
#context_description ⇒ Object
9 10 11 |
# File 'lib/gli/app_support.rb', line 9 def context_description "in global context" end |
#copy_options_to_aliased_versions(global_options, command, options) ⇒ Object
Copies all options in both global_options and options to keys for the aliases of those flags. For example, if a flag works with either -f or –flag, this will copy the value from [:f] to [:flag] to allow the user to access the options by any alias
82 83 84 85 |
# File 'lib/gli/app_support.rb', line 82 def (,command,) # :nodoc: () command.() end |
#error_device=(e) ⇒ Object
Override the device of stderr; exposed only for testing
5 6 7 |
# File 'lib/gli/app_support.rb', line 5 def error_device=(e) #:nodoc: @stderr = e end |
#flags ⇒ Object
:nodoc:
113 114 115 |
# File 'lib/gli/app_support.rb', line 113 def flags # :nodoc: @flags ||= {} end |
#get_default_command ⇒ Object
Get the default command for the entire app
36 37 38 |
# File 'lib/gli/app_support.rb', line 36 def get_default_command @default_command end |
#override_command_defaults(command_list, config) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/gli/app_support.rb', line 148 def override_command_defaults(command_list,config) command_list.each do |command_name,command| next if command_name == :initconfig || command.nil? command_config = (config['commands'] || {})[command_name] || {} override_default(command.topmost_ancestor.flags,command_config) override_default(command.topmost_ancestor.switches,command_config) override_command_defaults(command.commands,command_config) end end |
#override_default(tokens, config) ⇒ Object
160 161 162 163 164 |
# File 'lib/gli/app_support.rb', line 160 def override_default(tokens,config) tokens.each do |name,token| token.default_value=config[name] if config[name] end end |
#override_defaults_based_on_config(config) ⇒ Object
Sets the default values for flags based on the configuration
141 142 143 144 145 146 |
# File 'lib/gli/app_support.rb', line 141 def override_defaults_based_on_config(config) override_default(flags,config) override_default(switches,config) override_command_defaults(commands,config) end |
#parse_config ⇒ Object
:nodoc:
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/gli/app_support.rb', line 87 def parse_config # :nodoc: config = { 'commands' => {}, } if @config_file && File.exist?(@config_file) require 'yaml' config.merge!(File.open(@config_file) { |file| YAML::load(file) }) end config end |
#post_block ⇒ Object
131 132 133 134 |
# File 'lib/gli/app_support.rb', line 131 def post_block @post_block ||= Proc.new do end end |
#pre_block ⇒ Object
125 126 127 128 129 |
# File 'lib/gli/app_support.rb', line 125 def pre_block @pre_block ||= Proc.new do true end end |
#reset ⇒ Object
Reset the GLI module internal data structures; mostly useful for testing
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/gli/app_support.rb', line 14 def reset # :nodoc: switches.clear flags.clear @commands = nil @version = nil @config_file = nil @use_openstruct = false @prog_desc = nil @error_block = false @pre_block = false @post_block = false @default_command = :help @around_block = nil clear_nexts end |
#run(args) ⇒ Object
Runs whatever command is needed based on the arguments.
args
-
the command line ARGV array
Returns a number that would be a reasonable exit code
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/gli/app_support.rb', line 45 def run(args) #:nodoc: args = args.dup if @preserve_argv command = nil begin override_defaults_based_on_config(parse_config) add_help_switch_if_needed(switches) ,command,,arguments = GLIOptionParser.new(commands,flags,switches,accepts,@default_command).(args) (,command,) = convert_to_openstruct_if_needed() = convert_to_openstruct_if_needed() if proceed?(,command,,arguments) call_command(command,,,arguments) end 0 rescue Exception => ex handle_exception(ex,command) end end |
#stderr ⇒ Object
105 106 107 |
# File 'lib/gli/app_support.rb', line 105 def stderr @stderr ||= STDERR end |
#switches ⇒ Object
:nodoc:
117 118 119 |
# File 'lib/gli/app_support.rb', line 117 def switches # :nodoc: @switches ||= {} end |
#version_string ⇒ Object
Get the version string
31 32 33 |
# File 'lib/gli/app_support.rb', line 31 def version_string #:nodoc @version end |