Class: Grouik::Cli
- Inherits:
-
Object
- Object
- Grouik::Cli
- 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
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#argv ⇒ Object
readonly
Returns the value of attribute argv.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
-
.defaults ⇒ Object
default options.
-
.program_name ⇒ String
Program name.
-
.run(argv = ARGV) ⇒ Object
Run.
Instance Method Summary collapse
-
#initialize(argv = ARGV) ⇒ Cli
constructor
Constructor.
-
#parse! ⇒ self
Parse command line options.
-
#parser ⇒ OptionParser
Provide an
OptionParser
. -
#processables ⇒ Array<OpenStruct>
Get processable items (based on command arguments), used during execution.
- #program_name ⇒ String
-
#run ⇒ Fixnum
Execute CLI and return exit code.
Methods included from Grouik::Concerns::Helpable
Constructor Details
#initialize(argv = ARGV) ⇒ Cli
Constructor
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
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
23 24 25 |
# File 'src/lib/grouik/cli.rb', line 23 def arguments @arguments end |
#argv ⇒ Object (readonly)
Returns the value of attribute argv.
21 22 23 |
# File 'src/lib/grouik/cli.rb', line 21 def argv @argv end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
22 23 24 |
# File 'src/lib/grouik/cli.rb', line 22 def @options end |
Class Method Details
.defaults ⇒ Object
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_name ⇒ String
Program name
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
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
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 |
#parser ⇒ OptionParser
Provide an 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. = 'Usage: %s [OPTION]... [FILE]...' % program_name parser end |
#processables ⇒ Array<OpenStruct>
Get processable items (based on command arguments), used during execution
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: , '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_name ⇒ String
57 58 59 |
# File 'src/lib/grouik/cli.rb', line 57 def program_name self.class.program_name end |
#run ⇒ Fixnum
Execute CLI and return exit code
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 [:version] STDOUT.puts helpers.get(:cli).version_chapter return 0 end processables.each do |processable| Dir.chdir(processable.path) do process(processable.) end end 0 end |