Class: Kut::LibraryGeneratorCommand
- Inherits:
-
Object
- Object
- Kut::LibraryGeneratorCommand
- Defined in:
- lib/kut/commands/lib-gen-cmd.rb
Instance Attribute Summary collapse
-
#generators ⇒ Object
Returns the value of attribute generators.
-
#help_banner ⇒ Object
readonly
Returns the value of attribute help_banner.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #add_default_generators ⇒ Object
- #find_generator(name) ⇒ Object
- #help(args) ⇒ Object
-
#initialize ⇒ LibraryGeneratorCommand
constructor
A new instance of LibraryGeneratorCommand.
- #run(args) ⇒ Object
Constructor Details
#initialize ⇒ LibraryGeneratorCommand
Returns a new instance of LibraryGeneratorCommand.
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 40 41 42 43 44 |
# File 'lib/kut/commands/lib-gen-cmd.rb', line 11 def initialize @name = 'gen-lib' @help_banner = 'Generate schema library' @generators = [] @options = OpenStruct.new @options.in_file_name = '-' @options.out_file_name = '-' @options.generator_name = 'default' @option_parser = OptionParser.new do |opts| opts. = "Usage: kut #{self.name} [options]" opts.on("-G", "--generator GENERATOR_NAME", "Select generator") do |gen_name| @options.generator_name = gen_name end opts.on("-i", "--input INPUT_FILE", "Innput pins file. if - to use stdin.") do |file_name| @options.in_file_name = file_name end opts.on("-o", "--output OUTPUT_LIBRARY_FILE", "Output libary file. if - to use stdout.") do |file_name| @options.out_file_name = file_name end opts.on("--list", "List of avalaible generators") do $stderr << "Avalaible generators:\n" generators.each { |gen| $stderr << "#{' ' * 3}#{gen.name}\n" } exit end end add_default_generators end |
Instance Attribute Details
#generators ⇒ Object
Returns the value of attribute generators.
9 10 11 |
# File 'lib/kut/commands/lib-gen-cmd.rb', line 9 def generators @generators end |
#help_banner ⇒ Object (readonly)
Returns the value of attribute help_banner.
8 9 10 |
# File 'lib/kut/commands/lib-gen-cmd.rb', line 8 def @help_banner end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/kut/commands/lib-gen-cmd.rb', line 7 def name @name end |
Instance Method Details
#add_default_generators ⇒ Object
46 47 48 49 50 |
# File 'lib/kut/commands/lib-gen-cmd.rb', line 46 def add_default_generators generators << Kut::Library::DefaultGenerator.new generators << Kut::Library::SimpleGenerator.new generators << Kut::Library::GOSTConnGenerator.new end |
#find_generator(name) ⇒ Object
52 53 54 55 |
# File 'lib/kut/commands/lib-gen-cmd.rb', line 52 def find_generator(name) generators.each { |g| return g if g.name.to_s() == name } nil end |
#help(args) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/kut/commands/lib-gen-cmd.rb', line 57 def help(args) #args_cp = args.dup() @option_parser.parse!(args) gen = find_generator(@options.generator_name) gen_help = '' gen_help = gen.prepare(args).to_s() if gen @option_parser.to_s() + gen_help end |
#run(args) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/kut/commands/lib-gen-cmd.rb', line 66 def run(args) args_cp = args.dup begin @option_parser.parse!(args) rescue OptionParser::InvalidOption => e args = [(args_cp - args)[-1]] + args end gen = find_generator(@options.generator_name) $stderr << "Generator '#{@options.generator_name}' not found.\n" unless gen exit unless gen f_in = $stdin f_in = File.new(@options.in_file_name) if @options.in_file_name && @options.in_file_name != '-' f_out = $stdout f_out = File.new(@options.out_file_name, 'w') if @options.out_file_name && @options.out_file_name != '-' f_out << "EESchema-LIBRARY Version 2.0 24/1/1997-18:9:6\n" gen.prepare(args) f_out << gen.generate(f_in, f_out) f_out << "#EndLibrary\n" end |