Class: CharsAt::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/charsat/cli.rb

Instance Method Summary collapse

Instance Method Details



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/charsat/cli.rb', line 7

def print(args)
  command = Cri::Command.define do
    name        'charsat'
    usage       "echo 'test' | charsat 1 2 3 # outputs t, e and s"
    summary     "Prints the n'th characters using 1-based indexing from STDIN"

    skip_option_parsing

    flag   :h,  :help,  'show help for this command' do |value, cmd|
      puts cmd.help
      exit 0
    end

    run do |opts, args, cmd|

      if args.empty? || args.first == "--help" || args.first == "-h"
        puts cmd.help
        exit 1
      end

      string = (STDIN.tty?) ? STDIN.getpass("Enter string: ") : $stdin.read
      indices = args.map(&:to_i).select { |i| i <= string.length }

      if indices.empty?
        puts cmd.help
        exit 1
      end

      puts Model.new(string, indices).parsed.join(', ')
    end
  end
  command.run(args)
end