Class: Clitopic::Command::DefaultsFile

Inherits:
Base
  • Object
show all
Defined in:
lib/clitopic/command/clito.rb

Class Method Summary collapse

Methods inherited from Base

cmd_options, fullname, load_defaults, load_options, option, register, topic, topic_options

Methods included from Parser::OptParser

#check_all_required, #check_required, #help, #parse, #parser, #process_options

Class Method Details

.callObject



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/clitopic/command/clito.rb', line 131

def call
  if @arguments.size == 0
    file = Clitopic::Helpers.find_default_file || Clitopic.default_files.first
    if file.nil?
      raise ArgumentError.new("Missing file")
    end
  else
    file = @arguments[0]
  end
  opts = dump_options(file, @options[:merge], @options[:force])
  puts opts
  return opts
end

.cmd_comment(cmd) ⇒ Object



117
118
119
120
# File 'lib/clitopic/command/clito.rb', line 117

def cmd_comment(cmd)
  "\n        # #{cmd.fullname}\n" +
  "        # #{cmd.short_description}"
end

.cmd_opts(cmd, opts) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/clitopic/command/clito.rb', line 36

def cmd_opts(cmd, opts)
  if cmd.cmd_options.size > 0 && (!cmd.hidden || options[:hidden])
    cmd_comment = "#{cmd.name}$comment"
    opts[cmd_comment] = {}
    opts = opts[cmd_comment]
    opts[cmd.name] = {"options" => {}, "args" =>  []}
    cmd.cmd_options.each do |opt|
      opts[cmd.name]["options"]["#{opt[:name].to_s}$comment"] = {}
      opts[cmd.name]["options"]["#{opt[:name].to_s}$comment"][opt[:name].to_s] = opt[:default]
    end
  end
end

.deep_merge(h1, h2) ⇒ Object



49
50
51
52
# File 'lib/clitopic/command/clito.rb', line 49

def deep_merge(h1, h2)
  merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
  h1.merge(h2, &merger)
end

.dump_options(file, merge = true, force = false) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/clitopic/command/clito.rb', line 54

def dump_options(file, merge=true, force=false)
  opts = {"common_options" => {}}
  opts["common_options"] = {} if Clitopic::Commands.global_options.size > 0
  Clitopic::Commands.global_options.each do |opt|
    opts["common_options"][opt[:name].to_s] = opt[:default]
  end
  opts["commands"] = {} if Clitopic::Commands.global_commands.size > 0
  Clitopic::Commands.global_commands.each do |c, cmd|
    cmd_opts(cmd, opts["commands"])
  end
  opts['topics'] = {}
  Clitopic::Topics.topics.each do |topic_name, topic|
    if @options[:topics] && !@options[:topics].index(topic_name.to_s)
      next
    end

    if (!topic.hidden || options[:hidden])
      topic_comment = "#{topic_name}$comment"
      opts['topics'][topic_comment] = {}
      opts['topics'][topic_comment][topic_name] = {}
      opts['topics'][topic_comment][topic_name]["topic_options$comment"] = {"topic_options" => {}} if topic.topic_options.size > 0
      topic.topic_options.each do |opt|
        opts['topics'][topic_comment][topic_name]["topic_options$comment"]["topic_options"][opt[:name].to_s] = opt[:default]
      end
      if topic.commands.size > 0
        topic.commands.each do |c, cmd|
          cmd_opts(cmd, opts['topics'][topic_comment][topic_name])
        end
      end
    end
  end

  if File.exist?(file)
    if merge == false && force == false
      raise ArgumentError.new("File #{file} exists, use --merge or --force")
    end
    if merge && !force
      opts = deep_merge(opts, YAML.load_file(file))
    end
  end
  puts "write: #{file}"
  yaml = opts.to_yaml
  yaml = yaml.gsub("topic_options$comment:", "  # common-topic options")
  Clitopic::Topics.topics.each do |topic_name, topic|
    yaml = yaml.gsub(/(\s+)#{topic_name}\$comment:/, '\1' + topic_comment(topic))
    if topic.commands.size > 0
      topic.commands.each do |c, cmd|
        yaml = yaml.gsub(/(\s+)#{c}\$comment:/, '\1' + cmd_comment(cmd))
        yaml = opt_comment(cmd, yaml)
      end
    end
  end
  File.open(file, 'wb') do  |file|
    file.write(yaml)
  end
  return opts
end

.opt_comment(cmd, yaml) ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/clitopic/command/clito.rb', line 122

def opt_comment(cmd, yaml)
  if cmd.cmd_options.size > 0 && (!cmd.hidden || options[:hidden])
    cmd.cmd_options.each do |opt|
      yaml = yaml.gsub(/(\s+)#{opt[:name].to_s}\$comment:/, '\1' +  "# #{opt[:args].last}")
    end
  end
  return yaml
end

.topic_comment(topic) ⇒ Object



112
113
114
115
# File 'lib/clitopic/command/clito.rb', line 112

def topic_comment(topic)
  "\n    # Topic: #{topic.name}\n" +
  "    # #{topic.description.gsub("\n", "\n    # ")}"
end