Sha256: d1fabeec40171ac6beca99ac5e8c31c8e79a2d894c9d8010badb2715083704bb

Contents?: true

Size: 1.91 KB

Versions: 201

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

require 'eac_cli/definition/argument_option'
require 'eac_cli/definition/boolean_option'
require 'eac_cli/definition/positional_argument'

module EacCli
  class Definition
    class Alternative
      SUBCOMMAND_NAME_ARG = :subcommand
      SUBCOMMAND_ARGS_ARG = :subcommand_args

      def arg_opt(*args)
        options_set << ::EacCli::Definition::ArgumentOption.from_args(args)
      end

      def bool_opt(*args)
        options_set << ::EacCli::Definition::BooleanOption.from_args(args)
      end

      def options
        options_set.to_a
      end

      def options_argument?
        @options_argument ? true : false
      end

      def options_argument(enable)
        @options_argument = enable

        self
      end

      def pos_arg(name, arg_options = {})
        new_pos_arg = ::EacCli::Definition::PositionalArgument.new(name, arg_options)
        check_positional_blocked(new_pos_arg)
        pos_set << new_pos_arg
      end

      def positional
        pos_set.to_a
      end

      def positional_arguments_blocked?(new_pos_arg)
        last = pos_set.last
        return false unless last
        return true if subcommands?
        return true if last.repeat?
        return true if last.optional? && new_pos_arg.if_present(&:required?)

        false
      end

      def subcommands
        pos_arg(SUBCOMMAND_NAME_ARG, subcommand: true)
        pos_set << ::EacCli::Definition::PositionalArgument.new(SUBCOMMAND_ARGS_ARG,
                                                                optional: true, repeat: true)
      end

      def subcommands?
        pos_set.any?(&:subcommand?)
      end

      private

      def check_positional_blocked(new_pos_arg)
        raise 'Positional arguments are blocked' if positional_arguments_blocked?(new_pos_arg)
      end

      def pos_set
        @pos_set ||= []
      end

      def options_set
        @options_set ||= []
      end
    end
  end
end

Version data entries

201 entries across 201 versions & 4 rubygems

Version Path
eac_cli-0.34.1 lib/eac_cli/definition/alternative.rb
eac_cli-0.34.0 lib/eac_cli/definition/alternative.rb
eac_tools-0.66.0 sub/eac_cli/lib/eac_cli/definition/alternative.rb
eac_tools-0.65.1 sub/eac_cli/lib/eac_cli/definition/alternative.rb
eac_tools-0.65.0 sub/eac_cli/lib/eac_cli/definition/alternative.rb
eac_cli-0.33.0 lib/eac_cli/definition/alternative.rb
eac_tools-0.64.0 sub/eac_cli/lib/eac_cli/definition/alternative.rb
eac_cli-0.32.0 lib/eac_cli/definition/alternative.rb
eac_tools-0.63.0 sub/eac_cli/lib/eac_cli/definition/alternative.rb
eac_tools-0.62.1 sub/eac_cli/lib/eac_cli/definition/alternative.rb
eac_cli-0.31.0 lib/eac_cli/definition/alternative.rb
eac_tools-0.62.0 sub/eac_cli/lib/eac_cli/definition/alternative.rb
eac_tools-0.61.1 sub/eac_cli/lib/eac_cli/definition/alternative.rb
eac_tools-0.61.0 sub/eac_cli/lib/eac_cli/definition/alternative.rb
eac_tools-0.60.3 sub/eac_cli/lib/eac_cli/definition/alternative.rb
eac_tools-0.60.2 sub/eac_cli/lib/eac_cli/definition/alternative.rb
eac_tools-0.60.1 sub/eac_cli/lib/eac_cli/definition/alternative.rb
eac_tools-0.60.0 sub/eac_cli/lib/eac_cli/definition/alternative.rb
eac_tools-0.59.0 sub/eac_cli/lib/eac_cli/definition/alternative.rb
eac_tools-0.58.0 sub/eac_cli/lib/eac_cli/definition/alternative.rb