Module: Belafonte::Help::AppExtensions

Defined in:
lib/belafonte/help/app_extensions.rb

Instance Method Summary collapse

Instance Method Details

#command_argObject



32
33
34
35
# File 'lib/belafonte/help/app_extensions.rb', line 32

def command_arg
  return '' unless show_command_arg?
  ' command'
end

#display_argsObject



46
47
48
49
# File 'lib/belafonte/help/app_extensions.rb', line 46

def display_args
  return '' unless has_args?
  " #{non_command_arg_names.join(' ')}"
end

#display_descriptionObject



22
23
24
# File 'lib/belafonte/help/app_extensions.rb', line 22

def display_description
  description.to_s
end

#display_flags(cmd) ⇒ Object



41
42
43
44
# File 'lib/belafonte/help/app_extensions.rb', line 41

def display_flags(cmd)
  return '' unless has_flags?
  " [#{cmd} options]"
end

#display_titleObject



18
19
20
# File 'lib/belafonte/help/app_extensions.rb', line 18

def display_title
  root? ? executable : title
end

#executableObject



14
15
16
# File 'lib/belafonte/help/app_extensions.rb', line 14

def executable
  File.basename($0)
end

#full_pathObject



26
27
28
29
30
# File 'lib/belafonte/help/app_extensions.rb', line 26

def full_path
  return signature unless parent
  parent.extend(AppExtensions)
  "#{parent.full_path} #{signature}"
end

#has_args?Boolean



69
70
71
# File 'lib/belafonte/help/app_extensions.rb', line 69

def has_args?
  non_command_args.any?
end

#has_flags?Boolean



61
62
63
# File 'lib/belafonte/help/app_extensions.rb', line 61

def has_flags?
  configured_switches.any? || configured_options.any?
end

#has_subcommands?Boolean



73
74
75
# File 'lib/belafonte/help/app_extensions.rb', line 73

def has_subcommands?
  configured_subcommands.any?
end

#help_flagObject



77
78
79
# File 'lib/belafonte/help/app_extensions.rb', line 77

def help_flag
  @help_flag ||= Belafonte::Switch.new(name: :help, short: 'h', long: 'help', description: 'Shows this message')
end

#non_command_arg_namesObject



51
52
53
# File 'lib/belafonte/help/app_extensions.rb', line 51

def non_command_arg_names
  non_command_args.map(&:name).map(&:to_s)
end

#non_command_argsObject



65
66
67
# File 'lib/belafonte/help/app_extensions.rb', line 65

def non_command_args
  configured_args.reject {|arg| arg.name.to_sym == :command}
end

#rootObject



4
5
6
7
8
# File 'lib/belafonte/help/app_extensions.rb', line 4

def root
  return self unless parent
  parent.extend(AppExtensions)
  parent.root
end

#root?Boolean



10
11
12
# File 'lib/belafonte/help/app_extensions.rb', line 10

def root?
  root == self
end

#show_command_arg?Boolean



37
38
39
# File 'lib/belafonte/help/app_extensions.rb', line 37

def show_command_arg?
  Belafonte::Help::Generator.target == self && has_subcommands?
end

#signatureObject



55
56
57
58
59
# File 'lib/belafonte/help/app_extensions.rb', line 55

def signature
  display_title.tap {|cmd|
    return cmd + display_flags(cmd) + display_args + command_arg
  }
end

#sorted_commandsObject



86
87
88
89
# File 'lib/belafonte/help/app_extensions.rb', line 86

def sorted_commands
  configured_subcommands.
    sort {|left,right| left.name <=> right.name}
end

#sorted_flagsObject



81
82
83
84
# File 'lib/belafonte/help/app_extensions.rb', line 81

def sorted_flags
  (configured_switches + configured_options).
    sort {|left,right| left.name <=> right.name}
end