From 164f47a108c26ede3bf3236c4682ca74cc043e2f Mon Sep 17 00:00:00 2001 From: Gautham Goli Date: Sat, 24 Mar 2018 20:04:45 +0530 Subject: [PATCH 1/2] irb: Use Parser to parse args --- Library/Homebrew/dev-cmd/irb.rb | 14 +++++++++++--- Library/Homebrew/extend/ARGV.rb | 4 ---- Library/Homebrew/utils/env.rb | 9 +++++++++ 3 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 Library/Homebrew/utils/env.rb diff --git a/Library/Homebrew/dev-cmd/irb.rb b/Library/Homebrew/dev-cmd/irb.rb index 700cbe0097..ac27cf16af 100644 --- a/Library/Homebrew/dev-cmd/irb.rb +++ b/Library/Homebrew/dev-cmd/irb.rb @@ -5,6 +5,9 @@ #: If `--pry` is passed or HOMEBREW_PRY is set, pry will be #: used instead of irb. +require "cli_parser" +require "utils/env" + class Symbol def f(*args) Formulary.factory(to_s, *args) @@ -21,7 +24,12 @@ module Homebrew module_function def irb - if ARGV.include? "--examples" + args = Homebrew::CLI::Parser.new do + switch "--examples" + switch "--pry" + end.parse + + if args.examples? puts "'v8'.f # => instance of the v8 formula" puts ":hub.f.installed?" puts ":lua.f.methods - 1.methods" @@ -29,7 +37,7 @@ module Homebrew return end - if ARGV.pry? + if args.pry? || Utils::EnvVars.pry? Homebrew.install_gem_setup_path! "pry" require "pry" Pry.config.prompt_name = "brew" @@ -45,7 +53,7 @@ module Homebrew ohai "Interactive Homebrew Shell" puts "Example commands available with: brew irb --examples" - if ARGV.pry? + if args.pry? || Utils::EnvVars.pry? Pry.start else IRB.start diff --git a/Library/Homebrew/extend/ARGV.rb b/Library/Homebrew/extend/ARGV.rb index 8f8a63c7ef..0a66a0abe6 100644 --- a/Library/Homebrew/extend/ARGV.rb +++ b/Library/Homebrew/extend/ARGV.rb @@ -268,10 +268,6 @@ module HomebrewArgvExtension include? "--fetch-HEAD" end - def pry? - include?("--pry") || !ENV["HOMEBREW_PRY"].nil? - end - # eg. `foo -ns -i --bar` has three switches, n, s and i def switch?(char) return false if char.length > 1 diff --git a/Library/Homebrew/utils/env.rb b/Library/Homebrew/utils/env.rb new file mode 100644 index 0000000000..7a4709fb51 --- /dev/null +++ b/Library/Homebrew/utils/env.rb @@ -0,0 +1,9 @@ +module Utils + module EnvVars + class << self + def pry? + !ENV["HOMEBREW_PRY"].nil? + end + end + end +end From 99438e8e44ce7ce1f87334e70fb5850295c33e96 Mon Sep 17 00:00:00 2001 From: Gautham Goli Date: Sun, 25 Mar 2018 11:04:18 +0530 Subject: [PATCH 2/2] Parser: Add env named argument for switch to check environment variables --- Library/Homebrew/cli_parser.rb | 15 +++++++++++---- Library/Homebrew/dev-cmd/irb.rb | 7 +++---- Library/Homebrew/test/cli_parser_spec.rb | 7 +++++++ Library/Homebrew/utils/env.rb | 9 --------- 4 files changed, 21 insertions(+), 17 deletions(-) delete mode 100644 Library/Homebrew/utils/env.rb diff --git a/Library/Homebrew/cli_parser.rb b/Library/Homebrew/cli_parser.rb index 7d56d40089..c6eb823c30 100644 --- a/Library/Homebrew/cli_parser.rb +++ b/Library/Homebrew/cli_parser.rb @@ -10,13 +10,12 @@ module Homebrew instance_eval(&block) end - def switch(*names, description: nil) + def switch(*names, description: nil, env: nil) description = option_to_description(*names) if description.nil? @parser.on(*names, description) do - names.each do |name| - @parsed_args["#{option_to_name(name)}?"] = true - end + enable_switch(*names) end + enable_switch(*names) if !env.nil? && !ENV["HOMEBREW_#{env.to_s.upcase}"].nil? end def comma_array(name, description: nil) @@ -50,6 +49,14 @@ module Homebrew @parser.parse!(cmdline_args) @parsed_args end + + private + + def enable_switch(*names) + names.each do |name| + @parsed_args["#{option_to_name(name)}?"] = true + end + end end end end diff --git a/Library/Homebrew/dev-cmd/irb.rb b/Library/Homebrew/dev-cmd/irb.rb index ac27cf16af..b27cb4ab80 100644 --- a/Library/Homebrew/dev-cmd/irb.rb +++ b/Library/Homebrew/dev-cmd/irb.rb @@ -6,7 +6,6 @@ #: used instead of irb. require "cli_parser" -require "utils/env" class Symbol def f(*args) @@ -26,7 +25,7 @@ module Homebrew def irb args = Homebrew::CLI::Parser.new do switch "--examples" - switch "--pry" + switch "--pry", env: :pry end.parse if args.examples? @@ -37,7 +36,7 @@ module Homebrew return end - if args.pry? || Utils::EnvVars.pry? + if args.pry? Homebrew.install_gem_setup_path! "pry" require "pry" Pry.config.prompt_name = "brew" @@ -53,7 +52,7 @@ module Homebrew ohai "Interactive Homebrew Shell" puts "Example commands available with: brew irb --examples" - if args.pry? || Utils::EnvVars.pry? + if args.pry? Pry.start else IRB.start diff --git a/Library/Homebrew/test/cli_parser_spec.rb b/Library/Homebrew/test/cli_parser_spec.rb index 2c2c7d9d93..fadb5cc881 100644 --- a/Library/Homebrew/test/cli_parser_spec.rb +++ b/Library/Homebrew/test/cli_parser_spec.rb @@ -6,6 +6,7 @@ describe Homebrew::CLI::Parser do described_class.new do switch "-v", "--verbose", description: "Flag for verbosity" switch "--more-verbose", description: "Flag for higher verbosity" + switch "--pry", env: :pry end } @@ -34,6 +35,12 @@ describe Homebrew::CLI::Parser do it "raises an exception when an invalid option is passed" do expect { parser.parse(["--random"]) }.to raise_error(OptionParser::InvalidOption, /--random/) end + + it "maps environment var to an option" do + allow(ENV).to receive(:[]).with("HOMEBREW_PRY").and_return("1") + args = parser.parse([]) + expect(args.pry?).to be true + end end describe "test long flag options" do diff --git a/Library/Homebrew/utils/env.rb b/Library/Homebrew/utils/env.rb deleted file mode 100644 index 7a4709fb51..0000000000 --- a/Library/Homebrew/utils/env.rb +++ /dev/null @@ -1,9 +0,0 @@ -module Utils - module EnvVars - class << self - def pry? - !ENV["HOMEBREW_PRY"].nil? - end - end - end -end