Class: Pantry::CommandLine
- Inherits:
-
Object
- Object
- Pantry::CommandLine
- Defined in:
- lib/pantry/command_line.rb
Constant Summary collapse
- BASE_OPTIONS =
The top-level set of CLI options and flags Pantry respects
proc { "Usage: #{File.basename($0)} [options] [command [command options]]" option "-h", "--host HOSTNAME", String, "Hostname of the Server to connect to" option "--curve-key-file FILE", String, "Name of the file in .pantry holding Curve keys.", "Specifying this option will turn on Curve encryption." option "-a", "--application APPLICATION", String, "Filter Clients by a specific APPLICATION" option "-e", "--environment ENVIRONMENT", String, "Filter Clients by a specific ENVIRONMENT" option "-r", "--roles ROLE1,ROLE2", Array, "Filter Clients by given ROLES" option "-v", "--verbose", "Verbose output (INFO)" option "-d", "--debug", "Even more Verbose output (DEBUG)" option "-V", "--version", "Print out Pantry's version" }
Instance Method Summary collapse
-
#initialize(command_line) ⇒ CommandLine
constructor
A new instance of CommandLine.
-
#parse! ⇒ Object
Parse the full command line.
-
#triggered_command ⇒ Object
Returns details of the command found during parsing.
Constructor Details
#initialize(command_line) ⇒ CommandLine
Returns a new instance of CommandLine.
20 21 22 23 24 25 |
# File 'lib/pantry/command_line.rb', line 20 def initialize(command_line) @command_line = command_line @known_commands = {} @commands = find_all_cli_enabled_commands end |
Instance Method Details
#parse! ⇒ Object
Parse the full command line. Returns a hash containing the options found as well as what is still left on the command line. If the command line is empty, will default to –help.
Returns [nil, nil] if help was requested or there was a problem.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/pantry/command_line.rb', line 32 def parse! @command_line = merge_command_line_with_defaults(@command_line) parser = build_parser(@commands) begin if @command_line.empty? @command_line << "--help" end @parsed_options = parser.parse!(@command_line) if @parsed_options['help'] # Help printed already return [nil, nil] end [@parsed_options, @command_line] rescue => ex puts ex, "" puts parser.help [nil, nil] end end |
#triggered_command ⇒ Object
Returns details of the command found during parsing. Returns a hash with the keys banner
and class
, or returns nil if no matching command was found
59 60 61 62 63 64 |
# File 'lib/pantry/command_line.rb', line 59 def triggered_command [ @commands[@parsed_options.command_found], @parsed_options[@parsed_options.command_found] ] end |