Extract out arg parsing method to <cmd>_args method

This commit is contained in:
Gautham Goli 2018-07-30 18:25:38 +05:30
parent 32e5a5686b
commit b6c456b681
No known key found for this signature in database
GPG Key ID: 6A9ABBC284468364
16 changed files with 97 additions and 35 deletions

View File

@ -94,10 +94,11 @@ module Homebrew
@parser.to_s @parser.to_s
end end
def parse(cmdline_args) def parse(cmdline_args = ARGV)
remaining_args = @parser.parse(cmdline_args) remaining_args = @parser.parse(cmdline_args)
check_constraint_violations check_constraint_violations
Homebrew.args[:remaining] = remaining_args Homebrew.args[:remaining] = remaining_args
@parser
end end
private private

View File

@ -52,8 +52,8 @@ require "cli_parser"
module Homebrew module Homebrew
module_function module_function
def audit def audit_args
Homebrew::CLI::Parser.parse do Homebrew::CLI::Parser.new do
banner <<~EOS banner <<~EOS
Usage: brew audit [options] [<formulae>] Usage: brew audit [options] [<formulae>]
@ -61,6 +61,7 @@ module Homebrew
run before submitting a new formula. run before submitting a new formula.
If no <formulae> are provided, all of them are checked. If no <formulae> are provided, all of them are checked.
EOS EOS
switch "--strict", description: "Run additional style checks, including Rubocop style checks." switch "--strict", description: "Run additional style checks, including Rubocop style checks."
switch "--online", description: "Run additional slower style checks that require a\nnetwork connection." switch "--online", description: "Run additional slower style checks that require a\nnetwork connection."
@ -76,6 +77,10 @@ module Homebrew
switch :verbose switch :verbose
switch :debug switch :debug
end end
end
def audit
audit_args.parse
Homebrew.auditing = true Homebrew.auditing = true
inject_dump_stats!(FormulaAuditor, /^audit_/) if args.audit_debug? inject_dump_stats!(FormulaAuditor, /^audit_/) if args.audit_debug?

View File

@ -69,8 +69,8 @@ MAXIMUM_STRING_MATCHES = 100
module Homebrew module Homebrew
module_function module_function
def bottle def bottle_args
Homebrew::CLI::Parser.parse do Homebrew::CLI::Parser.new do
switch "--merge" switch "--merge"
switch "--skip-relocation" switch "--skip-relocation"
switch "--force-core-tap" switch "--force-core-tap"
@ -84,6 +84,10 @@ module Homebrew
switch :debug switch :debug
flag "--root-url" flag "--root-url"
end end
end
def bottle
bottle_args.parse
return merge if args.merge? return merge if args.merge?

View File

@ -47,8 +47,8 @@ require "cli_parser"
module Homebrew module Homebrew
module_function module_function
def bump_formula_pr def bump_formula_pr_args
Homebrew::CLI::Parser.parse do Homebrew::CLI::Parser.new do
switch "--devel" switch "--devel"
switch "-n", "--dry-run" switch "-n", "--dry-run"
switch "--write" switch "--write"
@ -70,6 +70,10 @@ module Homebrew
conflicts "--url", "--tag" conflicts "--url", "--tag"
end end
end
def bump_formula_pr
bump_formula_pr_args.parse
# As this command is simplifying user run commands then let's just use a # As this command is simplifying user run commands then let's just use a
# user path, too. # user path, too.

View File

@ -27,9 +27,8 @@ require "cli_parser"
module Homebrew module Homebrew
module_function module_function
# Create a formula from a tarball URL def create_args
def create Homebrew::CLI::Parser.new do
Homebrew::CLI::Parser.parse do
switch "--autotools" switch "--autotools"
switch "--cmake" switch "--cmake"
switch "--meson" switch "--meson"
@ -42,6 +41,12 @@ module Homebrew
flag "--set-version=" flag "--set-version="
flag "--tap=" flag "--tap="
end end
end
# Create a formula from a tarball URL
def create
create_args.parse
raise UsageError if ARGV.named.empty? raise UsageError if ARGV.named.empty?
# Ensure that the cache exists so we can fetch the tarball # Ensure that the cache exists so we can fetch the tarball

View File

@ -10,12 +10,16 @@ require "cli_parser"
module Homebrew module Homebrew
module_function module_function
def edit def edit_args
Homebrew::CLI::Parser.parse do Homebrew::CLI::Parser.new do
switch :force switch :force
switch :verbose switch :verbose
switch :debug switch :debug
end end
end
def edit
edit_args.parse
unless (HOMEBREW_REPOSITORY/".git").directory? unless (HOMEBREW_REPOSITORY/".git").directory?
raise <<~EOS raise <<~EOS

View File

@ -7,11 +7,15 @@ require "cli_parser"
module Homebrew module Homebrew
module_function module_function
def formula def formula_args
Homebrew::CLI::Parser.parse do Homebrew::CLI::Parser.new do
switch :debug switch :debug
switch :verbose switch :verbose
end end
end
def formula
formula_args.parse
raise FormulaUnspecifiedError if ARGV.named.empty? raise FormulaUnspecifiedError if ARGV.named.empty?

View File

@ -22,11 +22,15 @@ end
module Homebrew module Homebrew
module_function module_function
def irb def irb_args
Homebrew::CLI::Parser.parse do Homebrew::CLI::Parser.new do
switch "--examples" switch "--examples"
switch "--pry", env: :pry switch "--pry", env: :pry
end end
end
def irb
irb_args.parse
if args.examples? if args.examples?
puts "'v8'.f # => instance of the v8 formula" puts "'v8'.f # => instance of the v8 formula"

View File

@ -19,13 +19,17 @@ require "cli_parser"
module Homebrew module Homebrew
module_function module_function
def linkage def linkage_args
Homebrew::CLI::Parser.parse do Homebrew::CLI::Parser.new do
switch "--test" switch "--test"
switch "--reverse" switch "--reverse"
switch :verbose switch :verbose
switch :debug switch :debug
end end
end
def linkage
linkage_args.parse
CacheStoreDatabase.use(:linkage) do |db| CacheStoreDatabase.use(:linkage) do |db|
kegs = if ARGV.kegs.empty? kegs = if ARGV.kegs.empty?

View File

@ -11,6 +11,7 @@ require "formula"
require "erb" require "erb"
require "ostruct" require "ostruct"
require "cli_parser" require "cli_parser"
require "dev-cmd/audit"
module Homebrew module Homebrew
module_function module_function
@ -19,11 +20,15 @@ module Homebrew
TARGET_MAN_PATH = HOMEBREW_REPOSITORY/"manpages" TARGET_MAN_PATH = HOMEBREW_REPOSITORY/"manpages"
TARGET_DOC_PATH = HOMEBREW_REPOSITORY/"docs" TARGET_DOC_PATH = HOMEBREW_REPOSITORY/"docs"
def man def man_args
Homebrew::CLI::Parser.parse do Homebrew::CLI::Parser.new do
switch "--fail-if-changed" switch "--fail-if-changed"
switch "--link" switch "--link"
end end
end
def man
man_args.parse
raise UsageError unless ARGV.named.empty? raise UsageError unless ARGV.named.empty?
@ -64,7 +69,7 @@ module Homebrew
variables = OpenStruct.new variables = OpenStruct.new
variables[:commands] = path_glob_commands("#{HOMEBREW_LIBRARY_PATH}/cmd/*.{rb,sh}") variables[:commands] = path_glob_commands("#{HOMEBREW_LIBRARY_PATH}/cmd/*.{rb,sh}")
variables[:developer_commands] = path_glob_commands("#{HOMEBREW_LIBRARY_PATH}/dev-cmd/*.{rb,sh}") variables[:developer_commands] = [Homebrew.send(:audit_args).summary] + path_glob_commands("#{HOMEBREW_LIBRARY_PATH}/dev-cmd/*.{rb,sh}")
readme = HOMEBREW_REPOSITORY/"README.md" readme = HOMEBREW_REPOSITORY/"README.md"
variables[:lead_maintainer] = variables[:lead_maintainer] =
readme.read[/(Homebrew's lead maintainer .*\.)/, 1] readme.read[/(Homebrew's lead maintainer .*\.)/, 1]

View File

@ -7,11 +7,15 @@ require "cli_parser"
module Homebrew module Homebrew
module_function module_function
def mirror def mirror_args
Homebrew::CLI::Parser.parse do Homebrew::CLI::Parser.new do
switch :debug switch :debug
switch :verbose switch :verbose
end end
end
def mirror
mirror_args.parse
odie "This command requires at least formula argument!" if ARGV.named.empty? odie "This command requires at least formula argument!" if ARGV.named.empty?

View File

@ -72,10 +72,8 @@ end
module Homebrew module Homebrew
module_function module_function
def pull def pull_args
odie "You meant `git pull --rebase`." if ARGV[0] == "--rebase" Homebrew::CLI::Parser.new do
Homebrew::CLI::Parser.parse do
switch "--bottle" switch "--bottle"
switch "--bump" switch "--bump"
switch "--clean" switch "--clean"
@ -90,6 +88,12 @@ module Homebrew
flag "--bintray-org=" flag "--bintray-org="
flag "--test-bot-user=" flag "--test-bot-user="
end end
end
def pull
odie "You meant `git pull --rebase`." if ARGV[0] == "--rebase"
pull_args.parse
if ARGV.named.empty? if ARGV.named.empty?
odie "This command requires at least one argument containing a URL or pull request number" odie "This command requires at least one argument containing a URL or pull request number"

View File

@ -10,10 +10,13 @@ require "cli_parser"
module Homebrew module Homebrew
module_function module_function
def release_notes def release_notes_args
Homebrew::CLI::Parser.parse do Homebrew::CLI::Parser.new do
switch "--markdown" switch "--markdown"
end end
end
def release_notes
release_notes_args.parse
previous_tag = ARGV.named.first previous_tag = ARGV.named.first
previous_tag ||= Utils.popen_read( previous_tag ||= Utils.popen_read(

View File

@ -15,11 +15,15 @@ module Homebrew
path.write content path.write content
end end
def tap_new def tap_new_args
Homebrew::CLI::Parser.parse do Homebrew::CLI::Parser.new do
switch :debug switch :debug
switch :verbose switch :verbose
end end
end
def tap_new
tap_new_args.parse
raise "A tap argument is required" if ARGV.named.empty? raise "A tap argument is required" if ARGV.named.empty?

View File

@ -21,8 +21,8 @@ require "fileutils"
module Homebrew module Homebrew
module_function module_function
def tests def tests_args
Homebrew::CLI::Parser.parse do Homebrew::CLI::Parser.new do
switch "--no-compat" switch "--no-compat"
switch "--generic" switch "--generic"
switch "--coverage" switch "--coverage"
@ -32,6 +32,9 @@ module Homebrew
flag "--only=" flag "--only="
flag "--seed=" flag "--seed="
end end
end
def tests
tests_args.parse
HOMEBREW_LIBRARY_PATH.cd do HOMEBREW_LIBRARY_PATH.cd do
ENV.delete("HOMEBREW_COLOR") ENV.delete("HOMEBREW_COLOR")

View File

@ -19,8 +19,8 @@ require "cli_parser"
module Homebrew module Homebrew
module_function module_function
def update_test def update_test_args
Homebrew::CLI::Parser.parse do Homebrew::CLI::Parser.new do
switch "--to-tag" switch "--to-tag"
switch "--keep-tmp" switch "--keep-tmp"
switch :verbose switch :verbose
@ -28,6 +28,10 @@ module Homebrew
flag "--commit=" flag "--commit="
flag "--before=" flag "--before="
end end
end
def update_test
update_test_args.parse
ENV["HOMEBREW_UPDATE_TEST"] = "1" ENV["HOMEBREW_UPDATE_TEST"] = "1"