Class: Brut::CLI::ParsedCommandLine
- Inherits:
-
Object
- Object
- Brut::CLI::ParsedCommandLine
- Defined in:
- lib/brut/cli/parsed_command_line.rb
Overview
Parses the command line and makes everything parsed available as attributes.
Instance Attribute Summary collapse
-
#argv ⇒ Array<String>
readonly
The remaining unparsed command line arguments.
-
#command ⇒ Brut::CLI::Commands::BaseCommand
readonly
The command that should be executed based on what was parsed from the command line.
-
#options ⇒ Brut::CLI::Options
readonly
The parsed command line switches and flags.
-
#project_environment ⇒ Brut::Framework::ProjectEnvironment
readonly
The project enviornment, if specified.
Instance Method Summary collapse
-
#initialize(app_command:, argv:, env:) ⇒ ParsedCommandLine
constructor
Create the ParsedCommandLine based on a base command (which provides the initial set of command line options), the ARGV and the ENV when the command was invoked.
Constructor Details
#initialize(app_command:, argv:, env:) ⇒ ParsedCommandLine
Create the ParsedCommandLine based on a base command (which provides the initial set of command line options), the ARGV and the ENV when the command was invoked.
This should always succeed, however depending on the contents of the parameters, the value
for #command may be a command that outputs an error.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/brut/cli/parsed_command_line.rb', line 31 def initialize(app_command:, argv:, env:) brut_provided_help_requested = false app_option_parser = new_option_parser do |opts| opts. = app_command.description app_command.accepts.each_with_index { |class_or_class_and_proc,index| accept(opts,class_or_class_and_proc,index) } app_command.opts.each do |option| opts.on(*option) end opts.on("--help", "-h", "Show help") do brut_provided_help_requested = true end end = { 'log-level': "error", } remaining_argv = app_option_parser.order!(argv,into: ) if remaining_argv[0] == "help" brut_provided_help_requested = true remaining_argv.shift end help_command = if brut_provided_help_requested Brut::CLI::Commands::Help.new(app_command,app_option_parser) end command = app_command loop do arg = remaining_argv.shift command_found = command.commands.detect { it.name == arg } if command_found command_found.parent_command = command command = command_found else if arg remaining_argv.unshift(arg) end break end end if command != app_command command_option_parser = new_option_parser do |opts| opts. = command.description app_command.accepts.each_with_index { |class_or_class_and_proc,index| accept(opts,class_or_class_and_proc,index) } command.accepts.each_with_index { |class_or_class_and_proc,index| accept(opts,class_or_class_and_proc,index) } opts.on("--help", "-h", "Show help") do brut_provided_help_requested = true end app_command.opts.each { opts.on(*it) } command.opts.each { opts.on(*it) } end remaining_argv = command_option_parser.parse!(remaining_argv, into: ) if brut_provided_help_requested help_command = Brut::CLI::Commands::Help.new(command,command_option_parser) elsif help_command help_command.option_parser = command_option_parser end end if help_command command = help_command else if remaining_argv.empty? && command.default_command command = command.default_command end end if [:env] @project_environment = .delete(:env) end @command = command @argv = remaining_argv @options = Brut::CLI::Options.new() rescue => ex @command = if env["BRUT_DEBUG"] == "true" Brut::CLI::Commands::RaiseError.new(ex) else Brut::CLI::Commands::OutputError.new(ex) end @argv = argv @options = Brut::CLI::Options.new({}) end |
Instance Attribute Details
#argv ⇒ Array<String> (readonly)
The remaining unparsed command line arguments.
15 16 17 |
# File 'lib/brut/cli/parsed_command_line.rb', line 15 def argv @argv end |
#command ⇒ Brut::CLI::Commands::BaseCommand (readonly)
The command that should be executed based on what was parsed from the command line
10 11 12 |
# File 'lib/brut/cli/parsed_command_line.rb', line 10 def command @command end |
#options ⇒ Brut::CLI::Options (readonly)
The parsed command line switches and flags.
20 21 22 |
# File 'lib/brut/cli/parsed_command_line.rb', line 20 def @options end |
#project_environment ⇒ Brut::Framework::ProjectEnvironment (readonly)
The project enviornment, if specified
24 25 26 |
# File 'lib/brut/cli/parsed_command_line.rb', line 24 def project_environment @project_environment end |