Hello community, here is the log from the commit of package rubygem-cri for openSUSE:Factory checked in at 2017-06-15 11:24:54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Comparing /work/SRC/openSUSE:Factory/rubygem-cri (Old) and /work/SRC/openSUSE:Factory/.rubygem-cri.new (New) ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Package is "rubygem-cri" Thu Jun 15 11:24:54 2017 rev:5 rq:503592 version:2.9.1 Changes: -------- --- /work/SRC/openSUSE:Factory/rubygem-cri/rubygem-cri.changes 2017-04-17 10:27:08.859107349 +0200 +++ /work/SRC/openSUSE:Factory/.rubygem-cri.new/rubygem-cri.changes 2017-06-15 11:25:34.904863388 +0200 @@ -1,0 +2,6 @@ +Tue Jun 6 05:32:55 UTC 2017 - [email protected] + +- updated to version 2.9.1 + see installed NEWS.md + +------------------------------------------------------------------- Old: ---- cri-2.8.0.gem New: ---- cri-2.9.1.gem ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Other differences: ------------------ ++++++ rubygem-cri.spec ++++++ --- /var/tmp/diff_new_pack.jRmHoi/_old 2017-06-15 11:25:35.428789429 +0200 +++ /var/tmp/diff_new_pack.jRmHoi/_new 2017-06-15 11:25:35.432788864 +0200 @@ -24,7 +24,7 @@ # Name: rubygem-cri -Version: 2.8.0 +Version: 2.9.1 Release: 0 %define mod_name cri %define mod_full_name %{mod_name}-%{version} ++++++ cri-2.8.0.gem -> cri-2.9.1.gem ++++++ diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/Gemfile.lock new/Gemfile.lock --- old/Gemfile.lock 2017-04-01 13:56:35.000000000 +0200 +++ new/Gemfile.lock 2017-06-03 12:45:27.000000000 +0200 @@ -1,7 +1,7 @@ PATH remote: . specs: - cri (2.7.1) + cri (2.9.1) colored (~> 1.2) GEM @@ -10,21 +10,24 @@ asciidoctor (1.5.5) ast (2.3.0) colored (1.2) - coveralls (0.8.20) + coveralls (0.8.21) json (>= 1.8, < 3) simplecov (~> 0.14.1) term-ansicolor (~> 1.3) thor (~> 0.19.4) tins (~> 1.6) docile (1.1.5) - json (2.0.3) - minitest (5.10.1) + json (2.1.0) + minitest (5.10.2) + parallel (1.11.2) parser (2.4.0.0) ast (~> 2.2) powerpack (0.1.1) - rainbow (2.2.1) + rainbow (2.2.2) + rake rake (12.0.0) - rubocop (0.48.0) + rubocop (0.49.1) + parallel (~> 1.10) parser (>= 2.3.3.1, < 3.0) powerpack (~> 0.1) rainbow (>= 1.99.1, < 3.0) @@ -35,13 +38,13 @@ docile (~> 1.1.0) json (>= 1.8, < 3) simplecov-html (~> 0.10.0) - simplecov-html (0.10.0) - term-ansicolor (1.5.0) + simplecov-html (0.10.1) + term-ansicolor (1.6.0) tins (~> 1.0) thor (0.19.4) - tins (1.13.2) - unicode-display_width (1.1.3) - yard (0.9.8) + tins (1.14.0) + unicode-display_width (1.2.1) + yard (0.9.9) PLATFORMS ruby @@ -57,4 +60,4 @@ yard BUNDLED WITH - 1.14.6 + 1.15.1 diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/NEWS.md new/NEWS.md --- old/NEWS.md 2017-04-01 13:56:35.000000000 +0200 +++ new/NEWS.md 2017-06-03 12:45:27.000000000 +0200 @@ -1,23 +1,36 @@ Cri News ======== -2.8.0 ------ +## 2.9.1 + +Fixes: + +* Made default values be always returned, even when not explicitly specified (#57, #58) + +## 2.9.0 + +Features: + +* Allowed specifying default option value (#55) + +Enhancements: + +* Added support for specifying values for combined options (#56) + +## 2.8.0 Features: -* Allow passing `hard_exit: false` to `Command#run` to prevent `SystemExit` (#51) -* Allow specifying the default subcommand (#54) +* Allowed passing `hard_exit: false` to `Command#run` to prevent `SystemExit` (#51) +* Allowed specifying the default subcommand (#54) -2.7.1 ------ +## 2.7.1 Fixes: * Fixed some grammatical mistakes -2.7.0 ------ +## 2.7.0 Features: @@ -28,80 +41,66 @@ * Added option values to help output (#37, #40, #41) * Made option descriptions wrap (#36, #45) [Bart Mesuere] -2.6.1 ------ +## 2.6.1 * Disable ANSI color codes when not supported (#31, #32) -2.6.0 ------ +## 2.6.0 * Added support for multi-valued options (#29) [Toon Willems] -2.5.0 ------ +## 2.5.0 * Made the default help command handle subcommands (#27) * Added `#raw` method to argument arrays, returning all arguments including `--` (#22) -2.4.1 ------ +## 2.4.1 * Fixed ordering of option groups on Ruby 1.8.x (#14, #15) * Fixed ordering of commands when --verbose is passed (#16, #18) -2.4.0 ------ +## 2.4.0 * Allowed either short or long option to be, eh, optional (#9, #10) [Ken Coar] * Fixed wrap-and-indent behavior (#12) [Ken Coar] * Moved version information into `cri/version` -2.3.0 ------ +## 2.3.0 * Added colors (#1) * Added support for marking commands as hidden -2.2.1 ------ +## 2.2.1 * Made command help sort subcommands -2.2.0 ------ +## 2.2.0 * Allowed commands with subcommands to have a run block -2.1.0 ------ +## 2.1.0 * Added support for runners * Split up local/global command options -2.0.2 ------ +## 2.0.2 * Added command filename to stack traces -2.0.1 ------ +## 2.0.1 * Sorted ambiguous command names * Restored compatibility with Ruby 1.8.x -2.0.0 ------ +## 2.0.0 * Added DSL * Added support for nested commands -1.0.1 ------ +## 1.0.1 * Made gem actually include code. D'oh. -1.0.0 ------ +## 1.0.0 * Initial release! diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/README.adoc new/README.adoc --- old/README.adoc 2017-04-01 13:56:35.000000000 +0200 +++ new/README.adoc 2017-06-03 12:45:27.000000000 +0200 @@ -116,18 +116,41 @@ * `Cri::CommandDSL#required` (implies required argument) * `Cri::CommandDSL#optional` (implies optional argument) -All these methods take the short option form as their first argument, and a -long option form as their second argument. Either the short or the long form -can be nil, but not both (because that would not make any sense). In the -example above, the `--more` option has no short form. +All these methods take these arguments: + +1. a short option +2. a long option +3. a description +4. optional extra parameters + +Either the short or the long form can be nil, but not both (because that +would not make any sense). In the example above, the `--more` option has no +short form. Each of the above methods also take a block, which will be executed when the option is found. The argument to the block are the option value (`true` in case the option does not have an argument) and the command. +==== Options with default values ==== + +The `:default` parameter sets the option value that will be used if no explicit value is provided: + +[source,ruby] +-------------------------------------------------------------------------------- +optional :a, :animal, 'add animal', default: 'giraffe' +-------------------------------------------------------------------------------- + +In the example above, the value for the `--animal` option will be the string +`"giraffe"`, unless otherwise specified: + +-------------------------------------------------------------------------------- +OPTIONS + -a --animal[=<value>] add animal (default: giraffe) +-------------------------------------------------------------------------------- + ==== Multivalued options ==== -Each of these four methods take a `:multiple` option. When set to true, multiple +Each of these four methods take a `:multiple` parameter. When set to true, multiple option valus are accepted, and the option values will be stored in an array. For example, to parse the command line options string `-o foo.txt -o bar.txt` diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/Rakefile new/Rakefile --- old/Rakefile 2017-04-01 13:56:35.000000000 +0200 +++ new/Rakefile 2017-06-03 12:45:27.000000000 +0200 @@ -20,9 +20,9 @@ end RuboCop::RakeTask.new(:test_style) do |task| - task.options = %w(--display-cop-names --format simple) + task.options = %w[--display-cop-names --format simple] end -task test: %i(test_unit test_style) +task test: %i[test_unit test_style] task default: :test Binary files old/checksums.yaml.gz and new/checksums.yaml.gz differ diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/lib/cri/argument_array.rb new/lib/cri/argument_array.rb --- old/lib/cri/argument_array.rb 2017-04-01 13:56:35.000000000 +0200 +++ new/lib/cri/argument_array.rb 2017-06-03 12:45:27.000000000 +0200 @@ -8,7 +8,7 @@ # @param [Array<String>] raw_arguments A list of raw arguments, i.e. # including any separator arguments (`--`). def initialize(raw_arguments) - super(raw_arguments.reject { |a| '--' == a }) + super(raw_arguments.reject { |a| a == '--' }) @raw_arguments = raw_arguments end diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/lib/cri/command_dsl.rb new/lib/cri/command_dsl.rb --- old/lib/cri/command_dsl.rb 2017-04-01 13:56:35.000000000 +0200 +++ new/lib/cri/command_dsl.rb 2017-06-03 12:45:27.000000000 +0200 @@ -121,11 +121,16 @@ requiredness = params.fetch(:argument, :forbidden) multiple = params.fetch(:multiple, false) hidden = params.fetch(:hidden, false) + default = params.fetch(:default, nil) if short.nil? && long.nil? raise ArgumentError, 'short and long options cannot both be nil' end + if default && requiredness == :forbidden + raise ArgumentError, 'a default value cannot be specified for flag options' + end + @command.option_definitions << { short: short.nil? ? nil : short.to_s, long: long.nil? ? nil : long.to_s, @@ -134,6 +139,7 @@ multiple: multiple, block: block, hidden: hidden, + default: default, } end alias opt option diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/lib/cri/help_renderer.rb new/lib/cri/help_renderer.rb --- old/lib/cri/help_renderer.rb 2017-04-01 13:56:35.000000000 +0200 +++ new/lib/cri/help_renderer.rb 2017-06-03 12:45:27.000000000 +0200 @@ -146,11 +146,10 @@ text << "\n" ordered_defs = defs.sort_by { |x| x[:short] || x[:long] } - ordered_defs.each do |opt_def| - unless opt_def[:hidden] - text << format_opt_def(opt_def, length) - text << fmt.wrap_and_indent(opt_def[:desc], LINE_WIDTH, length + OPT_DESC_SPACING + DESC_INDENT, true) << "\n" - end + ordered_defs.reject { |opt_def| opt_def[:hidden] }.each do |opt_def| + text << format_opt_def(opt_def, length) + desc = opt_def[:desc] + (opt_def[:default] ? " (default: #{opt_def[:default]})" : '') + text << fmt.wrap_and_indent(desc, LINE_WIDTH, length + OPT_DESC_SPACING + DESC_INDENT, true) << "\n" end end diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/lib/cri/option_parser.rb new/lib/cri/option_parser.rb --- old/lib/cri/option_parser.rb 2017-04-01 13:56:35.000000000 +0200 +++ new/lib/cri/option_parser.rb 2017-06-03 12:45:27.000000000 +0200 @@ -173,6 +173,9 @@ add_argument(e) end end + + add_defaults + self ensure @running = false @@ -180,6 +183,10 @@ private + def add_defaults + @definitions.each { |d| add_default_option(d) } + end + def handle_dashdash(e) add_argument(e) @no_more_options = true @@ -199,7 +206,7 @@ definition = @definitions.find { |d| d[:long] == option_key } raise IllegalOptionError.new(option_key) if definition.nil? - if %i(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,10 +230,7 @@ definition = @definitions.find { |d| d[:short] == option_key } 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 - raise OptionRequiresAnArgumentError.new(option_key) - elsif %i(required optional).include?(definition[:argument]) + if %i[required optional].include?(definition[:argument]) # Get option value option_value = find_option_value(definition, option_key) @@ -242,7 +246,9 @@ def find_option_value(definition, option_key) option_value = @unprocessed_arguments_and_options.shift if option_value.nil? || option_value =~ /^-/ - if definition[:argument] == :required + if definition[:argument] == :optional && definition[:default] + option_value = definition[:default] + elsif definition[:argument] == :required raise OptionRequiresAnArgumentError.new(option_key) else @unprocessed_arguments_and_options.unshift(option_value) @@ -253,7 +259,8 @@ end def add_option(definition, value) - key = (definition[:long] || definition[:short]).to_sym + key = key_for(definition) + if definition[:multiple] options[key] ||= [] options[key] << value @@ -264,10 +271,29 @@ delegate.option_added(key, value, self) unless delegate.nil? end + def add_default_option(definition) + key = key_for(definition) + return if options.key?(key) + + value = definition[:default] + return unless value + + if definition[:multiple] + options[key] ||= [] + options[key] << value + else + options[key] = value + end + end + + def key_for(definition) + (definition[:long] || definition[:short]).to_sym + end + def add_argument(value) @raw_arguments << value - unless '--' == value + unless value == '--' delegate.argument_added(value, self) unless delegate.nil? end end diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/lib/cri/version.rb new/lib/cri/version.rb --- old/lib/cri/version.rb 2017-04-01 13:56:35.000000000 +0200 +++ new/lib/cri/version.rb 2017-06-03 12:45:27.000000000 +0200 @@ -1,4 +1,4 @@ module Cri # The current Cri version. - VERSION = '2.8.0'.freeze + VERSION = '2.9.1'.freeze end diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/metadata new/metadata --- old/metadata 2017-04-01 13:56:35.000000000 +0200 +++ new/metadata 2017-06-03 12:45:27.000000000 +0200 @@ -1,14 +1,14 @@ --- !ruby/object:Gem::Specification name: cri version: !ruby/object:Gem::Version - version: 2.8.0 + version: 2.9.1 platform: ruby authors: - Denis Defreyne autorequire: bindir: bin cert_chain: [] -date: 2017-04-01 00:00:00.000000000 Z +date: 2017-06-03 00:00:00.000000000 Z dependencies: - !ruby/object:Gem::Dependency name: colored @@ -77,6 +77,7 @@ - test/test_command.rb - test/test_command_dsl.rb - test/test_command_runner.rb +- test/test_help_renderer.rb - test/test_option_parser.rb - test/test_string_formatter.rb homepage: http://stoneship.org/software/cri/ @@ -101,7 +102,7 @@ version: '0' requirements: [] rubyforge_project: -rubygems_version: 2.6.11 +rubygems_version: 2.6.12 signing_key: specification_version: 4 summary: a library for building easy-to-use command-line tools diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/test/test_argument_array.rb new/test/test_argument_array.rb --- old/test/test_argument_array.rb 2017-04-01 13:56:35.000000000 +0200 +++ new/test/test_argument_array.rb 2017-06-03 12:45:27.000000000 +0200 @@ -4,7 +4,7 @@ class ArgumentArrayTestCase < Cri::TestCase def test_initialize arr = Cri::ArgumentArray.new(['foo', 'bar', '--', 'baz']) - assert_equal %w(foo bar baz), arr + assert_equal %w[foo bar baz], arr assert_equal ['foo', 'bar', '--', 'baz'], arr.raw end end diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/test/test_basic_help.rb new/test/test_basic_help.rb --- old/test/test_basic_help.rb 2017-04-01 13:56:35.000000000 +0200 +++ new/test/test_basic_help.rb 2017-06-03 12:45:27.000000000 +0200 @@ -49,7 +49,7 @@ # Subcommand stdout, stderr = capture_io_while do - help_cmd.run(%w(foo subsubby)) + help_cmd.run(%w[foo subsubby]) end assert_match(/I am subsubby!/m, stdout) assert_equal('', stderr) @@ -57,7 +57,7 @@ # Non-existing subcommand stdout, stderr = capture_io_while do assert_raises SystemExit do - help_cmd.run(%w(foo mysterycmd)) + help_cmd.run(%w[foo mysterycmd]) end end assert_equal '', stdout diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/test/test_basic_root.rb new/test/test_basic_root.rb --- old/test/test_basic_root.rb 2017-04-01 13:56:35.000000000 +0200 +++ new/test/test_basic_root.rb 2017-06-03 12:45:27.000000000 +0200 @@ -7,7 +7,7 @@ stdout, _stderr = capture_io_while do err = assert_raises SystemExit do - cmd.run(%w(-h)) + cmd.run(%w[-h]) end assert_equal 0, err.status end @@ -19,7 +19,7 @@ cmd = Cri::Command.new_basic_root stdout, _stderr = capture_io_while do - cmd.run(%w(-h), {}, hard_exit: false) + cmd.run(%w[-h], {}, hard_exit: false) end assert stdout =~ /COMMANDS.*\n.*help.*show help/ diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/test/test_command.rb new/test/test_command.rb --- old/test/test_command.rb 2017-04-01 13:56:35.000000000 +0200 +++ new/test/test_command.rb 2017-06-03 12:45:27.000000000 +0200 @@ -120,7 +120,7 @@ def test_invoke_simple_without_opts_or_args out, err = capture_io_while do - simple_cmd.run(%w()) + simple_cmd.run(%w[]) end assert_equal ['Awesome moo!', '', ''], lines(out) @@ -129,7 +129,7 @@ def test_invoke_simple_with_args out, err = capture_io_while do - simple_cmd.run(%w(abc xyz)) + simple_cmd.run(%w[abc xyz]) end assert_equal ['Awesome moo!', 'abc,xyz', ''], lines(out) @@ -138,7 +138,7 @@ def test_invoke_simple_with_opts out, err = capture_io_while do - simple_cmd.run(%w(-c -b x)) + simple_cmd.run(%w[-c -b x]) end assert_equal ['Awesome moo!', '', 'bbb=x,ccc=true'], lines(out) @@ -148,7 +148,7 @@ def test_invoke_simple_with_missing_opt_arg out, err = capture_io_while do err = assert_raises SystemExit do - simple_cmd.run(%w(-b)) + simple_cmd.run(%w[-b]) end assert_equal 1, err.status end @@ -159,7 +159,7 @@ def test_invoke_simple_with_missing_opt_arg_no_exit out, err = capture_io_while do - simple_cmd.run(%w(-b), {}, hard_exit: false) + simple_cmd.run(%w[-b], {}, hard_exit: false) end assert_equal [], lines(out) @@ -169,7 +169,7 @@ def test_invoke_simple_with_illegal_opt out, err = capture_io_while do err = assert_raises SystemExit do - simple_cmd.run(%w(-z)) + simple_cmd.run(%w[-z]) end assert_equal 1, err.status end @@ -180,7 +180,7 @@ def test_invoke_simple_with_illegal_opt_no_exit out, err = capture_io_while do - simple_cmd.run(%w(-z), {}, hard_exit: false) + simple_cmd.run(%w[-z], {}, hard_exit: false) end assert_equal [], lines(out) @@ -189,7 +189,7 @@ 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) @@ -199,7 +199,7 @@ def test_invoke_nested_without_opts_or_args out, err = capture_io_while do err = assert_raises SystemExit do - nested_cmd.run(%w()) + nested_cmd.run(%w[]) end assert_equal 1, err.status end @@ -210,7 +210,7 @@ def test_invoke_nested_without_opts_or_args_no_exit out, err = capture_io_while do - nested_cmd.run(%w(), {}, hard_exit: false) + nested_cmd.run(%w[], {}, hard_exit: false) end assert_equal [], lines(out) @@ -219,7 +219,7 @@ 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) @@ -229,7 +229,7 @@ def test_invoke_nested_with_incorrect_command_name out, err = capture_io_while do err = assert_raises SystemExit do - nested_cmd.run(%w(oogabooga)) + nested_cmd.run(%w[oogabooga]) end assert_equal 1, err.status end @@ -240,7 +240,7 @@ def test_invoke_nested_with_incorrect_command_name_no_exit out, err = capture_io_while do - nested_cmd.run(%w(oogabooga), {}, hard_exit: false) + nested_cmd.run(%w[oogabooga], {}, hard_exit: false) end assert_equal [], lines(out) @@ -250,7 +250,7 @@ def test_invoke_nested_with_ambiguous_command_name out, err = capture_io_while do err = assert_raises SystemExit do - nested_cmd.run(%w(s)) + nested_cmd.run(%w[s]) end assert_equal 1, err.status end @@ -261,7 +261,7 @@ def test_invoke_nested_with_ambiguous_command_name_no_exit out, err = capture_io_while do - nested_cmd.run(%w(s), {}, hard_exit: false) + nested_cmd.run(%w[s], {}, hard_exit: false) end assert_equal [], lines(out) @@ -270,7 +270,7 @@ 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) @@ -279,7 +279,7 @@ 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) @@ -288,14 +288,14 @@ def test_invoke_nested_with_run_block out, err = capture_io_while do - nested_cmd_with_run_block.run(%w()) + nested_cmd_with_run_block.run(%w[]) end assert_equal ['super'], lines(out) 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) @@ -573,7 +573,7 @@ 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 @@ -599,7 +599,7 @@ 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 diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/test/test_command_dsl.rb new/test/test_command_dsl.rb --- old/test/test_command_dsl.rb 2017-04-01 13:56:35.000000000 +0200 +++ new/test/test_command_dsl.rb 2017-06-03 12:45:27.000000000 +0200 @@ -26,7 +26,7 @@ # 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 @@ -36,14 +36,17 @@ assert_equal 'This command does a lot of stuff.', command.description # 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 }, - ]) + expected_option_definitions = + Set.new( + [ + { short: 'a', long: 'aaa', desc: 'opt a', argument: :optional, multiple: true, hidden: false, block: nil, default: nil }, + { short: 'b', long: 'bbb', desc: 'opt b', argument: :required, multiple: false, hidden: false, block: nil, default: nil }, + { short: 'c', long: 'ccc', desc: 'opt c', argument: :optional, multiple: false, hidden: false, block: nil, default: nil }, + { short: 'd', long: 'ddd', desc: 'opt d', argument: :forbidden, multiple: false, hidden: false, block: nil, default: nil }, + { short: 'e', long: 'eee', desc: 'opt e', argument: :forbidden, multiple: false, hidden: false, block: nil, default: nil }, + { short: 'f', long: 'fff', desc: 'opt f', argument: :forbidden, multiple: false, hidden: true, block: nil, default: nil }, + ], + ) actual_option_definitions = Set.new(command.option_definitions) assert_equal expected_option_definitions, actual_option_definitions end @@ -68,14 +71,17 @@ # 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 }, - ]) + expected_option_definitions = + Set.new( + [ + { short: 's', long: nil, desc: 'short', argument: :forbidden, multiple: false, hidden: false, block: nil, default: nil }, + { short: nil, long: 'long', desc: 'long', argument: :forbidden, multiple: false, hidden: false, block: nil, default: nil }, + ], + ) actual_option_definitions = Set.new(command.option_definitions) assert_equal expected_option_definitions, actual_option_definitions end @@ -93,11 +99,14 @@ command = dsl.command # 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 }, - ]) + expected_option_definitions = + Set.new( + [ + { short: 'f', long: 'flag', desc: 'flag', argument: :forbidden, multiple: true, hidden: false, block: nil, default: nil }, + { short: 'r', long: 'required', desc: 'req', argument: :required, multiple: true, hidden: false, block: nil, default: nil }, + { short: 'o', long: 'optional', desc: 'opt', argument: :optional, multiple: true, hidden: false, block: nil, default: nil }, + ], + ) actual_option_definitions = Set.new(command.option_definitions) assert_equal expected_option_definitions, actual_option_definitions end @@ -115,11 +124,14 @@ command = dsl.command # 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 }, - ]) + expected_option_definitions = + Set.new( + [ + { short: 'f', long: 'flag', desc: 'flag', argument: :forbidden, multiple: false, hidden: true, block: nil, default: nil }, + { short: 'r', long: 'required', desc: 'req', argument: :required, multiple: false, hidden: true, block: nil, default: nil }, + { short: 'o', long: 'optional', desc: 'opt', argument: :optional, multiple: false, hidden: true, block: nil, default: nil }, + ], + ) actual_option_definitions = Set.new(command.option_definitions) assert_equal expected_option_definitions, actual_option_definitions end @@ -149,6 +161,25 @@ end end + def test_default_value_with_equiredness_is_required + dsl = Cri::CommandDSL.new + + dsl.instance_eval do + required 'a', 'animal', 'Specify animal', default: 'giraffe' + end + end + + def test_default_value_errors_when_requiredness_is_forbidden + dsl = Cri::CommandDSL.new + + err = assert_raises ArgumentError do + dsl.instance_eval do + flag 'a', 'animal', 'Allow animal', default: 'giraffe' + end + end + assert_equal('a default value cannot be specified for flag options', err.message) + end + def test_subcommand # Define dsl = Cri::CommandDSL.new @@ -175,7 +206,7 @@ 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 +235,7 @@ # Check $did_it_work = false - command.run(%w(certainly)) + command.run(%w[certainly]) assert_equal 'certainly', $did_it_work end end diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/test/test_command_runner.rb new/test/test_command_runner.rb --- old/test/test_command_runner.rb 2017-04-01 13:56:35.000000000 +0200 +++ new/test/test_command_runner.rb 2017-06-03 12:45:27.000000000 +0200 @@ -6,7 +6,7 @@ super @options = { vehicle: 'pig' } - @arguments = %w(baby_monkey) + @arguments = %w[baby_monkey] @command = Cri::Command.new end diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/test/test_help_renderer.rb new/test/test_help_renderer.rb --- old/test/test_help_renderer.rb 1970-01-01 01:00:00.000000000 +0100 +++ new/test/test_help_renderer.rb 2017-06-03 12:45:27.000000000 +0200 @@ -0,0 +1,45 @@ +require 'helper' + +module Cri + class HelpRendererTestCase < Cri::TestCase + # NOTE: Additional test cases are in test_command.rb + + def help_for(cmd) + io = StringIO.new + Cri::HelpRenderer.new(cmd, io: io).render + end + + def test_simple + expected = <<EOS +NAME + help - show help + +USAGE + help [command_name] + +DESCRIPTION + 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 command-line options. When a command is given, a command + description, as well as command-specific command-line options, are shown. + +OPTIONS + -v --verbose show more detailed help +EOS + + cmd = Cri::Command.new_basic_help + assert_equal(expected, help_for(cmd)) + end + + def test_with_defaults + cmd = Cri::Command.define do + name 'build' + optional nil, :'with-animal', 'Add animal', default: 'giraffe' + end + + help = help_for(cmd) + + assert_match(/^ --with-animal\[=<value>\] Add animal \(default: giraffe\)$/, help) + end + end +end diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/test/test_option_parser.rb new/test/test_option_parser.rb --- old/test/test_option_parser.rb 2017-04-01 13:56:35.000000000 +0200 +++ new/test/test_option_parser.rb 2017-06-03 12:45:27.000000000 +0200 @@ -3,17 +3,17 @@ 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(%w(foo bar baz), parser.arguments) + 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,7 +22,7 @@ end def test_parse_with_unused_options - input = %w(foo) + input = %w[foo] definitions = [ { long: 'aaa', short: 'a', argument: :forbidden }, ] @@ -33,7 +33,7 @@ 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 }, ] @@ -41,11 +41,11 @@ parser = Cri::OptionParser.parse(input, definitions) assert(parser.options[:aaa]) - assert_equal(%w(foo bar), parser.arguments) + assert_equal(%w[foo bar], parser.arguments) 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 }, ] @@ -53,11 +53,11 @@ parser = Cri::OptionParser.parse(input, definitions) assert_equal({ aaa: 'xxx' }, parser.options) - assert_equal(%w(foo bar), parser.arguments) + 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 }, ] @@ -65,11 +65,11 @@ parser = Cri::OptionParser.parse(input, definitions) assert_equal({ aaa: 'xxx' }, parser.options) - assert_equal(%w(foo bar), parser.arguments) + 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 }, ] @@ -80,7 +80,7 @@ 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 }, @@ -92,7 +92,7 @@ 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 }, ] @@ -104,7 +104,7 @@ 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 }, ] @@ -116,7 +116,7 @@ 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 }, @@ -132,7 +132,7 @@ 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 }, ] @@ -140,11 +140,11 @@ parser = Cri::OptionParser.parse(input, definitions) assert(parser.options[:aaa]) - assert_equal(%w(foo bar), parser.arguments) + assert_equal(%w[foo bar], parser.arguments) 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 }, ] @@ -155,7 +155,7 @@ 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 }, @@ -167,24 +167,27 @@ assert(parser.options[:aaa]) assert(parser.options[:bbb]) assert(parser.options[:ccc]) - assert_equal(%w(foo bar), parser.arguments) + assert_equal(%w[foo bar], parser.arguments) end def test_parse_with_short_combined_valueful_options_with_missing_value - input = %w(foo -abc bar) + input = %w[foo -abc bar qux] definitions = [ { long: 'aaa', short: 'a', argument: :required }, { long: 'bbb', short: 'b', argument: :forbidden }, { long: 'ccc', short: 'c', argument: :forbidden }, ] - assert_raises(Cri::OptionParser::OptionRequiresAnArgumentError) do - Cri::OptionParser.parse(input, definitions) - end + parser = Cri::OptionParser.parse(input, definitions) + + assert_equal('bar', parser.options[:aaa]) + assert(parser.options[:bbb]) + assert(parser.options[:ccc]) + assert_equal(%w[foo qux], parser.arguments) 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 }, @@ -196,7 +199,7 @@ 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 }, ] @@ -208,7 +211,7 @@ 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 }, ] @@ -220,7 +223,7 @@ 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 }, @@ -236,7 +239,7 @@ end def test_parse_with_single_hyphen - input = %w(foo - bar) + input = %w[foo - bar] definitions = [] parser = Cri::OptionParser.parse(input, definitions) @@ -246,7 +249,7 @@ 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) @@ -257,7 +260,7 @@ 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 }, ] @@ -268,15 +271,140 @@ 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 }, ] parser = Cri::OptionParser.parse(input, definitions) - assert_equal(%w(test test2), parser.options[:long]) + assert_equal(%w[test test2], parser.options[:long]) assert_equal(3, parser.options[:verbose].size) end + + def test_parse_with_default_required_unspecified + input = %w[foo] + definitions = [ + { long: 'animal', short: 'a', argument: :required, default: 'donkey' }, + ] + + parser = Cri::OptionParser.parse(input, definitions) + + assert_equal({ animal: 'donkey' }, parser.options) + assert_equal(['foo'], parser.arguments) + end + + def test_parse_with_default_required_no_value + input = %w[foo -a] + definitions = [ + { long: 'animal', short: 'a', argument: :required, default: 'donkey' }, + ] + + assert_raises(Cri::OptionParser::OptionRequiresAnArgumentError) do + Cri::OptionParser.parse(input, definitions) + end + end + + def test_parse_with_default_required_value + input = %w[foo -a giraffe] + definitions = [ + { long: 'animal', short: 'a', argument: :required, default: 'donkey' }, + ] + + parser = Cri::OptionParser.parse(input, definitions) + + assert_equal({ animal: 'giraffe' }, parser.options) + assert_equal(['foo'], parser.arguments) + end + + def test_parse_with_default_optional_unspecified + input = %w[foo] + definitions = [ + { long: 'animal', short: 'a', argument: :optional, default: 'donkey' }, + ] + + parser = Cri::OptionParser.parse(input, definitions) + + assert_equal({ animal: 'donkey' }, parser.options) + assert_equal(['foo'], parser.arguments) + end + + def test_parse_with_default_optional_no_value + input = %w[foo -a] + definitions = [ + { long: 'animal', short: 'a', argument: :optional, default: 'donkey' }, + ] + + parser = Cri::OptionParser.parse(input, definitions) + + assert_equal({ animal: 'donkey' }, parser.options) + assert_equal(['foo'], parser.arguments) + end + + def test_parse_with_default_optional_value + input = %w[foo -a giraffe] + definitions = [ + { long: 'animal', short: 'a', argument: :optional, default: 'donkey' }, + ] + + parser = Cri::OptionParser.parse(input, definitions) + + assert_equal({ animal: 'giraffe' }, parser.options) + assert_equal(['foo'], parser.arguments) + end + + def test_parse_with_default_optional_value_and_arg + input = %w[foo -a gi raffe] + definitions = [ + { long: 'animal', short: 'a', argument: :optional, default: 'donkey' }, + ] + + parser = Cri::OptionParser.parse(input, definitions) + + assert_equal({ animal: 'gi' }, parser.options) + assert_equal(%w[foo raffe], parser.arguments) + end + + def test_parse_with_combined_required_options + input = %w[foo -abc xxx yyy zzz] + definitions = [ + { long: 'aaa', short: 'a', argument: :forbidden }, + { long: 'bbb', short: 'b', argument: :required }, + { long: 'ccc', short: 'c', argument: :required }, + ] + + parser = Cri::OptionParser.parse(input, definitions) + + assert_equal({ aaa: true, bbb: 'xxx', ccc: 'yyy' }, parser.options) + assert_equal(%w[foo zzz], parser.arguments) + end + + def test_parse_with_combined_optional_options + input = %w[foo -abc xxx yyy zzz] + definitions = [ + { long: 'aaa', short: 'a', argument: :forbidden }, + { long: 'bbb', short: 'b', argument: :optional }, + { long: 'ccc', short: 'c', argument: :required }, + ] + + parser = Cri::OptionParser.parse(input, definitions) + + assert_equal({ aaa: true, bbb: 'xxx', ccc: 'yyy' }, parser.options) + assert_equal(%w[foo zzz], parser.arguments) + end + + def test_parse_with_combined_optional_options_with_missing_value + input = %w[foo -abc xxx] + definitions = [ + { long: 'aaa', short: 'a', argument: :forbidden }, + { long: 'bbb', short: 'b', argument: :required }, + { long: 'ccc', short: 'c', argument: :optional, default: 'c default' }, + ] + + parser = Cri::OptionParser.parse(input, definitions) + + assert_equal({ aaa: true, bbb: 'xxx', ccc: 'c default' }, parser.options) + assert_equal(%w[foo], parser.arguments) + end end end
