Class: Clitopic::Command::DefaultsFile
- Inherits:
-
Base
- Object
- Base
- Clitopic::Command::DefaultsFile
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
#check_all_required, #check_required, #help, #parse, #parser, #process_options
Class Method Details
.call ⇒ Object
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/clitopic/command/clito.rb', line 121
def call
puts @options
if @arguments.size == 0
file = Clitopic::Helpers.find_default_file
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
|
113
114
115
116
|
# File 'lib/clitopic/command/clito.rb', line 113
def (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
|
# File 'lib/clitopic/command/clito.rb', line 36
def cmd_opts(cmd, opts)
if cmd.cmd_options.size > 0 && (!cmd.hidden || options[:hidden])
= "#{cmd.name}$comment"
opts[] = {}
opts = opts[]
opts[cmd.name] = {"options" => {}, "args" => []}
cmd.cmd_options.each do |opt|
opts[cmd.name]["options"][opt[:name].to_s] = opt[:default]
end
end
end
|
.deep_merge(h1, h2) ⇒ Object
48
49
50
51
|
# File 'lib/clitopic/command/clito.rb', line 48
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
53
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
|
# File 'lib/clitopic/command/clito.rb', line 53
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 (!topic.hidden || options[:hidden])
= "#{topic_name}$comment"
opts['topics'][] = {}
opts['topics'][][topic_name] = {}
opts['topics'][][topic_name]["topic_options"] = {} if topic.topic_options.size > 0
topic.topic_options.each do |opt|
opts['topics'][][topic_name]["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_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
Clitopic::Topics.topics.each do |topic_name, topic|
yaml = yaml.gsub("#{topic_name}$comment:", (topic))
if topic.commands.size > 0
topic.commands.each do |c, cmd|
yaml = yaml.gsub("#{c}$comment:", (cmd))
end
end
end
File.open(file, 'wb') do |file|
file.write(yaml)
end
return opts
end
|
118
119
120
|
# File 'lib/clitopic/command/clito.rb', line 118
def (opt)
"# "
end
|
108
109
110
111
|
# File 'lib/clitopic/command/clito.rb', line 108
def (topic)
"\n # Topic: #{topic.name}\n" +
" # #{topic.description.gsub("\n", "\n # ")}"
end
|