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.
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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/brut/cli/parsed_command_line.rb', line 32 def initialize(app_command:, argv:, env:) help_command_class = if env["BRUT_HELP_IN_MARKDOWN"] == "true" Brut::CLI::Commands::HelpInMarkdown else Brut::CLI::Commands::Help end brut_provided_help_requested = false app_option_parser = new_option_parser(app_command.name) 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 = {} 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 help_command_class.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(app_command.name) 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 = help_command_class.new(command,command_option_parser) elsif help_command help_command.option_parser = command_option_parser end end if help_command command = help_command end if [:env] @project_environment = .delete(:env) end @command = command @argv = remaining_argv @options = Brut::CLI::Options.new() if !@options.log_level? if @options.verbose? || @options.debug? @options[:'log-level'] = "debug" elsif @options.quiet? @options[:'log-level'] = "error" else @options[:'log-level'] = "info" end end if !@options[:'log-file'] log_file_path = if env["XDG_STATE_HOME"] Pathname(env["XDG_STATE_HOME"]) / "brut" elsif env["HOME"] && File.writable?(env["HOME"]) Pathname("#{env['HOME']}/.local/state/") / "brut" else nil end if log_file_path @options[:'log-file'] = log_file_path / (app_command.name + ".log") end else @options[:'log-file'] = Pathname(@options[:'log-file']) end if @options[:'log-stdout'].nil? @options[:'log-stdout'] = @options.verbose? || @options.debug? end 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.
16 17 18 |
# File 'lib/brut/cli/parsed_command_line.rb', line 16 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
11 12 13 |
# File 'lib/brut/cli/parsed_command_line.rb', line 11 def command @command end |
#options ⇒ Brut::CLI::Options (readonly)
The parsed command line switches and flags.
21 22 23 |
# File 'lib/brut/cli/parsed_command_line.rb', line 21 def @options end |
#project_environment ⇒ Brut::Framework::ProjectEnvironment (readonly)
The project enviornment, if specified
25 26 27 |
# File 'lib/brut/cli/parsed_command_line.rb', line 25 def project_environment @project_environment end |