diff --git a/.travis.yml b/.travis.yml index ebede58..0159545 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,7 @@ language: crystal + script: - - scripts/test +- bin/test + +after_success: +- if [ "$TRAVIS_BRANCH" == "master" ]; then bin/deploy_doc; fi diff --git a/bin/deploy_doc b/bin/deploy_doc new file mode 100755 index 0000000..7ae8d53 --- /dev/null +++ b/bin/deploy_doc @@ -0,0 +1,65 @@ +#!/usr/bin/env ruby +require "yaml" +require "tmpdir" + +VERSION = /^refs\/tags\/v(\d+(\.\d+)*)$/ + +Dir.mktmpdir do |dir| + cred_file = File.join(dir, ".git-credentials") + `touch #{cred_file}` + `chmod 600 #{cred_file}` + File.write cred_file, "https://mosop:#{ENV["MOSOP_GITHUB_ACCESS_TOKEN"]}@github.com\n" + Dir.mktmpdir do |dir| + Dir.chdir(dir) do + `git init` + `git config --local credential.helper 'store --file #{cred_file}'` + `git remote add origin https://github.com/mosop/optarg.git` + gh_pages_found = false + versions = [] + `git ls-remote`.chomp.split("\n").each do |line| + sha, ref = line.split(/\s+/) + if VERSION =~ ref + versions << Gem::Version.new($1) + elsif ref == "refs/heads/gh-pages" + gh_pages_found = true + end + end + version = versions.sort.last + exit unless version + `git fetch origin v#{version}:tags/v#{version}` + `git checkout -b latest v#{version}` + `crystal deps` + `crystal doc` + doc_dir = File.join(dir, "doc") + Dir.mktmpdir do |dir| + Dir.chdir(dir) do + ver_dir = File.join(dir, "v#{version}") + if gh_pages_found + `git init` + `git config --local credential.helper 'store --file #{cred_file}'` + `git remote add origin https://github.com/mosop/optarg.git` + `git pull origin gh-pages` + `git checkout -b gh-pages origin/gh-pages` + Dir.glob("*") do |f| + `rm -rf #{f}` unless f.start_with?("v") + end + `rm -rf .git` + end + `cp -a #{doc_dir}/* #{dir}/` + `rm -rf #{ver_dir}` + `mkdir -p #{ver_dir}` + `cp -a #{doc_dir}/* #{ver_dir}/` + `git init` + `git config --local credential.helper 'store --file #{cred_file}'` + `git remote add origin https://github.com/mosop/optarg.git` + `git config --add --local user.name mosop` + `git config --add --local user.email mosop@users.noreply.github.com` + `git checkout -b gh-pages` + `git add .` + `git commit -m '#{Time.now.strftime("%Y%m%d")}'` + `git push -f origin gh-pages` + end + end + end + end +end diff --git a/scripts/test b/bin/test similarity index 100% rename from scripts/test rename to bin/test diff --git a/shard.yml b/shard.yml index 312156b..fa00946 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: optarg -version: 0.5.0 +version: 0.5.1 authors: - mosop @@ -11,4 +11,4 @@ crystal: 0.20.5 dependencies: callback: github: mosop/callback - version: ~> 0.6.0 + version: ~> 0.6.1 diff --git a/spec/classes/argument_value_container_spec.cr b/spec/classes/argument_value_container_spec.cr index 3f84e34..a8c6817 100644 --- a/spec/classes/argument_value_container_spec.cr +++ b/spec/classes/argument_value_container_spec.cr @@ -6,7 +6,7 @@ module OptargArgumentValueContainerClassFeature it "#[]" do model = Model.new(%w()) - model.parser.parsed_args << "arg" + model.__parser.parsed_args << "arg" model[0].should eq "arg" end diff --git a/spec/features/argument_array_spec.cr b/spec/features/argument_array_spec.cr index 269e8ec..5b9935e 100644 --- a/spec/features/argument_array_spec.cr +++ b/spec/features/argument_array_spec.cr @@ -10,6 +10,5 @@ module OptargArgumentArrayFeature result = Model.parse(%w(foo bar baz)) result.arg.should eq "foo" result.item.should eq ["bar", "baz"] - result.parsed_args.should eq %w(foo bar baz) end end diff --git a/spec/features/detail/argument_value_container_spec.cr b/spec/features/detail/argument_value_container_spec.cr deleted file mode 100644 index 4a4b5f4..0000000 --- a/spec/features/detail/argument_value_container_spec.cr +++ /dev/null @@ -1,15 +0,0 @@ -require "../../spec_helper" - -module OptargArgumentValueContainerFeatureDetail - class Model < Optarg::Model - end - - it name do - result = Model.parse(%w(foo bar baz)) - result.nameless_args.size.should eq 3 - result.nameless_args[0].should eq "foo" - result.nameless_args[1].should eq "bar" - result.nameless_args[2].should eq "baz" - result.nameless_args.map{|i| "#{i}!"}.should eq %w(foo! bar! baz!) - end -end diff --git a/spec/features/detail/avoiding_overriding_methods_spec.cr b/spec/features/detail/avoiding_overriding_methods_spec.cr deleted file mode 100644 index d774d0d..0000000 --- a/spec/features/detail/avoiding_overriding_methods_spec.cr +++ /dev/null @@ -1,13 +0,0 @@ -require "../../spec_helper" - -module OptargAvoidingOverridingMethodsFeatureDetail - class Model < Optarg::Model - string "--class" - end - - it name do - result = Model.parse(%w(--class foo)) - result.class.should eq Model - result[String]["--class"].should eq "foo" - end -end diff --git a/spec/features/detail/bool_option_value_accessor_spec.cr b/spec/features/detail/bool_option_value_accessor_spec.cr deleted file mode 100644 index 3018026..0000000 --- a/spec/features/detail/bool_option_value_accessor_spec.cr +++ /dev/null @@ -1,15 +0,0 @@ -require "../../spec_helper" - -module OptargBoolOptionValueAccessorFeatureDetail - class Model < Optarg::Model - bool "-b" - end - - it name do - result = Model.parse(%w(-b)) - result.b?.should be_true - - not_set = Model.parse(%w()) - not_set.b?.should be_false - end -end diff --git a/spec/features/detail/named_argument_value_accessor_spec.cr b/spec/features/detail/named_argument_value_accessor_spec.cr deleted file mode 100644 index 51d84b4..0000000 --- a/spec/features/detail/named_argument_value_accessor_spec.cr +++ /dev/null @@ -1,14 +0,0 @@ -require "../../spec_helper" - -module OptargNamedArgumentValueAccessorFeatureDetail - class Model < Optarg::Model - arg "arg1" - arg "arg2" - end - - it name do - result = Model.parse(%w(foo bar)) - result.arg1.should eq "foo" - result.arg2.should eq "bar" - end -end diff --git a/spec/features/detail/named_argument_value_hash_spec.cr b/spec/features/detail/named_argument_value_hash_spec.cr deleted file mode 100644 index 5834c72..0000000 --- a/spec/features/detail/named_argument_value_hash_spec.cr +++ /dev/null @@ -1,13 +0,0 @@ -require "../../spec_helper" - -module OptargNamedArgumentValueHashFeatureDetail - class Model < Optarg::Model - arg "named" - end - - it name do - result = Model.parse(%w(foo bar baz)) - expected = {"named" => "foo"} - result[String].should eq expected - end -end diff --git a/spec/features/detail/nameless_argument_value_hash_spec.cr b/spec/features/detail/nameless_argument_value_hash_spec.cr deleted file mode 100644 index 8be14c4..0000000 --- a/spec/features/detail/nameless_argument_value_hash_spec.cr +++ /dev/null @@ -1,13 +0,0 @@ -require "../../spec_helper" - -module OptargNamelessArgumentValueHashFeatureDetail - class Model < Optarg::Model - arg "named" - end - - it name do - result = Model.parse(%w(foo bar baz)) - expected = %w(bar baz) - result.nameless_args.should eq expected - end -end diff --git a/spec/features/detail/option_value_container_spec.cr b/spec/features/detail/option_value_container_spec.cr deleted file mode 100644 index f39b47c..0000000 --- a/spec/features/detail/option_value_container_spec.cr +++ /dev/null @@ -1,31 +0,0 @@ -require "../../spec_helper" - -module OptargOptionValueContainerFeatureDetail - class Model < Optarg::Model - string "-s" - bool "-b" - array "-a" - end - - it name do - result = Model.parse(%w(-s foo -b -a bar -a baz)) - s = {"-s" => "foo"} - b = {"-b" => true} - a = {"-a" => ["bar", "baz"]} - result[String].should eq s - result[Bool].should eq b - result[Array(String)].should eq a - end - - module KeyForMultipleName - class Model < Optarg::Model - bool %w(-f --force) - end - - it name do - result = Model.parse(%w(--force)) - result[Bool]["-f"].should be_true - expect_raises(KeyError) { result[Bool]["--force"] } - end - end -end diff --git a/spec/features/detail/parsed_argument_value_array_spec.cr b/spec/features/detail/parsed_argument_value_array_spec.cr deleted file mode 100644 index b2cc2d8..0000000 --- a/spec/features/detail/parsed_argument_value_array_spec.cr +++ /dev/null @@ -1,13 +0,0 @@ -require "../../spec_helper" - -module OptargParsedArgumentValueArrayFeatureDetail - class Model < Optarg::Model - arg "named" - end - - it name do - result = Model.parse(%w(foo bar baz)) - expected = %w(foo bar baz) - result.parsed_args.should eq expected - end -end diff --git a/spec/features/detail/string_array_option_value_accessor_spec.cr b/spec/features/detail/string_array_option_value_accessor_spec.cr deleted file mode 100644 index 080113a..0000000 --- a/spec/features/detail/string_array_option_value_accessor_spec.cr +++ /dev/null @@ -1,12 +0,0 @@ -require "../../spec_helper" - -module OptargStringArrayOptionValueAccessorFeatureDetail - class Model < Optarg::Model - array "-a" - end - - it name do - result = Model.parse(%w(-a foo -a bar -a baz)) - result.a.should eq %w(foo bar baz) - end -end diff --git a/spec/features/detail/string_option_value_accessor_spec.cr b/spec/features/detail/string_option_value_accessor_spec.cr deleted file mode 100644 index 6ea9c2e..0000000 --- a/spec/features/detail/string_option_value_accessor_spec.cr +++ /dev/null @@ -1,16 +0,0 @@ -require "../../spec_helper" - -module OptargStringOptionValueAccessorFeatureDetail - class Model < Optarg::Model - string "-s" - end - - it name do - result = Model.parse(%w(-s value)) - result.s.should eq "value" - - not_set = Model.parse(%w()) - expect_raises(KeyError) { not_set.s } - not_set.s?.should be nil - end -end diff --git a/spec/features/detail/unparsed_argument_value_array_spec.cr b/spec/features/detail/unparsed_argument_value_array_spec.cr deleted file mode 100644 index 343e82a..0000000 --- a/spec/features/detail/unparsed_argument_value_array_spec.cr +++ /dev/null @@ -1,13 +0,0 @@ -require "../../spec_helper" - -module OptargUnparsedArgumentValueArrayFeatureDetail - class Model < Optarg::Model - arg "stopper", stop: true - end - - it name do - result = Model.parse(%w(foo bar baz)) - expected = %w(bar baz) - result.unparsed_args.should eq expected - end -end diff --git a/spec/features/named_arguments_spec.cr b/spec/features/named_arguments_spec.cr index 87f0981..a87d971 100644 --- a/spec/features/named_arguments_spec.cr +++ b/spec/features/named_arguments_spec.cr @@ -10,7 +10,6 @@ module OptargNamedArgumentsFeature result = Model.parse(%w(/path/to/src /path/to/build and more)) result.src_dir.should eq "/path/to/src" result.build_dir.should eq "/path/to/build" - result.parsed_args.should eq ["/path/to/src", "/path/to/build", "and", "more"] result[String].should eq({"src_dir" => "/path/to/src", "build_dir" => "/path/to/build"}) result.nameless_args.should eq ["and", "more"] end diff --git a/spec/fix/inherit_definition_spec.cr b/spec/fix/inherit_definition_spec.cr index ff4129c..28459af 100644 --- a/spec/fix/inherit_definition_spec.cr +++ b/spec/fix/inherit_definition_spec.cr @@ -13,31 +13,31 @@ module OptargFixInheritDefinitionFeature end it name do - Sub.definitions.all.size.should eq 5 - Sub.definitions.arguments.size.should eq 1 - Sub.definitions.options.size.should eq 3 - Sub.definitions.value_options.size.should eq 2 - Sub.definitions.values.size.should eq 3 - Sub.definitions.handlers.size.should eq 1 - Sub.definitions.terminators.size.should eq 1 - Sub.definitions.argument_list.size.should eq 1 + Sub.__klass.definitions.all.size.should eq 5 + Sub.__klass.definitions.arguments.size.should eq 1 + Sub.__klass.definitions.options.size.should eq 3 + Sub.__klass.definitions.value_options.size.should eq 2 + Sub.__klass.definitions.values.size.should eq 3 + Sub.__klass.definitions.handlers.size.should eq 1 + Sub.__klass.definitions.terminators.size.should eq 1 + Sub.__klass.definitions.argument_list.size.should eq 1 - Sub.definitions.all["arg"]?.should_not be_nil - Sub.definitions.all["-s"]?.should_not be_nil - Sub.definitions.all["-b"]?.should_not be_nil - Sub.definitions.all["-h"]?.should_not be_nil - Sub.definitions.all["--"]?.should_not be_nil - Sub.definitions.arguments["arg"]?.should_not be_nil - Sub.definitions.options["-s"]?.should_not be_nil - Sub.definitions.options["-b"]?.should_not be_nil - Sub.definitions.options["-h"]?.should_not be_nil - Sub.definitions.value_options["-s"]?.should_not be_nil - Sub.definitions.value_options["-b"]?.should_not be_nil - Sub.definitions.values["arg"]?.should_not be_nil - Sub.definitions.values["-s"]?.should_not be_nil - Sub.definitions.values["-b"]?.should_not be_nil - Sub.definitions.handlers["-h"]?.should_not be_nil - Sub.definitions.terminators["--"]?.should_not be_nil - Sub.definitions.argument_list[0]?.should_not be_nil + Sub.__klass.definitions.all["arg"]?.should_not be_nil + Sub.__klass.definitions.all["-s"]?.should_not be_nil + Sub.__klass.definitions.all["-b"]?.should_not be_nil + Sub.__klass.definitions.all["-h"]?.should_not be_nil + Sub.__klass.definitions.all["--"]?.should_not be_nil + Sub.__klass.definitions.arguments["arg"]?.should_not be_nil + Sub.__klass.definitions.options["-s"]?.should_not be_nil + Sub.__klass.definitions.options["-b"]?.should_not be_nil + Sub.__klass.definitions.options["-h"]?.should_not be_nil + Sub.__klass.definitions.value_options["-s"]?.should_not be_nil + Sub.__klass.definitions.value_options["-b"]?.should_not be_nil + Sub.__klass.definitions.values["arg"]?.should_not be_nil + Sub.__klass.definitions.values["-s"]?.should_not be_nil + Sub.__klass.definitions.values["-b"]?.should_not be_nil + Sub.__klass.definitions.handlers["-h"]?.should_not be_nil + Sub.__klass.definitions.terminators["--"]?.should_not be_nil + Sub.__klass.definitions.argument_list[0]?.should_not be_nil end end diff --git a/spec/internal/argument_display_name_spec.cr b/spec/internal/argument_display_name_spec.cr index 237619c..ea56af7 100644 --- a/spec/internal/argument_display_name_spec.cr +++ b/spec/internal/argument_display_name_spec.cr @@ -21,8 +21,8 @@ module OptargInternalArugmentDisplayNameFeature end it name do - Model.definitions.arguments["arg"].metadata.display_name.should eq "arg" - Upcase.definitions.arguments["arg"].metadata.display_name.should eq "ARG" - Upcase.definitions.options["--option"].metadata.display_name.should eq "--option" + Model.__klass.definitions.arguments["arg"].metadata.display_name.should eq "arg" + Upcase.__klass.definitions.arguments["arg"].metadata.display_name.should eq "ARG" + Upcase.__klass.definitions.options["--option"].metadata.display_name.should eq "--option" end end diff --git a/spec/internal/raise_unknown_option_if_concatenated_options_not_matched_spec.cr b/spec/internal/raise_unknown_option_if_concatenated_options_not_matched_spec.cr index 2e3a420..3ba9e69 100644 --- a/spec/internal/raise_unknown_option_if_concatenated_options_not_matched_spec.cr +++ b/spec/internal/raise_unknown_option_if_concatenated_options_not_matched_spec.cr @@ -6,6 +6,6 @@ module OptargInternalRaiseUnknownOptionIfConcatenatedOptionsNotMatchedFeature it name do model = Model.new(%w(-ab)) - expect_raises(Optarg::UnknownOption, Optarg::UnknownOption.new(model.__parser, "-a").message) { model.parse } + expect_raises(Optarg::UnknownOption, Optarg::UnknownOption.new(model.__parser, "-a").message) { model.__parse } end end diff --git a/spec/internal/raise_unsupported_concatenation_spec.cr b/spec/internal/raise_unsupported_concatenation_spec.cr index 1f6e8aa..9787ec2 100644 --- a/spec/internal/raise_unsupported_concatenation_spec.cr +++ b/spec/internal/raise_unsupported_concatenation_spec.cr @@ -8,6 +8,6 @@ module OptargInternalRaiseUnsupportedConcatenationFeature it name do model = Model.new(%w(-sa)) - expect_raises(Optarg::UnsupportedConcatenation, Optarg::UnsupportedConcatenation.new(model.__parser, Model.definitions.options["-s"]).message) { model.__parse} + expect_raises(Optarg::UnsupportedConcatenation, Optarg::UnsupportedConcatenation.new(model.__parser, Model.__klass.definitions.options["-s"]).message) { model.__parse} end end diff --git a/spec/internal/required_argument_error_preserves_object_spec.cr b/spec/internal/required_argument_error_preserves_object_spec.cr index ce6c29e..64dbf35 100644 --- a/spec/internal/required_argument_error_preserves_object_spec.cr +++ b/spec/internal/required_argument_error_preserves_object_spec.cr @@ -13,6 +13,6 @@ module OptargInternalRequiredArgumentErrorPreservesObjectFeature error = ex ensure end - error.as(Optarg::Definitions::StringArgument::Validations::Existence::Error).definition.should be Model.definitions.arguments["arg1"] + error.as(Optarg::Definitions::StringArgument::Validations::Existence::Error).definition.should be Model.__klass.definitions.arguments["arg1"] end end diff --git a/spec/internal_spec.cr b/spec/internal_spec.cr index 3996dbc..ca0a30e 100644 --- a/spec/internal_spec.cr +++ b/spec/internal_spec.cr @@ -15,14 +15,14 @@ module OptargInternalFeature result.s.should eq "v" result.s?.should eq "v" result.b?.should be_true - result.parsed_args.should eq %w(arg parsed) + result.__parser.parsed_args.should eq %w(arg parsed) result[String].should eq({"-s" => "v", "arg" => "arg"}) result.nameless_args.should eq %w(parsed) result.unparsed_args.should eq %w(unparsed) - result.__parsed_nodes[0].should eq Optarg::Parser.new_node(%w(-s v), ParseModel.definitions.options["-s"]) - result.__parsed_nodes[1].should eq Optarg::Parser.new_node(%w(-b), ParseModel.definitions.options["-b"]) - result.__parsed_nodes[2].should eq Optarg::Parser.new_node(%w(arg), ParseModel.definitions.arguments["arg"]) - result.__parsed_nodes[3].should eq Optarg::Parser.new_node(%w(parsed)) + result.__parser.parsed_nodes[0].should eq Optarg::Parser.new_node(%w(-s v), ParseModel.__klass.definitions.options["-s"]) + result.__parser.parsed_nodes[1].should eq Optarg::Parser.new_node(%w(-b), ParseModel.__klass.definitions.options["-b"]) + result.__parser.parsed_nodes[2].should eq Optarg::Parser.new_node(%w(arg), ParseModel.__klass.definitions.arguments["arg"]) + result.__parser.parsed_nodes[3].should eq Optarg::Parser.new_node(%w(parsed)) end it "parses nothing" do @@ -32,9 +32,9 @@ module OptargInternalFeature result.s?.should be_nil result.b?.should be_false result.nameless_args.should eq %w() - result.parsed_args.should eq %w() + result.__parser.parsed_args.should eq %w() result.unparsed_args.should eq %w() - result.__parsed_nodes.should eq [] of Array(String) + result.__parser.parsed_nodes.should eq [] of Array(String) end class Supermodel < Optarg::Model @@ -167,12 +167,12 @@ module OptargInternalFeature describe "Metadata" do it "preserves metadata" do - MetadataModel.definitions.options["-s"].metadata.as(MetadataModel::Metadata).data.should eq "string" - MetadataModel.definitions.options["-b"].metadata.as(MetadataModel::Metadata).data.should eq "bool" - MetadataModel.definitions.options["-a"].metadata.as(MetadataModel::Metadata).data.should eq "array" - MetadataModel.definitions.arguments["arg"].metadata.as(MetadataModel::Metadata).data.should eq "arg" - MetadataModel.definitions.handlers["--help"].metadata.as(MetadataModel::Metadata).data.should eq "handler" - MetadataModel.definitions.terminators["--"].metadata.as(MetadataModel::Metadata).data.should eq "terminator" + MetadataModel.__klass.definitions.options["-s"].metadata.as(MetadataModel::Metadata).data.should eq "string" + MetadataModel.__klass.definitions.options["-b"].metadata.as(MetadataModel::Metadata).data.should eq "bool" + MetadataModel.__klass.definitions.options["-a"].metadata.as(MetadataModel::Metadata).data.should eq "array" + MetadataModel.__klass.definitions.arguments["arg"].metadata.as(MetadataModel::Metadata).data.should eq "arg" + MetadataModel.__klass.definitions.handlers["--help"].metadata.as(MetadataModel::Metadata).data.should eq "handler" + MetadataModel.__klass.definitions.terminators["--"].metadata.as(MetadataModel::Metadata).data.should eq "terminator" end end end diff --git a/src/lib/completion.cr b/src/lib/completion.cr index 404e170..ec4a5c0 100644 --- a/src/lib/completion.cr +++ b/src/lib/completion.cr @@ -1,4 +1,5 @@ module Optarg + # :nodoc: class Completion getter type : Symbol getter model : ModelClass diff --git a/src/lib/completion_generators.cr b/src/lib/completion_generators.cr index 3807b8f..377e9ac 100644 --- a/src/lib/completion_generators.cr +++ b/src/lib/completion_generators.cr @@ -1,2 +1,6 @@ +# :nodoc: +module Optarg::CompletionGenerators +end + require "./completion_generators/base" require "./completion_generators/*" diff --git a/src/lib/completion_generators/base.cr b/src/lib/completion_generators/base.cr index f9fff82..ada345a 100644 --- a/src/lib/completion_generators/base.cr +++ b/src/lib/completion_generators/base.cr @@ -1,4 +1,4 @@ -class Optarg::CompletionGenerators +module Optarg::CompletionGenerators abstract class Base include Completion::TextFormatter diff --git a/src/lib/completion_generators/bash.cr b/src/lib/completion_generators/bash.cr index 8bdb1eb..548ec3c 100644 --- a/src/lib/completion_generators/bash.cr +++ b/src/lib/completion_generators/bash.cr @@ -1,4 +1,4 @@ -class Optarg::CompletionGenerators +module Optarg::CompletionGenerators class Bash < Base end end diff --git a/src/lib/completion_generators/zsh.cr b/src/lib/completion_generators/zsh.cr index 7f3b966..56d73ff 100644 --- a/src/lib/completion_generators/zsh.cr +++ b/src/lib/completion_generators/zsh.cr @@ -1,4 +1,4 @@ -class Optarg::CompletionGenerators +module Optarg::CompletionGenerators class Zsh < Base end end diff --git a/src/lib/definition_mixins.cr b/src/lib/definition_mixins.cr index e8c5a1e..8f72a85 100644 --- a/src/lib/definition_mixins.cr +++ b/src/lib/definition_mixins.cr @@ -1 +1,5 @@ +# :nodoc: +module Optarg::DefinitionMixins +end + require "./definition_mixins/*" diff --git a/src/lib/definition_set.cr b/src/lib/definition_set.cr index c8b9854..7e679f7 100644 --- a/src/lib/definition_set.cr +++ b/src/lib/definition_set.cr @@ -1,4 +1,5 @@ module Optarg + # :nodoc: class DefinitionSet macro __set(types, list, array = nil) {% diff --git a/src/lib/definitions.cr b/src/lib/definitions.cr index 9648e4f..525ef27 100644 --- a/src/lib/definitions.cr +++ b/src/lib/definitions.cr @@ -1,2 +1,6 @@ +# :nodoc: +module Optarg::Definitions +end + require "./definitions/base" require "./definitions/*" diff --git a/src/lib/exceptions.cr b/src/lib/exceptions.cr index 7c37da2..c97f9fa 100644 --- a/src/lib/exceptions.cr +++ b/src/lib/exceptions.cr @@ -1,18 +1,22 @@ module Optarg class ParsingError < Exception + # :nodoc: getter parser : Parser + # :nodoc: def initialize(@parser, message) super message end end + # :nodoc: class UnknownOption < ParsingError def initialize(parser, name) super parser, "The #{name} option is unknown." end end + # :nodoc: class MissingValue < ParsingError getter option : DefinitionMixins::Option @@ -21,6 +25,7 @@ module Optarg end end + # :nodoc: class UnsupportedConcatenation < ParsingError getter option : DefinitionMixins::Option @@ -31,6 +36,7 @@ module Optarg end class ValidationError < ParsingError + # :nodoc: def initialize(parser, message) super parser, message end diff --git a/src/lib/metadata.cr b/src/lib/metadata.cr index 3a2b6a2..9630789 100644 --- a/src/lib/metadata.cr +++ b/src/lib/metadata.cr @@ -1,4 +1,5 @@ module Optarg + # :nodoc: class Metadata @definition : Definitions::Base? def definition=(value) diff --git a/src/lib/model.cr b/src/lib/model.cr index 185a5c3..505f947 100644 --- a/src/lib/model.cr +++ b/src/lib/model.cr @@ -1,4 +1,5 @@ module Optarg + # The base of model classes. abstract class Model macro inherited {% if @type.superclass == ::Optarg::Model %} @@ -18,42 +19,23 @@ module Optarg %} {% end %} - {% - class_id = @type.name.underscore.split("::").join("__").id - last_concrete_id = "LastConcrete_#{class_id}".id - %} - - {% if @type.abstract? %} - {% - last_concrete = superlast_concrete - %} - {% else %} - {% - last_concrete = @type - %} - {% end %} - - {% if last_concrete %} - alias {{last_concrete_id}} = ::{{last_concrete}} - {% end %} - @@__klass = ::Optarg::ModelClass.new( supermodel: {{ is_root ? nil : "::#{@type.superclass}.__klass".id }}, name: {{@type.name.split("::")[-1].underscore}}, abstract: {{@type.abstract?}} ) + # :nodoc: def self.__klass; @@__klass; end - def self.klass; @@__klass; end - - def self.__definitions; @@__klass.definitions; end - def self.definitions; @@__klass.definitions; end {% unless is_root %} - ::{{@type.superclass}}.__definitions.all.each do |kv| - __definitions << kv[1].subclassify(::{{@type}}) + ::{{@type.superclass}}.__klass.definitions.all.each do |kv| + @@__klass.definitions << kv[1].subclassify(::{{@type}}) end {% end %} + # A dedicated Optarg::Parser subclass for the model class. + # + # This class is automatically defined by the optarg library. {% if @type.abstract? %} abstract class Parser < ::{{superparser}} inherit_callback_group :validate, ::Proc(::{{@type}}, ::Nil) @@ -62,7 +44,8 @@ module Optarg class Parser < ::{{superparser}} inherit_callback_group :validate, ::Proc(::{{@type}}, ::Nil) - def data + # Returns a target model instance. + def data : ::{{@type}} @data.as(::{{@type}}) end end @@ -71,6 +54,7 @@ module Optarg (@__parser.var ||= Parser.new(self)).as(Parser) end + # :nodoc: class DefinitionContext @definition : ::Optarg::Definitions::Base @@ -86,74 +70,85 @@ module Optarg end end + # :nodoc: def self.__with_definition(df) yield DefinitionContext.new(df) end {% end %} end + # :nodoc: + def __klass; self.class.__klass; end + @__parser = Util::Var(Parser).new + # :nodoc: getter __argv : Array(String) + # :nodoc: def initialize(@__argv) end + # :nodoc: def self.__parse(argv, *args) new(argv, *args).tap do |o| o.__parse end end + # Creates a new model instance and parses the *argv* arguments. + # + # Returns the created instance. + def self.parse(argv : Array(String), *args) + __parse(argv, *args) + end + + # :nodoc: def __parse __parser.parse end - def self.klass; __klass; end - def self.parse(argv, *args); __parse(argv, *args); end - - def parse; __parse; end - def parser; __parser; end - - def __nameless_args; __parser.nameless_args; end - def nameless_args; __nameless_args; end - - def __parsed_args; __parser.parsed_args; end - def parsed_args; __parsed_args; end - - def __unparsed_args; __parser.unparsed_args; end - def unparsed_args; __unparsed_args; end - - def __parsed_nodes; __parser.parsed_nodes; end + # Returns an array that contains nameless argument values. + def nameless_args; __parser.nameless_args; end - def __definitions; self.class.__definitions; end + # Returns an array that contains unparsed argument values. + def unparsed_args; __parser.unparsed_args; end + # Returns a value hash for String-type options and arguments. def [](klass : String.class) __parser.args[klass] end + # Returns a value hash for Bool-type options and arguments. def [](klass : Bool.class) __parser.args[klass] end + # Returns a value hash for Array(String)-type options and arguments. def [](klass : Array(String).class) __parser.args[klass] end + # Returns an argument value at the *index*. def [](index : Int32) __parser.parsed_args[index] end + # Returns an argument value at the *index*. + # + # Returns nil if the *index* is out of range. def []?(index : Int32) __parser.parsed_args[index]? end + # Iterates argument values. def each __parser.parsed_args.each do |i| yield i end end + # :nodoc: def self.__with_self(*args) with self yield *args end diff --git a/src/lib/model/dsl/arg.cr b/src/lib/model/dsl/arg.cr index 477b827..15864ab 100644 --- a/src/lib/model/dsl/arg.cr +++ b/src/lib/model/dsl/arg.cr @@ -1,9 +1,10 @@ module Optarg class Model + # Defines a String argument model item. macro arg(names, metadata = nil, stop = nil, default = nil, required = nil, any_of = nil, complete = nil, _mixin = nil, &block) - define_static_value :argument, :nilable, ::Optarg::Definitions::StringArgument, {{names}}, nil, {{_mixin}} do |klass| + __define_static_value :argument, :nilable, ::Optarg::Definitions::StringArgument, {{names}}, nil, {{_mixin}} do |klass| arg = klass.new({{names}}, metadata: {{metadata}}, stop: {{stop}}, required: {{required}}, default: {{default}}, any_of: {{any_of}}, complete: {{complete}}) - __definitions << arg + @@__klass.definitions << arg {% if block %} __with_definition(option) {{block}} {% end %} diff --git a/src/lib/model/dsl/arg_array.cr b/src/lib/model/dsl/arg_array.cr index e3fcfff..b097658 100644 --- a/src/lib/model/dsl/arg_array.cr +++ b/src/lib/model/dsl/arg_array.cr @@ -1,9 +1,10 @@ module Optarg class Model + # Defines an Array(String) argument model item. macro arg_array(names, metadata = nil, default = nil, min = nil, any_item_of = nil, complete = nil, _mixin = nil, &block) - define_static_value :argument, :nilable, ::Optarg::Definitions::StringArrayArgument, {{names}}, nil, {{_mixin}} do |klass| + __define_static_value :argument, :nilable, ::Optarg::Definitions::StringArrayArgument, {{names}}, nil, {{_mixin}} do |klass| option = klass.new({{names}}, metadata: {{metadata}}, default: {{default}}, min: {{min}}, any_item_of: {{any_item_of}}, complete: {{complete}}) - __definitions << option + @@__klass.definitions << option {% if block %} __with_definition(option) {{block}} {% end %} diff --git a/src/lib/model/dsl/array.cr b/src/lib/model/dsl/array.cr index cbb2b68..d44ad64 100644 --- a/src/lib/model/dsl/array.cr +++ b/src/lib/model/dsl/array.cr @@ -1,9 +1,10 @@ module Optarg class Model + # Defines an Array(String) option model item. macro array(names, metadata = nil, default = nil, min = nil, any_item_of = nil, complete = nil, _mixin = nil, &block) - define_static_value :option, :nilable, ::Optarg::Definitions::StringArrayOption, {{names}}, nil, {{_mixin}} do |klass| + __define_static_value :option, :nilable, ::Optarg::Definitions::StringArrayOption, {{names}}, nil, {{_mixin}} do |klass| option = klass.new({{names}}, metadata: {{metadata}}, default: {{default}}, min: {{min}}, any_item_of: {{any_item_of}}, complete: {{complete}}) - __definitions << option + @@__klass.definitions << option {% if block %} __with_definition(option) {{block}} {% end %} diff --git a/src/lib/model/dsl/bool.cr b/src/lib/model/dsl/bool.cr index 6730939..632d8a9 100644 --- a/src/lib/model/dsl/bool.cr +++ b/src/lib/model/dsl/bool.cr @@ -1,12 +1,13 @@ module Optarg class Model + # Defines a Bool option model item. macro bool(names, metadata = nil, stop = nil, default = nil, not = nil, _mixin = nil, &block) - define_static_value :option, :predicate, ::Optarg::Definitions::BoolOption, {{names}}, nil, {{_mixin}} do |klass| + __define_static_value :option, :predicate, ::Optarg::Definitions::BoolOption, {{names}}, nil, {{_mixin}} do |klass| option = klass.new({{names}}, metadata: {{metadata}}, stop: {{stop}}, default: {{default}}) - __definitions << option + @@__klass.definitions << option {% if not %} not = ::Optarg::Definitions::NotOption.new({{not}}, option, metadata: nil, stop: {{stop}}) - __definitions << not + @@__klass.definitions << not {% end %} {% if block %} __with_definition(option) {{block}} diff --git a/src/lib/model/dsl/handler.cr b/src/lib/model/dsl/handler.cr index 6e9e854..360d29d 100644 --- a/src/lib/model/dsl/handler.cr +++ b/src/lib/model/dsl/handler.cr @@ -1,9 +1,10 @@ module Optarg class Model + # Defines a handler model item. macro on(names, metadata = nil, stop = nil, &block) - define_static_handler nil, {{names}} {{block}} - %handler = create_static_handler({{names}}, metadata: {{metadata}}, stop: {{stop}}) - __definitions << %handler + __define_static_handler nil, {{names}} {{block}} + %handler = __create_static_handler({{names}}, metadata: {{metadata}}, stop: {{stop}}) + @@__klass.definitions << %handler end end end diff --git a/src/lib/model/dsl/string.cr b/src/lib/model/dsl/string.cr index 79edd63..ccc89b7 100644 --- a/src/lib/model/dsl/string.cr +++ b/src/lib/model/dsl/string.cr @@ -1,9 +1,10 @@ module Optarg class Model + # Defines a String option model item. macro string(names, metadata = nil, stop = nil, default = nil, required = nil, any_of = nil, complete = nil, _mixin = nil, &block) - define_static_value :option, :nilable, ::Optarg::Definitions::StringOption, {{names}}, nil, {{_mixin}} do |klass| + __define_static_value :option, :nilable, ::Optarg::Definitions::StringOption, {{names}}, nil, {{_mixin}} do |klass| option = klass.new({{names}}, metadata: {{metadata}}, stop: {{stop}}, default: {{default}}, required: {{required}}, any_of: {{any_of}}, complete: {{complete}}) - __definitions << option + @@__klass.definitions << option {% if block %} __with_definition(option) {{block}} {% end %} diff --git a/src/lib/model/dsl/terminator.cr b/src/lib/model/dsl/terminator.cr index ed3967f..e1f5cc8 100644 --- a/src/lib/model/dsl/terminator.cr +++ b/src/lib/model/dsl/terminator.cr @@ -1,8 +1,9 @@ module Optarg class Model + # Defines a terminator model item. macro terminator(names, metadata = nil) %term = ::Optarg::Definitions::Terminator.new({{names}}, metadata: {{metadata}}) - __definitions << %term + @@__klass.definitions << %term end end end diff --git a/src/lib/model/macros/handler.cr b/src/lib/model/macros/handler.cr index 22d2b0d..89ee043 100644 --- a/src/lib/model/macros/handler.cr +++ b/src/lib/model/macros/handler.cr @@ -1,6 +1,7 @@ module Optarg class Model - macro define_static_handler(type, names, &block) + # :nodoc: + macro __define_static_handler(type, names, &block) {% names = [names] unless names.class_name == "ArrayLiteral" method_names = names.map{|i| i.split("=")[0].gsub(/^-*/, "").gsub(/-/, "_")} @@ -8,10 +9,12 @@ module Optarg df_class = "Handler__#{name}".id %} + # :nodoc: def __call_handler_for__{{name}} {{block.body}} end + # :nodoc: class {{df_class}} < ::Optarg::Definitions::Handler def visit(parser) if data = parser.data.as?(::{{@type}}) @@ -26,7 +29,8 @@ module Optarg end end - macro create_static_handler(names, metadata, stop) + # :nodoc: + macro __create_static_handler(names, metadata, stop) {% names = [names] unless names.class_name == "ArrayLiteral" method_names = names.map{|i| i.split("=")[0].gsub(/^-*/, "").gsub(/-/, "_")} diff --git a/src/lib/model/macros/value.cr b/src/lib/model/macros/value.cr index 79a951b..eba432c 100644 --- a/src/lib/model/macros/value.cr +++ b/src/lib/model/macros/value.cr @@ -1,6 +1,7 @@ module Optarg class Model - macro define_static_value(kind, access_type, metaclass, names, value_key, _mixin, &block) + # :nodoc: + macro __define_static_value(kind, access_type, metaclass, names, value_key, _mixin, &block) {% kind = kind.id metaclass = metaclass.resolve @@ -16,11 +17,13 @@ module Optarg {% if kind == :argument %} {% + value_name = key.upcase container_method = "self".id df_getter = "#{method_names[0]}_arg".id %} {% else %} {% + value_name = key container_method = "self".id df_getter = "#{method_names[0]}_option".id %} @@ -29,18 +32,29 @@ module Optarg {% for method_name, index in method_names %} {% if access_type == :predicate %} {% unless model_reserved.includes?("#{method_name}?".id) %} + # Returns the {{value_name}} {{kind.id}} value. + # + # This method is automatically defined by the optarg library. def {{method_name}}? !!{{container_method}}[::{{metaclass}}::Typed::Type][{{value_key}}]? end {% end %} {% elsif access_type == :nilable %} {% unless model_reserved.includes?(method_name) %} + # Returns the {{value_name}} {{kind.id}} value. + # + # This method is automatically defined by the optarg library. def {{method_name}} {{container_method}}[::{{metaclass}}::Typed::Type][{{value_key}}] end {% end %} {% unless model_reserved.includes?("#{method_name}?".id) %} + # Returns the {{value_name}} {{kind.id}} value. + # + # Returns nil, if the value is undefined. + # + # This method is automatically defined by the optarg library. def {{method_name}}? {{container_method}}[::{{metaclass}}::Typed::Type][{{value_key}}]? end diff --git a/src/lib/model_class.cr b/src/lib/model_class.cr index 6cb9a13..7d09cf9 100644 --- a/src/lib/model_class.cr +++ b/src/lib/model_class.cr @@ -1,4 +1,5 @@ module Optarg + # :nodoc: class ModelClass property name : String getter? abstract : Bool diff --git a/src/lib/parser.cr b/src/lib/parser.cr index 7f497db..f62dc35 100644 --- a/src/lib/parser.cr +++ b/src/lib/parser.cr @@ -3,28 +3,46 @@ module Optarg ::Callback.enable define_callback_group :validate, Proc(Model, Nil) + # :nodoc: alias Node = NamedTuple(args: Array(String), definitions: Array(Definitions::Base)) + # Returns a target model instance. getter data : Model + # :nodoc: getter parsed_nodes = [] of Node - getter! args : ValueContainer + + # :nodoc: getter nameless_args = %w() + + # :nodoc: getter parsed_args = %w() + + # :nodoc: getter unparsed_args = %w() @argument_index = 0 + # :nodoc: getter index = 0 + # :nodoc: def initialize(data) @data = data @args = ValueContainer.new(self) end + @args : ValueContainer? + # :nodoc: + def args + @args.not_nil! + end + + # :nodoc: def input_args data.__argv end + # :nodoc: def self.new_node(args = %w(), *definitions) node = {args: args, definitions: [] of Definitions::Base} definitions.each do |df| @@ -34,10 +52,12 @@ module Optarg end @definitions : DefinitionSet? + # :nodoc: def definitions - @definitions ||= data.__definitions + @definitions ||= data.__klass.definitions end + # :nodoc: def stopped? return false if parsed_nodes.size == 0 return parsed_nodes.last[:definitions].any?{|i| i.stops? || i.terminates?} @@ -49,6 +69,7 @@ module Optarg end end + # :nodoc: def parse definitions.all.each do |kv| kv[1].initialize_before_parse(self) @@ -61,10 +82,12 @@ module Optarg end end + # :nodoc: def eol? @index == input_args.size end + # :nodoc: def resume until eol? || stopped? visit @@ -74,6 +97,7 @@ module Optarg @index = input_args.size end + # :nodoc: def visit arg = self[0] if visit_terminator @@ -86,6 +110,7 @@ module Optarg end end + # :nodoc: def visit_terminator name = self[0] if node = find_with_def(definitions.terminators, self[0]){|df| df.visit(self)} @@ -94,6 +119,7 @@ module Optarg end end + # :nodoc: def find_with_def(dfs, name) dfs.each do |kv| next unless kv[1].matches?(name) @@ -103,6 +129,7 @@ module Optarg nil end + # :nodoc: def visit_concatenated_options names = self[0][1..-1].split("").map{|i| "-#{i}"} node = Parser.new_node([self[0]]) @@ -117,6 +144,7 @@ module Optarg @index += 1 end + # :nodoc: def visit_option name = self[0] if node = find_with_def(definitions.options, name){|df| df.visit(self)} @@ -127,6 +155,7 @@ module Optarg end end + # :nodoc: def visit_argument while @argument_index < definitions.arguments.size df = definitions.argument_list[@argument_index] @@ -146,18 +175,22 @@ module Optarg @index += 1 end + # :nodoc: def [](*args) input_args[@index..-1][*args] end + # :nodoc: def left input_args.size - @index end + # Creates and raises a new `ValidationError` with the *message*. def invalidate!(message : String) invalidate! ValidationError.new(self, message) end + # :nodoc: def invalidate!(ex : ValidationError) raise ex end diff --git a/src/lib/util.cr b/src/lib/util.cr index 2551595..8d719d8 100644 --- a/src/lib/util.cr +++ b/src/lib/util.cr @@ -1 +1,5 @@ +# :nodoc: +module Optarg::Util +end + require "./util/*" diff --git a/src/lib/validation_context.cr b/src/lib/validation_context.cr index e1fff56..71b1aba 100644 --- a/src/lib/validation_context.cr +++ b/src/lib/validation_context.cr @@ -1,4 +1,5 @@ module Optarg + # :nodoc: class ValidationContext @parser : Parser @definition : Definitions::Base diff --git a/src/lib/value.cr b/src/lib/value.cr index e3230af..7d82633 100644 --- a/src/lib/value.cr +++ b/src/lib/value.cr @@ -1,4 +1,5 @@ module Optarg + # :nodoc: abstract class Value(T) include ::Comparable(Value(T)) diff --git a/src/lib/value_container.cr b/src/lib/value_container.cr index af33755..44723bc 100644 --- a/src/lib/value_container.cr +++ b/src/lib/value_container.cr @@ -1,4 +1,5 @@ module Optarg + # :nodoc: class ValueContainer getter strings : ValueTypes::String::ValueHash getter bools : ValueTypes::Bool::ValueHash diff --git a/src/lib/value_hash.cr b/src/lib/value_hash.cr index 0c24792..de7e37c 100644 --- a/src/lib/value_hash.cr +++ b/src/lib/value_hash.cr @@ -1,4 +1,5 @@ module Optarg + # :nodoc: abstract class ValueHash(V) < Hash(String, V) @fallbacked = {} of String => Bool @parser : Parser diff --git a/src/lib/value_metadata.cr b/src/lib/value_metadata.cr index 903861a..a210706 100644 --- a/src/lib/value_metadata.cr +++ b/src/lib/value_metadata.cr @@ -1,4 +1,5 @@ module Optarg + # :nodoc: class ValueMetadata(T) @value : Value(T)? diff --git a/src/lib/value_types.cr b/src/lib/value_types.cr index f52a2d2..739e8bf 100644 --- a/src/lib/value_types.cr +++ b/src/lib/value_types.cr @@ -1 +1,5 @@ +# :nodoc: +module Optarg::ValueTypes +end + require "./value_types/*" diff --git a/src/lib/value_types/base.cr b/src/lib/value_types/base.cr index c4481a7..fd33a81 100644 --- a/src/lib/value_types/base.cr +++ b/src/lib/value_types/base.cr @@ -53,6 +53,7 @@ module Optarg::ValueTypes Error.new(parser, df, self, message) end + # :nodoc: class Error < ::Optarg::ValidationError getter definition : ::{{type}} getter validation : ::\{{@type}} diff --git a/src/lib/value_validation.cr b/src/lib/value_validation.cr index 1002d9f..a172903 100644 --- a/src/lib/value_validation.cr +++ b/src/lib/value_validation.cr @@ -1,4 +1,5 @@ module Optarg + # :nodoc: abstract class ValueValidation end end