Class: Grouik::Cli

Inherits:
Object
  • Object
show all
Includes:
Grouik::Concerns::Helpable
Defined in:
src/lib/grouik/cli.rb

Overview

Grouik command line interface

Provides a ready to use program, based on Grouik library

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Grouik::Concerns::Helpable

#helpers

Constructor Details

#initialize(argv = ARGV) ⇒ Cli

Constructor

Parameters:

  • argv (Array) (defaults to: ARGV)


64
65
66
67
68
69
# File 'src/lib/grouik/cli.rb', line 64

def initialize(argv = ARGV)
  @argv = argv.clone
  @options = self.class.defaults
  # @options = config unless config.empty?
  @arguments = []
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



23
24
25
# File 'src/lib/grouik/cli.rb', line 23

def arguments
  @arguments
end

#argvObject (readonly)

Returns the value of attribute argv.



21
22
23
# File 'src/lib/grouik/cli.rb', line 21

def argv
  @argv
end

#optionsObject (readonly)

Returns the value of attribute options.



22
23
24
# File 'src/lib/grouik/cli.rb', line 22

def options
  @options
end

Class Method Details

.defaultsObject

default options



43
44
45
46
47
48
49
50
51
52
53
# File 'src/lib/grouik/cli.rb', line 43

def defaults
  {
    stats: true,
    paths: ['.'],
    basedir: '.',
    output: STDOUT,
    ignores: [],
    require: nil,
    template: nil,
  }
end

.program_nameString

Program name

Returns:

  • (String)


31
32
33
# File 'src/lib/grouik/cli.rb', line 31

def program_name
  Pathname.new($PROGRAM_NAME).basename('.rb').to_s
end

.run(argv = ARGV) ⇒ Object

Run

Parameters:

  • argv (Array) (defaults to: ARGV)


38
39
40
# File 'src/lib/grouik/cli.rb', line 38

def run(argv = ARGV)
  self.new(argv).run
end

Instance Method Details

#parse!self

Parse command line options

Abort process (error code SHOULD BE 22) on invalid option

Returns:

  • (self)


86
87
88
89
90
91
92
93
94
95
96
97
# File 'src/lib/grouik/cli.rb', line 86

def parse!
  argv = self.argv.clone
  begin
    parser.parse!(argv)
  rescue OptionParser::InvalidOption
    STDERR.puts(parser)
    exit(Errno::EINVAL::Errno)
  end
  @arguments = argv
  # @options = prepare_options(@options)
  self
end

#parserOptionParser

Provide an OptionParser

Returns:

  • (OptionParser)


74
75
76
77
78
79
# File 'src/lib/grouik/cli.rb', line 74

def parser
  parser = helpers.get(:cli).make_parser(@options)
  parser.banner = 'Usage: %s [OPTION]... [FILE]...' % program_name

  parser
end

#processablesArray<OpenStruct>

Get processable items (based on command arguments), used during execution

Returns:

  • (Array<OpenStruct>)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'src/lib/grouik/cli.rb', line 102

def processables
  processables = []
  if arguments.empty?
    processables[0] = OpenStruct.new(
      path: Pathname.new(Dir.pwd),
      options: options,
      'file?' => false
    )
  else
    arguments.each do |filepath|
      config = helpers.get(:cli).read_config(filepath)

      processables << OpenStruct.new(
        path: Pathname.new(filepath).dirname,
        file: Pathname.new(filepath),
        options: config,
        'file?' => true
      )
    end
  end

  processables.map(&:freeze)
end

#program_nameString

Returns:

  • (String)


57
58
59
# File 'src/lib/grouik/cli.rb', line 57

def program_name
  self.class.program_name
end

#runFixnum

Execute CLI and return exit code

Returns:

  • (Fixnum)


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'src/lib/grouik/cli.rb', line 129

def run
  parse!

  if options[:version]
    STDOUT.puts helpers.get(:cli).version_chapter
    return 0
  end

  processables.each do |processable|
    Dir.chdir(processable.path) do
      process(processable.options)
    end
  end
  0
end