[DRE-commits] [ruby-cri] 02/06: New upstream version 2.8.0
Cédric Boutillier
boutil at moszumanska.debian.org
Sat Apr 22 22:09:15 UTC 2017
This is an automated email from the git hooks/post-receive script.
boutil pushed a commit to annotated tag debian/2.8.0-1_exp1
in repository ruby-cri.
commit ffc8198ca97881e6ea22fdc600547713e1040fe4
Author: Cédric Boutillier <boutil at debian.org>
Date: Fri Apr 21 22:38:30 2017 +0200
New upstream version 2.8.0
---
Gemfile | 6 +-
Gemfile.lock | 71 ++++++++++------------
NEWS.md | 15 +++++
README.adoc | 22 +++++--
Rakefile | 17 +++---
cri.gemspec | 4 +-
lib/cri.rb | 4 +-
lib/cri/argument_array.rb | 2 -
lib/cri/command.rb | 83 ++++++++++++++++---------
lib/cri/command_dsl.rb | 45 ++++++++------
lib/cri/command_runner.rb | 4 +-
lib/cri/commands/basic_help.rb | 12 ++--
lib/cri/commands/basic_root.rb | 4 +-
lib/cri/core_ext.rb | 2 -
lib/cri/core_ext/string.rb | 2 -
lib/cri/help_renderer.rb | 19 ++----
lib/cri/option_parser.rb | 26 ++++----
lib/cri/platform.rb | 2 -
lib/cri/string_formatter.rb | 4 +-
lib/cri/version.rb | 4 +-
metadata.yml | 109 ---------------------------------
test/helper.rb | 8 +--
test/test_argument_array.rb | 2 +-
test/test_base.rb | 5 +-
test/test_basic_help.rb | 2 +-
test/test_basic_root.rb | 17 +++++-
test/test_command.rb | 133 +++++++++++++++++++++++++++++++----------
test/test_command_dsl.rb | 62 +++++++++----------
test/test_command_runner.rb | 6 +-
test/test_option_parser.rb | 122 ++++++++++++++++++-------------------
test/test_string_formatter.rb | 2 +-
31 files changed, 403 insertions(+), 413 deletions(-)
diff --git a/Gemfile b/Gemfile
index 0760b27..f70f9f0 100644
--- a/Gemfile
+++ b/Gemfile
@@ -2,9 +2,9 @@ source 'https://rubygems.org'
gemspec
+gem 'asciidoctor'
gem 'coveralls'
-gem 'rake'
gem 'minitest'
-gem 'yard'
-gem 'asciidoctor'
+gem 'rake'
gem 'rubocop'
+gem 'yard'
diff --git a/Gemfile.lock b/Gemfile.lock
index 9b1e494..6e2bc01 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -1,61 +1,47 @@
PATH
remote: .
specs:
- cri (2.7.0)
+ cri (2.7.1)
colored (~> 1.2)
GEM
remote: https://rubygems.org/
specs:
- asciidoctor (1.5.2)
- ast (2.0.0)
- astrolabe (1.3.0)
- parser (>= 2.2.0.pre.3, < 3.0)
+ asciidoctor (1.5.5)
+ ast (2.3.0)
colored (1.2)
- coveralls (0.8.1)
- json (~> 1.8)
- rest-client (>= 1.6.8, < 2)
- simplecov (~> 0.10.0)
+ coveralls (0.8.20)
+ json (>= 1.8, < 3)
+ simplecov (~> 0.14.1)
term-ansicolor (~> 1.3)
- thor (~> 0.19.1)
+ thor (~> 0.19.4)
+ tins (~> 1.6)
docile (1.1.5)
- domain_name (0.5.24)
- unf (>= 0.0.5, < 1.0.0)
- http-cookie (1.0.2)
- domain_name (~> 0.5)
- json (1.8.2)
- mime-types (2.5)
- minitest (5.6.1)
- netrc (0.10.3)
- parser (2.2.2.2)
- ast (>= 1.1, < 3.0)
- powerpack (0.1.0)
- rainbow (2.0.0)
- rake (10.4.2)
- rest-client (1.8.0)
- http-cookie (>= 1.0.2, < 2.0)
- mime-types (>= 1.16, < 3.0)
- netrc (~> 0.7)
- rubocop (0.30.1)
- astrolabe (~> 1.3)
- parser (>= 2.2.2.1, < 3.0)
+ json (2.0.3)
+ minitest (5.10.1)
+ parser (2.4.0.0)
+ ast (~> 2.2)
+ powerpack (0.1.1)
+ rainbow (2.2.1)
+ rake (12.0.0)
+ rubocop (0.48.0)
+ parser (>= 2.3.3.1, < 3.0)
powerpack (~> 0.1)
rainbow (>= 1.99.1, < 3.0)
- ruby-progressbar (~> 1.4)
- ruby-progressbar (1.7.5)
- simplecov (0.10.0)
+ ruby-progressbar (~> 1.7)
+ unicode-display_width (~> 1.0, >= 1.0.1)
+ ruby-progressbar (1.8.1)
+ simplecov (0.14.1)
docile (~> 1.1.0)
- json (~> 1.8)
+ json (>= 1.8, < 3)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.0)
- term-ansicolor (1.3.0)
+ term-ansicolor (1.5.0)
tins (~> 1.0)
- thor (0.19.1)
- tins (1.5.1)
- unf (0.1.4)
- unf_ext
- unf_ext (0.0.7.1)
- yard (0.8.7.6)
+ thor (0.19.4)
+ tins (1.13.2)
+ unicode-display_width (1.1.3)
+ yard (0.9.8)
PLATFORMS
ruby
@@ -69,3 +55,6 @@ DEPENDENCIES
rake
rubocop
yard
+
+BUNDLED WITH
+ 1.14.6
diff --git a/NEWS.md b/NEWS.md
index 4ca9798..358ebae 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -1,6 +1,21 @@
Cri News
========
+2.8.0
+-----
+
+Features:
+
+* Allow passing `hard_exit: false` to `Command#run` to prevent `SystemExit` (#51)
+* Allow specifying the default subcommand (#54)
+
+2.7.1
+-----
+
+Fixes:
+
+* Fixed some grammatical mistakes
+
2.7.0
-----
diff --git a/README.adoc b/README.adoc
index 06ab264..9e39b51 100644
--- a/README.adoc
+++ b/README.adoc
@@ -6,13 +6,17 @@ link:https://coveralls.io/r/ddfreyne/cri[image:http://img.shields.io/coveralls/d
link:https://codeclimate.com/github/ddfreyne/cri[image:http://img.shields.io/codeclimate/github/ddfreyne/cri.svg[]]
link:http://inch-ci.org/github/ddfreyne/cri/[image:http://inch-ci.org/github/ddfreyne/cri.svg[]]
-Cri is a library for building easy-to-use commandline tools with support for
+Cri is a library for building easy-to-use command-line tools with support for
nested commands.
+== Requirements ==
+
+Cri requires Ruby 2.1 or newer.
+
== Usage ==
The central concept in Cri is the _command_, which has option definitions as
-well as code for actually executing itself. In Cri, the commandline tool
+well as code for actually executing itself. In Cri, the command-line tool
itself is a command as well.
Here’s a sample command definition:
@@ -45,7 +49,7 @@ end
--------------------------------------------------------------------------------
To run this command, invoke the `#run` method with the raw arguments. For
-example, for a root command (the commandline tool itself), the command could
+example, for a root command (the command-line tool itself), the command could
be called like this:
[source,ruby]
@@ -84,7 +88,7 @@ description 'This command does a lot of stuff. I really mean a lot.'
--------------------------------------------------------------------------------
These lines of the command definition specify the name of the command (or the
-commandline tool, if the command is the root command), the usage, a list of
+command-line tool, if the command is the root command), the usage, a list of
aliases that can be used to call this command, a one-line summary and a (long)
description. The usage should not include a “usage:” prefix nor the name of
the supercommand, because the latter will be automatically prepended.
@@ -174,7 +178,7 @@ into manageable pieces.
=== Subcommands ===
-Commands can have subcommands. For example, the `git` commandline tool would be
+Commands can have subcommands. For example, the `git` command-line tool would be
represented by a command that has subcommands named `commit`, `add`, and so on.
Commands with subcommands do not use a run block; execution will always be
dispatched to a subcommand (or none, if no subcommand is found).
@@ -189,6 +193,14 @@ root_cmd.add_command(cmd_commit)
root.cmd.add_command(cmd_init)
--------------------------------------------------------------------------------
+You can specify a default subcommand. This subcommand will be executed when the
+command has subcommands, and no subcommands are otherwise explicitly specified:
+
+[source,ruby]
+--------------------------------------------------------------------------------
+default_subcommand 'compile'
+--------------------------------------------------------------------------------
+
== Contributors ==
* Toon Willems
diff --git a/Rakefile b/Rakefile
index e6bcdee..71fdd58 100644
--- a/Rakefile
+++ b/Rakefile
@@ -14,16 +14,15 @@ YARD::Rake::YardocTask.new(:doc) do |yard|
]
end
-task :test_unit do
- require './test/helper.rb'
-
- FileList['./test/**/test_*.rb', './test/**/*_spec.rb'].each do |fn|
- require fn
- end
+Rake::TestTask.new(:test_unit) do |t|
+ t.test_files = Dir['test/**/*_spec.rb'] + Dir['test/**/test_*.rb']
+ t.libs << 'test'
end
-RuboCop::RakeTask.new(:test_style)
+RuboCop::RakeTask.new(:test_style) do |task|
+ task.options = %w(--display-cop-names --format simple)
+end
-task :test => [:test_unit, :test_style]
+task test: %i(test_unit test_style)
-task :default => :test
+task default: :test
diff --git a/cri.gemspec b/cri.gemspec
index b524f6f..7b1c053 100644
--- a/cri.gemspec
+++ b/cri.gemspec
@@ -7,8 +7,8 @@ Gem::Specification.new do |s|
s.name = 'cri'
s.version = Cri::VERSION
s.homepage = 'http://stoneship.org/software/cri/' # TODO: CREATE A WEB SITE YOU SILLY PERSON
- s.summary = 'a library for building easy-to-use commandline tools'
- s.description = 'Cri allows building easy-to-use commandline interfaces with support for subcommands.'
+ s.summary = 'a library for building easy-to-use command-line tools'
+ s.description = 'Cri allows building easy-to-use command-line interfaces with support for subcommands.'
s.license = 'MIT'
s.author = 'Denis Defreyne'
diff --git a/lib/cri.rb b/lib/cri.rb
index c56c70a..58611be 100644
--- a/lib/cri.rb
+++ b/lib/cri.rb
@@ -1,8 +1,6 @@
-# encoding: utf-8
-
require 'cri/version'
-# The namespace for Cri, a library for building easy-to-use commandline tools
+# The namespace for Cri, a library for building easy-to-use command-line tools
# with support for nested commands.
module Cri
# A generic error class for all Cri-specific errors.
diff --git a/lib/cri/argument_array.rb b/lib/cri/argument_array.rb
index f26a173..6bf813e 100644
--- a/lib/cri/argument_array.rb
+++ b/lib/cri/argument_array.rb
@@ -1,5 +1,3 @@
-# encoding: utf-8
-
module Cri
# Represents an array of arguments. It is an array that strips separator
# arguments (`--`) but provides a `#raw` method to get the raw arguments
diff --git a/lib/cri/command.rb b/lib/cri/command.rb
index bec9014..4fbde84 100644
--- a/lib/cri/command.rb
+++ b/lib/cri/command.rb
@@ -1,8 +1,6 @@
-# encoding: utf-8
-
module Cri
- # Cri::Command represents a command that can be executed on the commandline.
- # It is also used for the commandline tool itself.
+ # Cri::Command represents a command that can be executed on the command line.
+ # It is also used for the command-line tool itself.
class Command
# Delegate used for partitioning the list of arguments and options. This
# delegate will stop the parser as soon as the first argument, i.e. the
@@ -25,8 +23,7 @@ module Cri
# @param [Cri::OptionParser] option_parser The option parser
#
# @return [void]
- def option_added(_key, _value, _option_parser)
- end
+ def option_added(_key, _value, _option_parser); end
# Called when an argument is parsed.
#
@@ -41,13 +38,30 @@ module Cri
end
end
+ # Signals that Cri should abort execution. Unless otherwise specified using the `hard_exit`
+ # param, this exception will cause Cri to exit the running process.
+ #
+ # @api private
+ class CriExitException < StandardError
+ def initialize(is_error:)
+ @is_error = is_error
+ end
+
+ def error?
+ @is_error
+ end
+ end
+
# @return [Cri::Command, nil] This command’s supercommand, or nil if the
# command has no supercommand
attr_accessor :supercommand
# @return [Set<Cri::Command>] This command’s subcommands
attr_accessor :commands
- alias_method :subcommands, :commands
+ alias subcommands commands
+
+ # @return [Symbol] The name of the default subcommand
+ attr_accessor :default_subcommand_name
# @return [String] The name
attr_accessor :name
@@ -69,7 +83,7 @@ module Cri
# @return [Boolean] true if the command is hidden (e.g. because it is
# deprecated), false otherwise
attr_accessor :hidden
- alias_method :hidden?, :hidden
+ alias hidden? hidden
# @return [Array<Hash>] The list of option definitions
attr_accessor :option_definitions
@@ -126,6 +140,7 @@ module Cri
@aliases = Set.new
@commands = Set.new
@option_definitions = Set.new
+ @default_subcommand_name = nil
end
# Modifies the command using the DSL.
@@ -140,7 +155,7 @@ module Cri
if [-1, 0].include? block.arity
dsl.instance_eval(&block)
else
- block.call(dsl)
+ yield(dsl)
end
self
end
@@ -177,7 +192,7 @@ module Cri
if [-1, 0].include? block.arity
dsl.instance_eval(&block)
else
- block.call(dsl)
+ yield(dsl)
end
# Create command
@@ -214,22 +229,24 @@ module Cri
# @param [String] name The full, partial or aliases name of the command
#
# @return [Cri::Command] The command with the given name
- def command_named(name)
+ def command_named(name, hard_exit: true)
commands = commands_named(name)
- if commands.size < 1
+ if commands.empty?
$stderr.puts "#{self.name}: unknown command '#{name}'\n"
- exit 1
+ raise CriExitException.new(is_error: true)
elsif commands.size > 1
$stderr.puts "#{self.name}: '#{name}' is ambiguous:"
- $stderr.puts " #{commands.map { |c| c.name }.sort.join(' ') }"
- exit 1
+ $stderr.puts " #{commands.map(&:name).sort.join(' ')}"
+ raise CriExitException.new(is_error: true)
else
commands[0]
end
+ rescue CriExitException => e
+ exit(e.error? ? 1 : 0) if hard_exit
end
- # Runs the command with the given commandline arguments, possibly invoking
+ # Runs the command with the given command-line arguments, possibly invoking
# subcommands and passing on the options and arguments.
#
# @param [Array<String>] opts_and_args A list of unparsed arguments
@@ -238,7 +255,7 @@ module Cri
# supercommand
#
# @return [void]
- def run(opts_and_args, parent_opts = {})
+ def run(opts_and_args, parent_opts = {}, hard_exit: true)
# Parse up to command name
stuff = partition(opts_and_args)
opts_before_subcmd, subcmd_name, opts_and_args_after_subcmd = *stuff
@@ -251,17 +268,24 @@ module Cri
# Get command
if subcmd_name.nil?
- $stderr.puts "#{name}: no command given"
- exit 1
+ if default_subcommand_name
+ subcmd_name = default_subcommand_name
+ else
+ $stderr.puts "#{name}: no command given"
+ raise CriExitException.new(is_error: true)
+ end
end
- subcommand = command_named(subcmd_name)
+ subcommand = command_named(subcmd_name, hard_exit: hard_exit)
+ return if subcommand.nil?
# Run
- subcommand.run(opts_and_args_after_subcmd, opts_before_subcmd)
+ subcommand.run(opts_and_args_after_subcmd, opts_before_subcmd, hard_exit: hard_exit)
end
+ rescue CriExitException => e
+ exit(e.error? ? 1 : 0) if hard_exit
end
- # Runs the actual command with the given commandline arguments, not
+ # Runs the actual command with the given command-line arguments, not
# invoking any subcommands. If the command does not have an execution
# block, an error ir raised.
#
@@ -277,7 +301,8 @@ module Cri
def run_this(opts_and_args, parent_opts = {})
# Parse
parser = Cri::OptionParser.new(
- opts_and_args, global_option_definitions)
+ opts_and_args, global_option_definitions
+ )
handle_parser_errors_while { parser.run }
local_opts = parser.options
global_opts = parent_opts.merge(parser.options)
@@ -288,8 +313,8 @@ module Cri
# Execute
if block.nil?
- fail NotImplementedError,
- "No implementation available for '#{name}'"
+ raise NotImplementedError,
+ "No implementation available for '#{name}'"
end
block.call(global_opts, args, self)
end
@@ -341,14 +366,14 @@ module Cri
]
end
- def handle_parser_errors_while(&block)
- block.call
+ def handle_parser_errors_while
+ yield
rescue Cri::OptionParser::IllegalOptionError => e
$stderr.puts "#{name}: illegal option -- #{e}"
- exit 1
+ raise CriExitException.new(is_error: true)
rescue Cri::OptionParser::OptionRequiresAnArgumentError => e
$stderr.puts "#{name}: option requires an argument -- #{e}"
- exit 1
+ raise CriExitException.new(is_error: true)
end
end
end
diff --git a/lib/cri/command_dsl.rb b/lib/cri/command_dsl.rb
index 6797a5a..bdea814 100644
--- a/lib/cri/command_dsl.rb
+++ b/lib/cri/command_dsl.rb
@@ -1,5 +1,3 @@
-# encoding: utf-8
-
module Cri
# The command DSL is a class that is used for building and modifying
# commands.
@@ -33,6 +31,17 @@ module Cri
@command.add_command(command)
end
+ # Sets the name of the default subcommand, i.e. the subcommand that will
+ # be executed if no subcommand is explicitly specified. This is `nil` by
+ # default, and will typically only be set for the root command.
+ #
+ # @param [String, nil] name The name of the default subcommand
+ #
+ # @return [void]
+ def default_subcommand(name)
+ @command.default_subcommand_name = name
+ end
+
# Sets the command name.
#
# @param [String] arg The new command name
@@ -48,7 +57,7 @@ module Cri
#
# @return [void]
def aliases(*args)
- @command.aliases = args.flatten.map { |a| a.to_s }
+ @command.aliases = args.flatten.map(&:to_s)
end
# Sets the command summary.
@@ -114,20 +123,20 @@ module Cri
hidden = params.fetch(:hidden, false)
if short.nil? && long.nil?
- fail ArgumentError, 'short and long options cannot both be nil'
+ raise ArgumentError, 'short and long options cannot both be nil'
end
@command.option_definitions << {
- :short => short.nil? ? nil : short.to_s,
- :long => long.nil? ? nil : long.to_s,
- :desc => desc,
- :argument => requiredness,
- :multiple => multiple,
- :block => block,
- :hidden => hidden,
+ short: short.nil? ? nil : short.to_s,
+ long: long.nil? ? nil : long.to_s,
+ desc: desc,
+ argument: requiredness,
+ multiple: multiple,
+ block: block,
+ hidden: hidden,
}
end
- alias_method :opt, :option
+ alias opt option
# Adds a new option with a required argument to the command. If a block is
# given, it will be executed when the option is successfully parsed.
@@ -148,7 +157,7 @@ module Cri
#
# @see {#option}
def required(short, long, desc, params = {}, &block)
- params = params.merge(:argument => :required)
+ params = params.merge(argument: :required)
option(short, long, desc, params, &block)
end
@@ -171,10 +180,10 @@ module Cri
#
# @see {#option}
def flag(short, long, desc, params = {}, &block)
- params = params.merge(:argument => :forbidden)
+ params = params.merge(argument: :forbidden)
option(short, long, desc, params, &block)
end
- alias_method :forbidden, :flag
+ alias forbidden flag
# Adds a new option with an optional argument to the command. If a block
# is given, it will be executed when the option is successfully parsed.
@@ -195,7 +204,7 @@ module Cri
#
# @see {#option}
def optional(short, long, desc, params = {}, &block)
- params = params.merge(:argument => :optional)
+ params = params.merge(argument: :optional)
option(short, long, desc, params, &block)
end
@@ -213,8 +222,8 @@ module Cri
# @return [void]
def run(&block)
unless [2, 3].include?(block.arity)
- fail ArgumentError,
- 'The block given to Cri::Command#run expects two or three args'
+ raise ArgumentError,
+ 'The block given to Cri::Command#run expects two or three args'
end
@command.block = block
diff --git a/lib/cri/command_runner.rb b/lib/cri/command_runner.rb
index 322ad42..355fb36 100644
--- a/lib/cri/command_runner.rb
+++ b/lib/cri/command_runner.rb
@@ -1,5 +1,3 @@
-# encoding: utf-8
-
module Cri
# A command runner is responsible for the execution of a command. Using it
# is optional, but it is useful for commands whose execution block is large.
@@ -40,7 +38,7 @@ module Cri
#
# @abstract
def run
- fail NotImplementedError, 'Cri::CommandRunner subclasses must implement #run'
+ raise NotImplementedError, 'Cri::CommandRunner subclasses must implement #run'
end
end
end
diff --git a/lib/cri/commands/basic_help.rb b/lib/cri/commands/basic_help.rb
index 198dc1d..1e426e2 100644
--- a/lib/cri/commands/basic_help.rb
+++ b/lib/cri/commands/basic_help.rb
@@ -1,21 +1,19 @@
-# encoding: utf-8
-
name 'help'
usage 'help [command_name]'
summary 'show help'
description <<-EOS
Show help for the given command, or show general help. When no command is
given, a list of available commands is displayed, as well as a list of global
-commandline options. When a command is given, a command description as well as
-command-specific commandline options are shown.
+command-line options. When a command is given, a command description, as well
+as command-specific command-line options, are shown.
EOS
flag :v, :verbose, 'show more detailed help'
run do |opts, args, cmd|
if cmd.supercommand.nil?
- fail NoHelpAvailableError,
- 'No help available because the help command has no supercommand'
+ raise NoHelpAvailableError,
+ 'No help available because the help command has no supercommand'
end
is_verbose = opts.fetch(:verbose, false)
@@ -23,5 +21,5 @@ run do |opts, args, cmd|
resolved_cmd = args.inject(cmd.supercommand) do |acc, name|
acc.command_named(name)
end
- puts resolved_cmd.help(:verbose => is_verbose, :io => $stdout)
+ puts resolved_cmd.help(verbose: is_verbose, io: $stdout)
end
diff --git a/lib/cri/commands/basic_root.rb b/lib/cri/commands/basic_root.rb
index f922cac..f60cedd 100644
--- a/lib/cri/commands/basic_root.rb
+++ b/lib/cri/commands/basic_root.rb
@@ -1,8 +1,6 @@
-# encoding: utf-8
-
flag :h, :help, 'show help for this command' do |_value, cmd|
puts cmd.help
- exit 0
+ raise CriExitException.new(is_error: false)
end
subcommand Cri::Command.new_basic_help
diff --git a/lib/cri/core_ext.rb b/lib/cri/core_ext.rb
index c846986..b5d6783 100644
--- a/lib/cri/core_ext.rb
+++ b/lib/cri/core_ext.rb
@@ -1,5 +1,3 @@
-# encoding: utf-8
-
module Cri
module CoreExtensions
end
diff --git a/lib/cri/core_ext/string.rb b/lib/cri/core_ext/string.rb
index 211bf84..c289696 100644
--- a/lib/cri/core_ext/string.rb
+++ b/lib/cri/core_ext/string.rb
@@ -1,5 +1,3 @@
-# encoding: utf-8
-
require 'colored'
module Cri
diff --git a/lib/cri/help_renderer.rb b/lib/cri/help_renderer.rb
index e44c5c3..26124fb 100644
--- a/lib/cri/help_renderer.rb
+++ b/lib/cri/help_renderer.rb
@@ -1,5 +1,3 @@
-# encoding: utf-8
-
module Cri
# The {HelpRenderer} class is responsible for generating a string containing
# the help for a given command, intended to be printed on the command line.
@@ -86,22 +84,21 @@ module Cri
length = shown_subcommands.map { |c| fmt.format_as_command(c.name, @io).size }.max
# Command
- shown_subcommands.sort_by { |cmd| cmd.name }.each do |cmd|
+ shown_subcommands.sort_by(&:name).each do |cmd|
text <<
format(
" %-#{length + DESC_INDENT}s %s\n",
fmt.format_as_command(cmd.name, @io),
- cmd.summary)
+ cmd.summary,
+ )
end
# Hidden notice
unless @is_verbose
diff = @cmd.subcommands.size - shown_subcommands.size
- case diff
- when 0
- when 1
+ if diff == 1
text << " (1 hidden command omitted; show it with --verbose)\n"
- else
+ elsif diff > 1
text << " (#{diff} hidden commands omitted; show them with --verbose)\n"
end
end
@@ -145,7 +142,7 @@ module Cri
return if defs.empty?
text << "\n"
- text << fmt.format_as_title("#{name}", @io)
+ text << fmt.format_as_title(name.to_s, @io)
text << "\n"
ordered_defs = defs.sort_by { |x| x[:short] || x[:long] }
@@ -164,8 +161,6 @@ module Cri
'<value>'
when :optional
'[<value>]'
- else
- nil
end
if value_postfix
@@ -182,8 +177,6 @@ module Cri
'=<value>'
when :optional
'[=<value>]'
- else
- nil
end
if value_postfix
diff --git a/lib/cri/option_parser.rb b/lib/cri/option_parser.rb
index 1483f78..099215c 100644
--- a/lib/cri/option_parser.rb
+++ b/lib/cri/option_parser.rb
@@ -1,7 +1,5 @@
-# encoding: utf-8
-
module Cri
- # Cri::OptionParser is used for parsing commandline options.
+ # Cri::OptionParser is used for parsing command-line options.
#
# Option definitions are hashes with the keys `:short`, `:long` and
# `:argument` (optionally `:description` but this is not used by the
@@ -19,7 +17,7 @@ module Cri
# { :short => 'p', :long => 'port', :argument => :required, :multiple => false },
# ]
#
- # For example, the following commandline options (which should not be
+ # For example, the following command-line options (which should not be
# passed as a string, but as an array of strings):
#
# foo -xyz -a hiss -s -m please --level 50 --father=ani -n luke squeak
@@ -92,11 +90,11 @@ module Cri
# @return [Array] The not yet parsed options and arguments.
attr_reader :unprocessed_arguments_and_options
- # Parses the commandline arguments. See the instance `parse` method for
+ # Parses the command-line arguments. See the instance `parse` method for
# details.
#
# @param [Array<String>] arguments_and_options An array containing the
- # commandline arguments (will probably be `ARGS` for a root command)
+ # command-line arguments (will probably be `ARGS` for a root command)
#
# @param [Array<Hash>] definitions An array of option definitions
#
@@ -108,7 +106,7 @@ module Cri
# Creates a new parser with the given options/arguments and definitions.
#
# @param [Array<String>] arguments_and_options An array containing the
- # commandline arguments (will probably be `ARGS` for a root command)
+ # command-line arguments (will probably be `ARGS` for a root command)
#
# @param [Array<Hash>] definitions An array of option definitions
def initialize(arguments_and_options, definitions)
@@ -146,7 +144,7 @@ module Cri
@running = false
end
- # Parses the commandline arguments into options and arguments.
+ # Parses the command-line arguments into options and arguments.
#
# During parsing, two errors can be raised:
#
@@ -199,9 +197,9 @@ module Cri
# Find definition
definition = @definitions.find { |d| d[:long] == option_key }
- fail IllegalOptionError.new(option_key) if definition.nil?
+ raise IllegalOptionError.new(option_key) if definition.nil?
- if [:required, :optional].include?(definition[:argument])
+ if %i(required optional).include?(definition[:argument])
# Get option value if necessary
if option_value.nil?
option_value = find_option_value(definition, option_key)
@@ -223,12 +221,12 @@ module Cri
option_keys.each do |option_key|
# Find definition
definition = @definitions.find { |d| d[:short] == option_key }
- fail IllegalOptionError.new(option_key) if definition.nil?
+ raise IllegalOptionError.new(option_key) if definition.nil?
if option_keys.length > 1 && definition[:argument] == :required
# This is a combined option and it requires an argument, so complain
- fail OptionRequiresAnArgumentError.new(option_key)
- elsif [:required, :optional].include?(definition[:argument])
+ raise OptionRequiresAnArgumentError.new(option_key)
+ elsif %i(required optional).include?(definition[:argument])
# Get option value
option_value = find_option_value(definition, option_key)
@@ -245,7 +243,7 @@ module Cri
option_value = @unprocessed_arguments_and_options.shift
if option_value.nil? || option_value =~ /^-/
if definition[:argument] == :required
- fail OptionRequiresAnArgumentError.new(option_key)
+ raise OptionRequiresAnArgumentError.new(option_key)
else
@unprocessed_arguments_and_options.unshift(option_value)
option_value = true
diff --git a/lib/cri/platform.rb b/lib/cri/platform.rb
index 96be321..4bc80d8 100644
--- a/lib/cri/platform.rb
+++ b/lib/cri/platform.rb
@@ -1,5 +1,3 @@
-# encoding: utf-8
-
module Cri
module Platform
# @return [Boolean] true if the current platform is Windows, false
diff --git a/lib/cri/string_formatter.rb b/lib/cri/string_formatter.rb
index 416bbcf..630c591 100644
--- a/lib/cri/string_formatter.rb
+++ b/lib/cri/string_formatter.rb
@@ -1,5 +1,3 @@
-# encoding: utf-8
-
require 'colored'
module Cri
@@ -21,7 +19,7 @@ module Cri
end
end
- paragraphs.reject { |p| p.empty? }.map { |p| p.join(' ') }
+ paragraphs.reject(&:empty?).map { |p| p.join(' ') }
end
# Word-wraps and indents the string.
diff --git a/lib/cri/version.rb b/lib/cri/version.rb
index 8cd5036..ada39f6 100644
--- a/lib/cri/version.rb
+++ b/lib/cri/version.rb
@@ -1,6 +1,4 @@
-# encoding: utf-8
-
module Cri
# The current Cri version.
- VERSION = '2.7.0'
+ VERSION = '2.8.0'.freeze
end
diff --git a/metadata.yml b/metadata.yml
deleted file mode 100644
index 0bb4890..0000000
--- a/metadata.yml
+++ /dev/null
@@ -1,109 +0,0 @@
---- !ruby/object:Gem::Specification
-name: cri
-version: !ruby/object:Gem::Version
- version: 2.7.0
-platform: ruby
-authors:
-- Denis Defreyne
-autorequire:
-bindir: bin
-cert_chain: []
-date: 2015-04-29 00:00:00.000000000 Z
-dependencies:
-- !ruby/object:Gem::Dependency
- name: colored
- requirement: !ruby/object:Gem::Requirement
- requirements:
- - - "~>"
- - !ruby/object:Gem::Version
- version: '1.2'
- type: :runtime
- prerelease: false
- version_requirements: !ruby/object:Gem::Requirement
- requirements:
- - - "~>"
- - !ruby/object:Gem::Version
- version: '1.2'
-- !ruby/object:Gem::Dependency
- name: bundler
- requirement: !ruby/object:Gem::Requirement
- requirements:
- - - "~>"
- - !ruby/object:Gem::Version
- version: '1.6'
- type: :development
- prerelease: false
- version_requirements: !ruby/object:Gem::Requirement
- requirements:
- - - "~>"
- - !ruby/object:Gem::Version
- version: '1.6'
-description: Cri allows building easy-to-use commandline interfaces with support for
- subcommands.
-email: denis.defreyne at stoneship.org
-executables: []
-extensions: []
-extra_rdoc_files:
-- LICENSE
-- README.adoc
-- NEWS.md
-files:
-- Gemfile
-- Gemfile.lock
-- LICENSE
-- NEWS.md
-- README.adoc
-- Rakefile
-- cri.gemspec
-- lib/cri.rb
-- lib/cri/argument_array.rb
-- lib/cri/command.rb
-- lib/cri/command_dsl.rb
-- lib/cri/command_runner.rb
-- lib/cri/commands/basic_help.rb
-- lib/cri/commands/basic_root.rb
-- lib/cri/core_ext.rb
-- lib/cri/core_ext/string.rb
-- lib/cri/help_renderer.rb
-- lib/cri/option_parser.rb
-- lib/cri/platform.rb
-- lib/cri/string_formatter.rb
-- lib/cri/version.rb
-- test/helper.rb
-- test/test_argument_array.rb
-- test/test_base.rb
-- test/test_basic_help.rb
-- test/test_basic_root.rb
-- test/test_command.rb
-- test/test_command_dsl.rb
-- test/test_command_runner.rb
-- test/test_option_parser.rb
-- test/test_string_formatter.rb
-homepage: http://stoneship.org/software/cri/
-licenses:
-- MIT
-metadata: {}
-post_install_message:
-rdoc_options:
-- "--main"
-- README.adoc
-require_paths:
-- lib
-required_ruby_version: !ruby/object:Gem::Requirement
- requirements:
- - - ">="
- - !ruby/object:Gem::Version
- version: '0'
-required_rubygems_version: !ruby/object:Gem::Requirement
- requirements:
- - - ">="
- - !ruby/object:Gem::Version
- version: '0'
-requirements: []
-rubyforge_project:
-rubygems_version: 2.4.6
-signing_key:
-specification_version: 4
-summary: a library for building easy-to-use commandline tools
-test_files: []
-has_rdoc:
diff --git a/test/helper.rb b/test/helper.rb
index 9473c68..b56c5bb 100644
--- a/test/helper.rb
+++ b/test/helper.rb
@@ -1,5 +1,3 @@
-# encoding: utf-8
-
require 'coveralls'
Coveralls.wear!
@@ -20,16 +18,16 @@ module Cri
uncapture_io(*@orig_io)
end
- def capture_io_while(&block)
+ def capture_io_while
orig_io = capture_io
- block.call
+ yield
[$stdout.string, $stderr.string]
ensure
uncapture_io(*orig_io)
end
def lines(string)
- string.scan(/^.*\n/).map { |s| s.chomp }
+ string.scan(/^.*\n/).map(&:chomp)
end
private
diff --git a/test/test_argument_array.rb b/test/test_argument_array.rb
index df485d0..abd7562 100644
--- a/test/test_argument_array.rb
+++ b/test/test_argument_array.rb
@@ -1,4 +1,4 @@
-# encoding: utf-8
+require 'helper'
module Cri
class ArgumentArrayTestCase < Cri::TestCase
diff --git a/test/test_base.rb b/test/test_base.rb
index a57bec4..f6c6a7f 100644
--- a/test/test_base.rb
+++ b/test/test_base.rb
@@ -1,8 +1,7 @@
-# encoding: utf-8
+require 'helper'
module Cri
class BaseTestCase < Cri::TestCase
- def test_stub
- end
+ def test_stub; end
end
end
diff --git a/test/test_basic_help.rb b/test/test_basic_help.rb
index 067301e..5506d29 100644
--- a/test/test_basic_help.rb
+++ b/test/test_basic_help.rb
@@ -1,4 +1,4 @@
-# encoding: utf-8
+require 'helper'
module Cri
class BasicHelpTestCase < Cri::TestCase
diff --git a/test/test_basic_root.rb b/test/test_basic_root.rb
index 01cfe06..929bbf7 100644
--- a/test/test_basic_root.rb
+++ b/test/test_basic_root.rb
@@ -1,4 +1,4 @@
-# encoding: utf-8
+require 'helper'
module Cri
class BasicRootTestCase < Cri::TestCase
@@ -6,9 +6,20 @@ module Cri
cmd = Cri::Command.new_basic_root
stdout, _stderr = capture_io_while do
- assert_raises SystemExit do
- cmd.run(%w( -h ))
+ err = assert_raises SystemExit do
+ cmd.run(%w(-h))
end
+ assert_equal 0, err.status
+ end
+
+ assert stdout =~ /COMMANDS.*\n.*help.*show help/
+ end
+
+ def test_run_with_help_no_exit
+ cmd = Cri::Command.new_basic_root
+
+ stdout, _stderr = capture_io_while do
+ cmd.run(%w(-h), {}, hard_exit: false)
end
assert stdout =~ /COMMANDS.*\n.*help.*show help/
diff --git a/test/test_command.rb b/test/test_command.rb
index 3130ab8..9074ffd 100644
--- a/test/test_command.rb
+++ b/test/test_command.rb
@@ -1,4 +1,4 @@
-# encoding: utf-8
+require 'helper'
module Cri
class CommandTestCase < Cri::TestCase
@@ -9,7 +9,7 @@ module Cri
summary 'does stuff'
description 'This command does a lot of stuff.'
- option :a, :aaa, 'opt a', :argument => :optional do |value, cmd|
+ option :a, :aaa, 'opt a', argument: :optional do |value, cmd|
$stdout.puts "#{cmd.name}:#{value}"
end
required :b, :bbb, 'opt b'
@@ -31,7 +31,7 @@ module Cri
def bare_cmd
Cri::Command.define do
- name 'moo'
+ name 'moo'
run do |_opts, _args|
end
@@ -45,7 +45,7 @@ module Cri
summary 'does super stuff'
description 'This command does super stuff.'
- option :a, :aaa, 'opt a', :argument => :optional do |value, cmd|
+ option :a, :aaa, 'opt a', argument: :optional do |value, cmd|
$stdout.puts "#{cmd.name}:#{value}"
end
required :b, :bbb, 'opt b'
@@ -61,7 +61,7 @@ module Cri
summary 'does subby stuff'
description 'This command does subby stuff.'
- option :m, :mmm, 'opt m', :argument => :optional
+ option :m, :mmm, 'opt m', argument: :optional
required :n, :nnn, 'opt n'
optional :o, :ooo, 'opt o'
flag :p, :ppp, 'opt p'
@@ -147,9 +147,19 @@ module Cri
def test_invoke_simple_with_missing_opt_arg
out, err = capture_io_while do
- assert_raises SystemExit do
- simple_cmd.run(%w( -b ))
+ err = assert_raises SystemExit do
+ simple_cmd.run(%w(-b))
end
+ assert_equal 1, err.status
+ end
+
+ assert_equal [], lines(out)
+ assert_equal ['moo: option requires an argument -- b'], lines(err)
+ end
+
+ def test_invoke_simple_with_missing_opt_arg_no_exit
+ out, err = capture_io_while do
+ simple_cmd.run(%w(-b), {}, hard_exit: false)
end
assert_equal [], lines(out)
@@ -158,9 +168,19 @@ module Cri
def test_invoke_simple_with_illegal_opt
out, err = capture_io_while do
- assert_raises SystemExit do
- simple_cmd.run(%w( -z ))
+ err = assert_raises SystemExit do
+ simple_cmd.run(%w(-z))
end
+ assert_equal 1, err.status
+ end
+
+ assert_equal [], lines(out)
+ assert_equal ['moo: illegal option -- z'], lines(err)
+ end
+
+ def test_invoke_simple_with_illegal_opt_no_exit
+ out, err = capture_io_while do
+ simple_cmd.run(%w(-z), {}, hard_exit: false)
end
assert_equal [], lines(out)
@@ -169,7 +189,7 @@ module Cri
def test_invoke_simple_with_opt_with_block
out, err = capture_io_while do
- simple_cmd.run(%w( -a 123 ))
+ simple_cmd.run(%w(-a 123))
end
assert_equal ['moo:123', 'Awesome moo!', '', 'aaa=123'], lines(out)
@@ -178,9 +198,19 @@ module Cri
def test_invoke_nested_without_opts_or_args
out, err = capture_io_while do
- assert_raises SystemExit do
+ err = assert_raises SystemExit do
nested_cmd.run(%w())
end
+ assert_equal 1, err.status
+ end
+
+ assert_equal [], lines(out)
+ assert_equal ['super: no command given'], lines(err)
+ end
+
+ def test_invoke_nested_without_opts_or_args_no_exit
+ out, err = capture_io_while do
+ nested_cmd.run(%w(), {}, hard_exit: false)
end
assert_equal [], lines(out)
@@ -189,7 +219,7 @@ module Cri
def test_invoke_nested_with_correct_command_name
out, err = capture_io_while do
- nested_cmd.run(%w( sub ))
+ nested_cmd.run(%w(sub))
end
assert_equal ['Sub-awesome!', '', ''], lines(out)
@@ -198,9 +228,19 @@ module Cri
def test_invoke_nested_with_incorrect_command_name
out, err = capture_io_while do
- assert_raises SystemExit do
- nested_cmd.run(%w( oogabooga ))
+ err = assert_raises SystemExit do
+ nested_cmd.run(%w(oogabooga))
end
+ assert_equal 1, err.status
+ end
+
+ assert_equal [], lines(out)
+ assert_equal ["super: unknown command 'oogabooga'"], lines(err)
+ end
+
+ def test_invoke_nested_with_incorrect_command_name_no_exit
+ out, err = capture_io_while do
+ nested_cmd.run(%w(oogabooga), {}, hard_exit: false)
end
assert_equal [], lines(out)
@@ -209,9 +249,19 @@ module Cri
def test_invoke_nested_with_ambiguous_command_name
out, err = capture_io_while do
- assert_raises SystemExit do
- nested_cmd.run(%w( s ))
+ err = assert_raises SystemExit do
+ nested_cmd.run(%w(s))
end
+ assert_equal 1, err.status
+ end
+
+ assert_equal [], lines(out)
+ assert_equal ["super: 's' is ambiguous:", ' sink sub'], lines(err)
+ end
+
+ def test_invoke_nested_with_ambiguous_command_name_no_exit
+ out, err = capture_io_while do
+ nested_cmd.run(%w(s), {}, hard_exit: false)
end
assert_equal [], lines(out)
@@ -220,7 +270,7 @@ module Cri
def test_invoke_nested_with_alias
out, err = capture_io_while do
- nested_cmd.run(%w( sup ))
+ nested_cmd.run(%w(sup))
end
assert_equal ['Sub-awesome!', '', ''], lines(out)
@@ -229,7 +279,7 @@ module Cri
def test_invoke_nested_with_options_before_command
out, err = capture_io_while do
- nested_cmd.run(%w( -a 666 sub ))
+ nested_cmd.run(%w(-a 666 sub))
end
assert_equal ['super:666', 'Sub-awesome!', '', 'aaa=666'], lines(out)
@@ -245,7 +295,7 @@ module Cri
assert_equal [], lines(err)
out, err = capture_io_while do
- nested_cmd_with_run_block.run(%w( sub ))
+ nested_cmd_with_run_block.run(%w(sub))
end
assert_equal ['sub'], lines(out)
@@ -354,7 +404,7 @@ module Cri
def test_help_with_multiple_groups
help = nested_cmd.subcommands.find { |cmd| cmd.name == 'sub' }.help
- assert_match(/OPTIONS.*OPTIONS FOR SUPER/m, help)
+ assert_match(/OPTIONS.*OPTIONS FOR SUPER/m, help)
end
def test_modify_with_block_argument
@@ -377,7 +427,7 @@ module Cri
cmd = Cri::Command.define do
name 'build'
- flag nil, :longflag, 'This is an option with a very long description that should be wrapped'
+ flag nil, :longflag, 'This is an option with a very long description that should be wrapped'
end
help = cmd.help
@@ -469,9 +519,9 @@ module Cri
assert cmd.help.include?('hidden command omitted')
refute cmd.help.include?('old-and-deprecated')
- refute cmd.help(:verbose => true).include?('hidden commands omitted')
- refute cmd.help(:verbose => true).include?('hidden command omitted')
- assert cmd.help(:verbose => true).include?('old-and-deprecated')
+ refute cmd.help(verbose: true).include?('hidden commands omitted')
+ refute cmd.help(verbose: true).include?('hidden command omitted')
+ assert cmd.help(verbose: true).include?('old-and-deprecated')
end
def test_hidden_commands_multiple
@@ -505,13 +555,13 @@ module Cri
refute cmd.help.include?('old-and-deprecated')
refute cmd.help.include?('ancient-and-deprecated')
- refute cmd.help(:verbose => true).include?('hidden commands omitted')
- refute cmd.help(:verbose => true).include?('hidden command omitted')
- assert cmd.help(:verbose => true).include?('old-and-deprecated')
- assert cmd.help(:verbose => true).include?('ancient-and-deprecated')
+ refute cmd.help(verbose: true).include?('hidden commands omitted')
+ refute cmd.help(verbose: true).include?('hidden command omitted')
+ assert cmd.help(verbose: true).include?('old-and-deprecated')
+ assert cmd.help(verbose: true).include?('ancient-and-deprecated')
pattern = /ancient-and-deprecated.*first.*old-and-deprecated/m
- assert_match(pattern, cmd.help(:verbose => true))
+ assert_match(pattern, cmd.help(verbose: true))
end
def test_run_with_raw_args
@@ -523,7 +573,7 @@ module Cri
end
out, _err = capture_io_while do
- cmd.run(%w( foo -- bar ))
+ cmd.run(%w(foo -- bar))
end
assert_equal "args=foo,bar args.raw=foo,--,bar\n", out
end
@@ -549,7 +599,7 @@ module Cri
end
out, _err = capture_io_while do
- cmd.run(%w( foo -- bar ))
+ cmd.run(%w(foo -- bar))
end
assert_equal "args=foo,bar args.raw=foo,--,bar\n", out
end
@@ -561,5 +611,26 @@ module Cri
assert_equal [bar, foo, qux], [foo, bar, qux].sort
end
+
+ def test_default_subcommand
+ subcommand = Cri::Command.define do
+ name 'sub'
+
+ run do |_opts, _args, _c|
+ $stdout.puts 'I am the subcommand!'
+ end
+ end
+
+ cmd = Cri::Command.define do
+ name 'super'
+ default_subcommand 'sub'
+ subcommand subcommand
+ end
+
+ out, _err = capture_io_while do
+ cmd.run([])
+ end
+ assert_equal "I am the subcommand!\n", out
+ end
end
end
diff --git a/test/test_command_dsl.rb b/test/test_command_dsl.rb
index 27a0a29..03f188d 100644
--- a/test/test_command_dsl.rb
+++ b/test/test_command_dsl.rb
@@ -1,4 +1,4 @@
-# encoding: utf-8
+require 'helper'
module Cri
class CommandDSLTestCase < Cri::TestCase
@@ -11,12 +11,12 @@ module Cri
summary 'does stuff'
description 'This command does a lot of stuff.'
- option :a, :aaa, 'opt a', :argument => :optional, :multiple => true
+ option :a, :aaa, 'opt a', argument: :optional, multiple: true
required :b, :bbb, 'opt b'
optional :c, :ccc, 'opt c'
flag :d, :ddd, 'opt d'
forbidden :e, :eee, 'opt e'
- flag :f, :fff, 'opt f', :hidden => true
+ flag :f, :fff, 'opt f', hidden: true
run do |_opts, _args|
$did_it_work = :probably
@@ -26,7 +26,7 @@ module Cri
# Run
$did_it_work = :sadly_not
- command.run(%w( -a x -b y -c -d -e ))
+ command.run(%w(-a x -b y -c -d -e))
assert_equal :probably, $did_it_work
# Check
@@ -37,13 +37,13 @@ module Cri
# Check options
expected_option_definitions = Set.new([
- { :short => 'a', :long => 'aaa', :desc => 'opt a', :argument => :optional, :multiple => true, :hidden => false, :block => nil },
- { :short => 'b', :long => 'bbb', :desc => 'opt b', :argument => :required, :multiple => false, :hidden => false, :block => nil },
- { :short => 'c', :long => 'ccc', :desc => 'opt c', :argument => :optional, :multiple => false, :hidden => false, :block => nil },
- { :short => 'd', :long => 'ddd', :desc => 'opt d', :argument => :forbidden, :multiple => false, :hidden => false, :block => nil },
- { :short => 'e', :long => 'eee', :desc => 'opt e', :argument => :forbidden, :multiple => false, :hidden => false, :block => nil },
- { :short => 'f', :long => 'fff', :desc => 'opt f', :argument => :forbidden, :multiple => false, :hidden => true, :block => nil },
- ])
+ { short: 'a', long: 'aaa', desc: 'opt a', argument: :optional, multiple: true, hidden: false, block: nil },
+ { short: 'b', long: 'bbb', desc: 'opt b', argument: :required, multiple: false, hidden: false, block: nil },
+ { short: 'c', long: 'ccc', desc: 'opt c', argument: :optional, multiple: false, hidden: false, block: nil },
+ { short: 'd', long: 'ddd', desc: 'opt d', argument: :forbidden, multiple: false, hidden: false, block: nil },
+ { short: 'e', long: 'eee', desc: 'opt e', argument: :forbidden, multiple: false, hidden: false, block: nil },
+ { short: 'f', long: 'fff', desc: 'opt f', argument: :forbidden, multiple: false, hidden: true, block: nil },
+ ])
actual_option_definitions = Set.new(command.option_definitions)
assert_equal expected_option_definitions, actual_option_definitions
end
@@ -68,14 +68,14 @@ module Cri
# Run
$did_it_work = :sadly_not
- command.run(%w( -s --long ))
+ command.run(%w(-s --long))
assert_equal :probably, $did_it_work
# Check options
expected_option_definitions = Set.new([
- { :short => 's', :long => nil, :desc => 'short', :argument => :forbidden, :multiple => false, :hidden => false, :block => nil },
- { :short => nil, :long => 'long', :desc => 'long', :argument => :forbidden, :multiple => false, :hidden => false, :block => nil },
- ])
+ { short: 's', long: nil, desc: 'short', argument: :forbidden, multiple: false, hidden: false, block: nil },
+ { short: nil, long: 'long', desc: 'long', argument: :forbidden, multiple: false, hidden: false, block: nil },
+ ])
actual_option_definitions = Set.new(command.option_definitions)
assert_equal expected_option_definitions, actual_option_definitions
end
@@ -84,9 +84,9 @@ module Cri
# Define
dsl = Cri::CommandDSL.new
dsl.instance_eval do
- flag :f, :flag, 'flag', :multiple => true
- required :r, :required, 'req', :multiple => true
- optional :o, :optional, 'opt', :multiple => true
+ flag :f, :flag, 'flag', multiple: true
+ required :r, :required, 'req', multiple: true
+ optional :o, :optional, 'opt', multiple: true
run { |_opts, _args| }
end
@@ -94,10 +94,10 @@ module Cri
# Check options
expected_option_definitions = Set.new([
- { :short => 'f', :long => 'flag', :desc => 'flag', :argument => :forbidden, :multiple => true, :hidden => false, :block => nil },
- { :short => 'r', :long => 'required', :desc => 'req', :argument => :required, :multiple => true, :hidden => false, :block => nil },
- { :short => 'o', :long => 'optional', :desc => 'opt', :argument => :optional, :multiple => true, :hidden => false, :block => nil },
- ])
+ { short: 'f', long: 'flag', desc: 'flag', argument: :forbidden, multiple: true, hidden: false, block: nil },
+ { short: 'r', long: 'required', desc: 'req', argument: :required, multiple: true, hidden: false, block: nil },
+ { short: 'o', long: 'optional', desc: 'opt', argument: :optional, multiple: true, hidden: false, block: nil },
+ ])
actual_option_definitions = Set.new(command.option_definitions)
assert_equal expected_option_definitions, actual_option_definitions
end
@@ -106,9 +106,9 @@ module Cri
# Define
dsl = Cri::CommandDSL.new
dsl.instance_eval do
- flag :f, :flag, 'flag', :hidden => true
- required :r, :required, 'req', :hidden => true
- optional :o, :optional, 'opt', :hidden => true
+ flag :f, :flag, 'flag', hidden: true
+ required :r, :required, 'req', hidden: true
+ optional :o, :optional, 'opt', hidden: true
run { |_opts, _args| }
end
@@ -116,10 +116,10 @@ module Cri
# Check options
expected_option_definitions = Set.new([
- { :short => 'f', :long => 'flag', :desc => 'flag', :argument => :forbidden, :multiple => false, :hidden => true, :block => nil },
- { :short => 'r', :long => 'required', :desc => 'req', :argument => :required, :multiple => false, :hidden => true, :block => nil },
- { :short => 'o', :long => 'optional', :desc => 'opt', :argument => :optional, :multiple => false, :hidden => true, :block => nil },
- ])
+ { short: 'f', long: 'flag', desc: 'flag', argument: :forbidden, multiple: false, hidden: true, block: nil },
+ { short: 'r', long: 'required', desc: 'req', argument: :required, multiple: false, hidden: true, block: nil },
+ { short: 'o', long: 'optional', desc: 'opt', argument: :optional, multiple: false, hidden: true, block: nil },
+ ])
actual_option_definitions = Set.new(command.option_definitions)
assert_equal expected_option_definitions, actual_option_definitions
end
@@ -175,7 +175,7 @@ module Cri
command = dsl.command
# Check
- assert_equal %w( aah moo ), command.aliases.sort
+ assert_equal %w(aah moo), command.aliases.sort
end
def test_run_arity
@@ -204,7 +204,7 @@ module Cri
# Check
$did_it_work = false
- command.run(%w( certainly ))
+ command.run(%w(certainly))
assert_equal 'certainly', $did_it_work
end
end
diff --git a/test/test_command_runner.rb b/test/test_command_runner.rb
index 8b0c02b..1f43140 100644
--- a/test/test_command_runner.rb
+++ b/test/test_command_runner.rb
@@ -1,12 +1,12 @@
-# encoding: utf-8
+require 'helper'
module Cri
class CommandRunnerTestCase < Cri::TestCase
def setup
super
- @options = { :vehicle => 'pig' }
- @arguments = %w( baby_monkey )
+ @options = { vehicle: 'pig' }
+ @arguments = %w(baby_monkey)
@command = Cri::Command.new
end
diff --git a/test/test_option_parser.rb b/test/test_option_parser.rb
index 443d957..4994c91 100644
--- a/test/test_option_parser.rb
+++ b/test/test_option_parser.rb
@@ -1,19 +1,19 @@
-# encoding: utf-8
+require 'helper'
module Cri
class OptionParserTestCase < Cri::TestCase
def test_parse_without_options
- input = %w( foo bar baz )
+ input = %w(foo bar baz)
definitions = []
parser = Cri::OptionParser.parse(input, definitions)
- assert_equal({}, parser.options)
+ assert_equal({}, parser.options)
assert_equal(%w(foo bar baz), parser.arguments)
end
def test_parse_with_invalid_option
- input = %w( foo -x )
+ input = %w(foo -x)
definitions = []
assert_raises(Cri::OptionParser::IllegalOptionError) do
@@ -22,9 +22,9 @@ module Cri
end
def test_parse_with_unused_options
- input = %w( foo )
+ input = %w(foo)
definitions = [
- { :long => 'aaa', :short => 'a', :argument => :forbidden },
+ { long: 'aaa', short: 'a', argument: :forbidden },
]
parser = Cri::OptionParser.parse(input, definitions)
@@ -33,9 +33,9 @@ module Cri
end
def test_parse_with_long_valueless_option
- input = %w( foo --aaa bar )
+ input = %w(foo --aaa bar)
definitions = [
- { :long => 'aaa', :short => 'a', :argument => :forbidden },
+ { long: 'aaa', short: 'a', argument: :forbidden },
]
parser = Cri::OptionParser.parse(input, definitions)
@@ -45,33 +45,33 @@ module Cri
end
def test_parse_with_long_valueful_option
- input = %w( foo --aaa xxx bar )
+ input = %w(foo --aaa xxx bar)
definitions = [
- { :long => 'aaa', :short => 'a', :argument => :required },
+ { long: 'aaa', short: 'a', argument: :required },
]
parser = Cri::OptionParser.parse(input, definitions)
- assert_equal({ :aaa => 'xxx' }, parser.options)
+ assert_equal({ aaa: 'xxx' }, parser.options)
assert_equal(%w(foo bar), parser.arguments)
end
def test_parse_with_long_valueful_equalsign_option
- input = %w( foo --aaa=xxx bar )
+ input = %w(foo --aaa=xxx bar)
definitions = [
- { :long => 'aaa', :short => 'a', :argument => :required },
+ { long: 'aaa', short: 'a', argument: :required },
]
parser = Cri::OptionParser.parse(input, definitions)
- assert_equal({ :aaa => 'xxx' }, parser.options)
+ assert_equal({ aaa: 'xxx' }, parser.options)
assert_equal(%w(foo bar), parser.arguments)
end
def test_parse_with_long_valueful_option_with_missing_value
- input = %w( foo --aaa )
+ input = %w(foo --aaa)
definitions = [
- { :long => 'aaa', :short => 'a', :argument => :required },
+ { long: 'aaa', short: 'a', argument: :required },
]
assert_raises(Cri::OptionParser::OptionRequiresAnArgumentError) do
@@ -80,10 +80,10 @@ module Cri
end
def test_parse_with_two_long_valueful_options
- input = %w( foo --all --port 2 )
+ input = %w(foo --all --port 2)
definitions = [
- { :long => 'all', :short => 'a', :argument => :required },
- { :long => 'port', :short => 'p', :argument => :required },
+ { long: 'all', short: 'a', argument: :required },
+ { long: 'port', short: 'p', argument: :required },
]
assert_raises(Cri::OptionParser::OptionRequiresAnArgumentError) do
@@ -92,9 +92,9 @@ module Cri
end
def test_parse_with_long_valueless_option_with_optional_value
- input = %w( foo --aaa )
+ input = %w(foo --aaa)
definitions = [
- { :long => 'aaa', :short => 'a', :argument => :optional },
+ { long: 'aaa', short: 'a', argument: :optional },
]
parser = Cri::OptionParser.parse(input, definitions)
@@ -104,23 +104,23 @@ module Cri
end
def test_parse_with_long_valueful_option_with_optional_value
- input = %w( foo --aaa xxx )
+ input = %w(foo --aaa xxx)
definitions = [
- { :long => 'aaa', :short => 'a', :argument => :optional },
+ { long: 'aaa', short: 'a', argument: :optional },
]
parser = Cri::OptionParser.parse(input, definitions)
- assert_equal({ :aaa => 'xxx' }, parser.options)
+ assert_equal({ aaa: 'xxx' }, parser.options)
assert_equal(['foo'], parser.arguments)
end
def test_parse_with_long_valueless_option_with_optional_value_and_more_options
- input = %w( foo --aaa -b -c )
+ input = %w(foo --aaa -b -c)
definitions = [
- { :long => 'aaa', :short => 'a', :argument => :optional },
- { :long => 'bbb', :short => 'b', :argument => :forbidden },
- { :long => 'ccc', :short => 'c', :argument => :forbidden },
+ { long: 'aaa', short: 'a', argument: :optional },
+ { long: 'bbb', short: 'b', argument: :forbidden },
+ { long: 'ccc', short: 'c', argument: :forbidden },
]
parser = Cri::OptionParser.parse(input, definitions)
@@ -132,9 +132,9 @@ module Cri
end
def test_parse_with_short_valueless_options
- input = %w( foo -a bar )
+ input = %w(foo -a bar)
definitions = [
- { :long => 'aaa', :short => 'a', :argument => :forbidden },
+ { long: 'aaa', short: 'a', argument: :forbidden },
]
parser = Cri::OptionParser.parse(input, definitions)
@@ -144,9 +144,9 @@ module Cri
end
def test_parse_with_short_valueful_option_with_missing_value
- input = %w( foo -a )
+ input = %w(foo -a)
definitions = [
- { :long => 'aaa', :short => 'a', :argument => :required },
+ { long: 'aaa', short: 'a', argument: :required },
]
assert_raises(Cri::OptionParser::OptionRequiresAnArgumentError) do
@@ -155,11 +155,11 @@ module Cri
end
def test_parse_with_short_combined_valueless_options
- input = %w( foo -abc bar )
+ input = %w(foo -abc bar)
definitions = [
- { :long => 'aaa', :short => 'a', :argument => :forbidden },
- { :long => 'bbb', :short => 'b', :argument => :forbidden },
- { :long => 'ccc', :short => 'c', :argument => :forbidden },
+ { long: 'aaa', short: 'a', argument: :forbidden },
+ { long: 'bbb', short: 'b', argument: :forbidden },
+ { long: 'ccc', short: 'c', argument: :forbidden },
]
parser = Cri::OptionParser.parse(input, definitions)
@@ -171,11 +171,11 @@ module Cri
end
def test_parse_with_short_combined_valueful_options_with_missing_value
- input = %w( foo -abc bar )
+ input = %w(foo -abc bar)
definitions = [
- { :long => 'aaa', :short => 'a', :argument => :required },
- { :long => 'bbb', :short => 'b', :argument => :forbidden },
- { :long => 'ccc', :short => 'c', :argument => :forbidden },
+ { long: 'aaa', short: 'a', argument: :required },
+ { long: 'bbb', short: 'b', argument: :forbidden },
+ { long: 'ccc', short: 'c', argument: :forbidden },
]
assert_raises(Cri::OptionParser::OptionRequiresAnArgumentError) do
@@ -184,10 +184,10 @@ module Cri
end
def test_parse_with_two_short_valueful_options
- input = %w( foo -a -p 2 )
+ input = %w(foo -a -p 2)
definitions = [
- { :long => 'all', :short => 'a', :argument => :required },
- { :long => 'port', :short => 'p', :argument => :required },
+ { long: 'all', short: 'a', argument: :required },
+ { long: 'port', short: 'p', argument: :required },
]
assert_raises(Cri::OptionParser::OptionRequiresAnArgumentError) do
@@ -196,9 +196,9 @@ module Cri
end
def test_parse_with_short_valueless_option_with_optional_value
- input = %w( foo -a )
+ input = %w(foo -a)
definitions = [
- { :long => 'aaa', :short => 'a', :argument => :optional },
+ { long: 'aaa', short: 'a', argument: :optional },
]
parser = Cri::OptionParser.parse(input, definitions)
@@ -208,23 +208,23 @@ module Cri
end
def test_parse_with_short_valueful_option_with_optional_value
- input = %w( foo -a xxx )
+ input = %w(foo -a xxx)
definitions = [
- { :long => 'aaa', :short => 'a', :argument => :optional },
+ { long: 'aaa', short: 'a', argument: :optional },
]
parser = Cri::OptionParser.parse(input, definitions)
- assert_equal({ :aaa => 'xxx' }, parser.options)
+ assert_equal({ aaa: 'xxx' }, parser.options)
assert_equal(['foo'], parser.arguments)
end
def test_parse_with_short_valueless_option_with_optional_value_and_more_options
- input = %w( foo -a -b -c )
+ input = %w(foo -a -b -c)
definitions = [
- { :long => 'aaa', :short => 'a', :argument => :optional },
- { :long => 'bbb', :short => 'b', :argument => :forbidden },
- { :long => 'ccc', :short => 'c', :argument => :forbidden },
+ { long: 'aaa', short: 'a', argument: :optional },
+ { long: 'bbb', short: 'b', argument: :forbidden },
+ { long: 'ccc', short: 'c', argument: :forbidden },
]
parser = Cri::OptionParser.parse(input, definitions)
@@ -236,30 +236,30 @@ module Cri
end
def test_parse_with_single_hyphen
- input = %w( foo - bar )
+ input = %w(foo - bar)
definitions = []
parser = Cri::OptionParser.parse(input, definitions)
- assert_equal({}, parser.options)
+ assert_equal({}, parser.options)
assert_equal(['foo', '-', 'bar'], parser.arguments)
end
def test_parse_with_end_marker
- input = %w( foo bar -- -x --yyy -abc )
+ input = %w(foo bar -- -x --yyy -abc)
definitions = []
parser = Cri::OptionParser.parse(input, definitions)
- assert_equal({}, parser.options)
+ assert_equal({}, parser.options)
assert_equal(['foo', 'bar', '-x', '--yyy', '-abc'], parser.arguments)
assert_equal(['foo', 'bar', '--', '-x', '--yyy', '-abc'], parser.arguments.raw)
end
def test_parse_with_end_marker_between_option_key_and_value
- input = %w( foo --aaa -- zzz )
+ input = %w(foo --aaa -- zzz)
definitions = [
- { :long => 'aaa', :short => 'a', :argument => :required },
+ { long: 'aaa', short: 'a', argument: :required },
]
assert_raises(Cri::OptionParser::OptionRequiresAnArgumentError) do
@@ -268,10 +268,10 @@ module Cri
end
def test_parse_with_multiple_options
- input = %w( foo -o test -o test2 -v -v -v)
+ input = %w(foo -o test -o test2 -v -v -v)
definitions = [
- { :long => 'long', :short => 'o', :argument => :required, :multiple => true },
- { :long => 'verbose', :short => 'v', :multiple => true },
+ { long: 'long', short: 'o', argument: :required, multiple: true },
+ { long: 'verbose', short: 'v', multiple: true },
]
parser = Cri::OptionParser.parse(input, definitions)
diff --git a/test/test_string_formatter.rb b/test/test_string_formatter.rb
index dd659b1..c30853d 100644
--- a/test/test_string_formatter.rb
+++ b/test/test_string_formatter.rb
@@ -1,4 +1,4 @@
-# encoding: utf-8
+require 'helper'
module Cri
class CoreExtTestCase < Cri::TestCase
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-ruby-extras/ruby-cri.git
More information about the Pkg-ruby-extras-commits
mailing list