Skip to content

Commit

Permalink
auto-sort options for output
Browse files Browse the repository at this point in the history
  • Loading branch information
jimweirich committed Oct 19, 2012
1 parent c246fd2 commit 6d2cbda
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 141 deletions.
288 changes: 148 additions & 140 deletions lib/rake/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -287,151 +287,159 @@ def display_prerequisites
end
end

def sort_options(options)
options.sort_by { |opt|
opt.select { |o| o =~ /^-/ }.map { |o| o.downcase }.sort.reverse
}
end
private :sort_options

# A list of all the standard options used in rake, suitable for
# passing to OptionParser.
def standard_rake_options
[
['--all', '-A', "Show all tasks, even uncommented ones",
lambda { |value|
options.show_all_tasks = value
}
],
['--backtrace', "Enable full backtrace.",
lambda { |value|
options.backtrace = value
}
],
['--classic-namespace', '-C', "Put Task and FileTask in the top level namespace",
lambda { |value|
require 'rake/classic_namespace'
options.classic_namespace = true
}
],
['--comments', "Show commented tasks only",
lambda { |value|
options.show_all_tasks = !value
}
],
['--no-deprecation-warnings', '-X', "Disable the deprecation warnings.",
lambda { |value|
options.ignore_deprecate = true
}
],
['--describe', '-D [PATTERN]', "Describe the tasks (matching optional PATTERN), then exit.",
lambda { |value|
select_tasks_to_show :describe, value, options
}
],
['--dry-run', '-n', "Do a dry run without executing actions.",
lambda { |value|
Rake.verbose(true)
Rake.nowrite(true)
options.dryrun = true
options.trace = true
}
],
['--execute', '-e CODE', "Execute some Ruby code and exit.",
lambda { |value|
eval(value)
exit
}
],
['--execute-print', '-p CODE', "Execute some Ruby code, print the result, then exit.",
lambda { |value|
puts eval(value)
exit
}
],
['--execute-continue', '-E CODE',
"Execute some Ruby code, then continue with normal task processing.",
lambda { |value| eval(value) }
],
['--libdir', '-I LIBDIR', "Include LIBDIR in the search path for required modules.",
lambda { |value| $:.push(value) }
],
['--no-search', '--nosearch', '-N', "Do not search parent directories for the Rakefile.",
lambda { |value| options.nosearch = true }
],
['--prereqs', '-P', "Display the tasks and dependencies, then exit.",
lambda { |value| options.show_prereqs = true }
],
['--quiet', '-q', "Do not log messages to standard output.",
lambda { |value| Rake.verbose(false) }
],
['--rakefile', '-f [FILE]', "Use FILE as the rakefile.",
lambda { |value|
value ||= ''
@rakefiles.clear
@rakefiles << value
}
],
['--rakelibdir', '--rakelib', '-R RAKELIBDIR',
"Auto-import any .rake files in RAKELIBDIR. (default is 'rakelib')",
lambda { |value| options.rakelib = value.split(File::PATH_SEPARATOR) }
],
['--reduce-compat', "Remove DSL in Object; remove Module#const_missing which defines ::Task etc.",
# Load-time option.
# Handled in bin/rake where Rake::REDUCE_COMPAT is defined (or not).
lambda { |_| }
],
['--require', '-r MODULE', "Require MODULE before executing rakefile.",
lambda { |value|
begin
require value
rescue LoadError => ex
sort_options(
[
['--all', '-A', "Show all tasks, even uncommented ones",
lambda { |value|
options.show_all_tasks = value
}
],
['--backtrace', "Enable full backtrace.",
lambda { |value|
options.backtrace = value
}
],
['--classic-namespace', '-C', "Put Task and FileTask in the top level namespace",
lambda { |value|
require 'rake/classic_namespace'
options.classic_namespace = true
}
],
['--comments', "Show commented tasks only",
lambda { |value|
options.show_all_tasks = !value
}
],
['--describe', '-D [PATTERN]', "Describe the tasks (matching optional PATTERN), then exit.",
lambda { |value|
select_tasks_to_show :describe, value, options
}
],
['--dry-run', '-n', "Do a dry run without executing actions.",
lambda { |value|
Rake.verbose(true)
Rake.nowrite(true)
options.dryrun = true
options.trace = true
}
],
['--execute', '-e CODE', "Execute some Ruby code and exit.",
lambda { |value|
eval(value)
exit
}
],
['--execute-print', '-p CODE', "Execute some Ruby code, print the result, then exit.",
lambda { |value|
puts eval(value)
exit
}
],
['--execute-continue', '-E CODE',
"Execute some Ruby code, then continue with normal task processing.",
lambda { |value| eval(value) }
],
['--libdir', '-I LIBDIR', "Include LIBDIR in the search path for required modules.",
lambda { |value| $:.push(value) }
],
['--no-search', '--nosearch', '-N', "Do not search parent directories for the Rakefile.",
lambda { |value| options.nosearch = true }
],
['--prereqs', '-P', "Display the tasks and dependencies, then exit.",
lambda { |value| options.show_prereqs = true }
],
['--quiet', '-q', "Do not log messages to standard output.",
lambda { |value| Rake.verbose(false) }
],
['--rakefile', '-f [FILE]', "Use FILE as the rakefile.",
lambda { |value|
value ||= ''
@rakefiles.clear
@rakefiles << value
}
],
['--rakelibdir', '--rakelib', '-R RAKELIBDIR',
"Auto-import any .rake files in RAKELIBDIR. (default is 'rakelib')",
lambda { |value| options.rakelib = value.split(File::PATH_SEPARATOR) }
],
['--reduce-compat', "Remove DSL in Object; remove Module#const_missing which defines ::Task etc.",
# Load-time option.
# Handled in bin/rake where Rake::REDUCE_COMPAT is defined (or not).
lambda { |_| }
],
['--require', '-r MODULE', "Require MODULE before executing rakefile.",
lambda { |value|
begin
rake_require value
rescue LoadError
raise ex
require value
rescue LoadError => ex
begin
rake_require value
rescue LoadError
raise ex
end
end
end
}
],
['--rules', "Trace the rules resolution.",
lambda { |value| options.trace_rules = true }
],
['--silent', '-s', "Like --quiet, but also suppresses the 'in directory' announcement.",
lambda { |value|
Rake.verbose(false)
options.silent = true
}
],
['--system', '-g',
"Using system wide (global) rakefiles (usually '~/.rake/*.rake').",
lambda { |value| options.load_system = true }
],
['--no-system', '--nosystem', '-G',
"Use standard project Rakefile search paths, ignore system wide rakefiles.",
lambda { |value| options.ignore_system = true }
],
['--tasks', '-T [PATTERN]', "Display the tasks (matching optional PATTERN) with descriptions, then exit.",
lambda { |value|
select_tasks_to_show :tasks, value, options
}
],
['--trace', '-t', "Turn on invoke/execute tracing, enable full backtrace.",
lambda { |value|
options.trace = true
options.backtrace = true
Rake.verbose(true)
}
],
['--verbose', '-v', "Log message to standard output.",
lambda { |value| Rake.verbose(true) }
],
['--version', '-V', "Display the program version.",
lambda { |value|
puts "rake, version #{RAKEVERSION}"
exit
}
],
['--where', '-W [PATTERN]', "Describe the tasks (matching optional PATTERN), then exit.",
lambda { |value|
select_tasks_to_show :lines, value, options
options.show_all_tasks = true
}
],
]
}
],
['--rules', "Trace the rules resolution.",
lambda { |value| options.trace_rules = true }
],
['--silent', '-s', "Like --quiet, but also suppresses the 'in directory' announcement.",
lambda { |value|
Rake.verbose(false)
options.silent = true
}
],
['--system', '-g',
"Using system wide (global) rakefiles (usually '~/.rake/*.rake').",
lambda { |value| options.load_system = true }
],
['--no-system', '--nosystem', '-G',
"Use standard project Rakefile search paths, ignore system wide rakefiles.",
lambda { |value| options.ignore_system = true }
],
['--tasks', '-T [PATTERN]', "Display the tasks (matching optional PATTERN) with descriptions, then exit.",
lambda { |value|
select_tasks_to_show :tasks, value, options
}
],
['--trace', '-t', "Turn on invoke/execute tracing, enable full backtrace.",
lambda { |value|
options.trace = true
options.backtrace = true
Rake.verbose(true)
}
],
['--verbose', '-v', "Log message to standard output.",
lambda { |value| Rake.verbose(true) }
],
['--version', '-V', "Display the program version.",
lambda { |value|
puts "rake, version #{RAKEVERSION}"
exit
}
],
['--where', '-W [PATTERN]', "Describe the tasks (matching optional PATTERN), then exit.",
lambda { |value|
select_tasks_to_show :lines, value, options
options.show_all_tasks = true
}
],
['--no-deprecation-warnings', '-X', "Disable the deprecation warnings.",
lambda { |value|
options.ignore_deprecate = true
}
],
])
end

def select_tasks_to_show(show_tasks, value, options)
Expand Down
2 changes: 1 addition & 1 deletion test/test_rake_application_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def test_classic_namespace
assert_equal opts.trace, $trace
assert_equal opts.dryrun, $dryrun
assert_equal opts.silent, $silent
end
end
end

assert_match(/deprecated/, err)
Expand Down

0 comments on commit 6d2cbda

Please sign in to comment.