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

Class Method Details

.included(klass) ⇒ Object



130
131
132
# File 'lib/gli/app_support.rb', line 130

def self.included(klass)
  @stderr = $stderr
end

Instance Method Details

#acceptsObject

:nodoc:



103
104
105
106
# File 'lib/gli/app_support.rb', line 103

def accepts #:nodoc:
  @accepts ||= {}

end

#argument_handling_strategyObject



210
211
212
# File 'lib/gli/app_support.rb', line 210

def argument_handling_strategy
  @argument_handling_strategy || :loose
end

#around_blocksObject



163
164
165
# File 'lib/gli/app_support.rb', line 163

def around_blocks
  @around_blocks || []
end

#autocompleteObject



218
219
220
# File 'lib/gli/app_support.rb', line 218

def autocomplete
  @autocomplete.nil? ? true : @autocomplete
end

#clear_nextsObject

:nodoc:



119
120
121
122
123
124
# File 'lib/gli/app_support.rb', line 119

def clear_nexts # :nodoc:
  super
  @skips_post = false
  @skips_pre = false
  @skips_around = false
end

#commandsObject

:nodoc:



142
143
144
145
146
147
148
149
150
# File 'lib/gli/app_support.rb', line 142

def commands # :nodoc:
  if !@commands
    @commands = { :help => GLI::Commands::Help.new(self), :_doc => GLI::Commands::Doc.new(self) }
    @commands_declaration_order ||= []
    @commands_declaration_order << @commands[:help]
    @commands_declaration_order << @commands[:_doc]
  end
  @commands
end

#commands_declaration_orderObject

Get an array of commands, ordered by when they were declared



42
43
44
# File 'lib/gli/app_support.rb', line 42

def commands_declaration_order # :nodoc:
  @commands_declaration_order
end

#config_file_nameObject

Return the name of the config file; mostly useful for generating help docs



100
101
102
# File 'lib/gli/app_support.rb', line 100

def config_file_name #:nodoc:
  @config_file
end

#context_descriptionObject



9
10
11
# File 'lib/gli/app_support.rb', line 9

def context_description
  "in global context"
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

#exe_nameObject



37
38
39
# File 'lib/gli/app_support.rb', line 37

def exe_name
  File.basename($0)
end

#flagsObject

:nodoc:



134
135
136
# File 'lib/gli/app_support.rb', line 134

def flags # :nodoc:
  @flags ||= {}
end

#get_default_commandObject

Get the default command for the entire app



52
53
54
# File 'lib/gli/app_support.rb', line 52

def get_default_command
  @default_command
end

#help_sort_typeObject



167
168
169
# File 'lib/gli/app_support.rb', line 167

def help_sort_type
  @help_sort_type || :alpha
end

#help_text_wrap_typeObject



171
172
173
# File 'lib/gli/app_support.rb', line 171

def help_text_wrap_type
  @help_text_wrap_type || :to_terminal
end

#override_command_defaults(command_list, config) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/gli/app_support.rb', line 187

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] || {}

    if @subcommand_option_handling_strategy == :legacy
      override_default(command.topmost_ancestor.flags,command_config)
      override_default(command.topmost_ancestor.switches,command_config)
    else
      override_default(command.flags,command_config)
      override_default(command.switches,command_config)
    end

    override_command_defaults(command.commands,command_config)
  end
end

#override_default(tokens, config) ⇒ Object



204
205
206
207
208
# File 'lib/gli/app_support.rb', line 204

def override_default(tokens,config)
  tokens.each do |name,token|
    token.default_value=config[name] unless config[name].nil?
  end
end

#override_defaults_based_on_config(config) ⇒ Object

Sets the default values for flags based on the configuration



180
181
182
183
184
185
# File 'lib/gli/app_support.rb', line 180

def override_defaults_based_on_config(config)
  override_default(flags,config)
  override_default(switches,config)

  override_command_defaults(commands,config)
end

#parse_configObject

:nodoc:



108
109
110
111
112
113
114
115
116
117
# File 'lib/gli/app_support.rb', line 108

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_blockObject



158
159
160
161
# File 'lib/gli/app_support.rb', line 158

def post_block
  @post_block ||= Proc.new do
  end
end

#pre_blockObject



152
153
154
155
156
# File 'lib/gli/app_support.rb', line 152

def pre_block
  @pre_block ||= Proc.new do
    true
  end
end

#resetObject

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
29
30
31
32
33
34
35
# File 'lib/gli/app_support.rb', line 14

def reset # :nodoc:
  switches.clear
  flags.clear
  @commands = nil
  @command_missing_block = nil
  @commands_declaration_order = []
  @flags_declaration_order = []
  @switches_declaration_order = []
  @version = nil
  @config_file = nil
  @use_openstruct = false
  @prog_desc = nil
  @error_block = false
  @pre_block = false
  @post_block = false
  @default_command = :help
  @autocomplete = false
  @around_block = nil
  @subcommand_option_handling_strategy = :legacy
  @argument_handling_strategy = :loose
  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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/gli/app_support.rb', line 61

def run(args) #:nodoc:
  args = args.dup if @preserve_argv
  the_command = nil
  begin
    override_defaults_based_on_config(parse_config)

    add_help_switch_if_needed(self)

    gli_option_parser = GLIOptionParser.new(commands,
                                            flags,
                                            switches,
                                            accepts,
                                            :default_command => @default_command,
                                            :autocomplete => autocomplete,
                                            :subcommand_option_handling_strategy => subcommand_option_handling_strategy,
                                            :argument_handling_strategy => argument_handling_strategy,
                                            :command_missing_block => @command_missing_block)

    parsing_result = gli_option_parser.parse_options(args)
    parsing_result.convert_to_openstruct! if @use_openstruct

    the_command = parsing_result.command

    if proceed?(parsing_result)
      call_command(parsing_result) 
      0
    else
      raise PreconditionFailed, "preconditions failed"
    end
  rescue Exception => ex
    if the_command.nil? && ex.respond_to?(:command_in_context)
      the_command = ex.command_in_context
    end
    handle_exception(ex,the_command)
  end
end

#stderrObject



126
127
128
# File 'lib/gli/app_support.rb', line 126

def stderr
  @stderr ||= STDERR
end

#subcommand_option_handling_strategyObject



214
215
216
# File 'lib/gli/app_support.rb', line 214

def subcommand_option_handling_strategy
  @subcommand_option_handling_strategy || :legacy
end

#switchesObject

:nodoc:



138
139
140
# File 'lib/gli/app_support.rb', line 138

def switches # :nodoc:
  @switches ||= {}
end

#synopsis_format_typeObject



175
176
177
# File 'lib/gli/app_support.rb', line 175

def synopsis_format_type
  @synopsis_format_type || :full
end

#version_stringObject

Get the version string



47
48
49
# File 'lib/gli/app_support.rb', line 47

def version_string #:nodoc:
  @version
end