Sha256: bab09feb4eddf62d8f93203259997c194fa379b7725bae55939212fdc1861c68

Contents?: true

Size: 1.79 KB

Versions: 15

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

# @@commands_accepting
arg_name 'OPTION'
command :commands_accepting do |c|
  c.desc 'Output in single column for completion'
  c.switch %i[c column]

  c.desc 'Join multiple arguments using boolean (AND|OR|NOT)'
  c.flag [:bool], must_match: REGEX_BOOL,
                  default_value: :and,
                  type: BooleanSymbol

  c.action do |g, o, a|
    cmds = []
    commands.each { |cmd, v| cmds.push(cmd) if has_flags?(v, a, o[:bool]) }

    if o[:column]
      puts cmds.sort
    else
      description = "Commands "
      description += "not " if o[:bool] == :not
      description += "accepting "
      description += a.map { |arg| "--#{arg}" }.join(o[:bool] == :and ? ' and ' : ' or ')
      puts "#{description}: #{cmds.sort.join(', ')}"
    end
  end

  def has_flags?(options, args, bool)
    case bool
    when :and
      all_flags?(options, args)
    when :not
      no_flags?(options, args)
    else
      any_flags?(options, args)
    end
  end

  def all_flags?(options, args)
    args.each do |arg|
      has_flag = false
      options.flags.merge(options.switches).each do |_, flag|
        if flag.name == arg.to_sym || flag.aliases&.include?(arg.to_sym)
          has_flag = true
          break
        end
      end
      return false unless has_flag
    end

    true
  end

  def any_flags?(options, args)
    args.each do |option|
      options.flags.merge(options.switches).each do |_, flag|
        return true if flag.name == option.to_sym || flag.aliases&.include?(option.to_sym)
      end
    end

    false
  end

  def no_flags?(options, args)
    args.each do |option|
      options.flags.merge(options.switches).each do |_, flag|
        return false if flag.name == option.to_sym || flag.aliases&.include?(option.to_sym)
      end
    end

    true
  end
end


Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
doing-2.1.41 bin/commands/commands_accepting.rb
doing-2.1.40 bin/commands/commands_accepting.rb
doing-2.1.39 bin/commands/commands_accepting.rb
doing-2.1.38 bin/commands/commands_accepting.rb
doing-2.1.37 bin/commands/commands_accepting.rb
doing-2.1.36 bin/commands/commands_accepting.rb
doing-2.1.35 bin/commands/commands_accepting.rb
doing-2.1.34 bin/commands/commands_accepting.rb
doing-2.1.33 bin/commands/commands_accepting.rb
doing-2.1.32 bin/commands/commands_accepting.rb
doing-2.1.31pre bin/commands/commands_accepting.rb
doing-2.1.30 bin/commands/commands_accepting.rb
doing-2.1.29 bin/commands/commands_accepting.rb
doing-2.1.28 bin/commands/commands_accepting.rb
doing-2.1.27 bin/commands/commands_accepting.rb