clian 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4aafa95a344936cada12ed9d9a235a8dd7cae688
4
- data.tar.gz: d03183a7f68c26aecb8b1293d17de893ee8beed6
3
+ metadata.gz: f3a5822d473285caced8c7692fafdd8e2000f698
4
+ data.tar.gz: 5a2eece2cdfe9a96506dbdaa53c52fd501bb5e41
5
5
  SHA512:
6
- metadata.gz: fb12e871477882eab8efd453a395fe5ba4018dd41495a954ac57052565e34a58642140de1e32ca19d3e38e137eb4b6ae286ccb0e25281170a3ce14ca56fdc712
7
- data.tar.gz: 6353f628553f9dac6f66c4296145abd73e70e2b3fdc103498d86ee3bb4fbf03e18b5855936fb6c55fe6aba99c338c98619b94d35e99dec95d549114eb28b2acc
6
+ metadata.gz: 232bb883c01d4196a39aa3e2936243a6f143497a2773b3374b828d0123918b4575e309b0159217e2f8803897d17d6ac94ba075cad0b73baf0b7e9ed707b11b40
7
+ data.tar.gz: 66a18bd7bfc48e147368df8f5bb1c10bdce11f62ebb1b7d5e89f8d35c73793c0ec669b88d54e89bdfe5f439ba50ef25d49a91d64841a66f3047d71466bbe182a
@@ -7,6 +7,7 @@ module Clian
7
7
 
8
8
  autoload :Authorizer, "#{dir}/authorizer.rb"
9
9
  autoload :Cli, "#{dir}/cli.rb"
10
+ autoload :Command, "#{dir}/command.rb"
10
11
  autoload :Config, "#{dir}/config.rb"
11
12
  autoload :VERSION, "#{dir}/version.rb"
12
13
  end
@@ -114,7 +114,10 @@ module Clian
114
114
  def completions(*command)
115
115
  help = self.class.commands
116
116
  global_options = self.class.class_options
117
- Clian::Command::Completions.new(help, global_options, command, config)
117
+
118
+ Clian::Command::Completions.new(help, global_options, command) do |banner|
119
+ custom_completion_for_banner(banner)
120
+ end
118
121
  end
119
122
 
120
123
  ################################################################
@@ -143,6 +146,10 @@ module Clian
143
146
 
144
147
  private
145
148
 
149
+ def custom_completion_for_banner(banner)
150
+ return nil
151
+ end
152
+
146
153
  def exit_on_error(&block)
147
154
  begin
148
155
  yield if block_given?
@@ -0,0 +1,9 @@
1
+ module Clian
2
+ module Command
3
+
4
+ dir = File.dirname(__FILE__) + "/command"
5
+
6
+ autoload :Completions, "#{dir}/completions.rb"
7
+
8
+ end # module Command
9
+ end # module Clian
@@ -0,0 +1,103 @@
1
+ module Clian
2
+ module Command
3
+ class Completions
4
+
5
+ def initialize(help, global_options, arguments, &custom_completion_proc)
6
+ @help, @global_options, @arguments = help, global_options, arguments
7
+ @custom_completion_proc = custom_completion_proc
8
+ command_name = arguments.find {|arg| arg !~ /^-/}
9
+
10
+ print completion_header
11
+ print possible_subcommands(help)
12
+
13
+ if command_name and help[command_name]
14
+ print arguments_spec(help[command_name])
15
+ print options_spec(help[command_name].options)
16
+ end
17
+ print options_spec(global_options)
18
+ end
19
+
20
+ private
21
+
22
+ def completion_header
23
+ "_arguments\n"
24
+ end
25
+
26
+ def possible_subcommands(help, position = 1)
27
+ str = "#{position}:Possible commands\\::(("
28
+ help.each_value do |cmd|
29
+ next if cmd.name == "completions"
30
+ str << " #{cmd.name}\\:"
31
+ str << cmd.description.gsub(/([()\s"';&|#\\])/, '\\\\\1')
32
+ end
33
+ str << "))\n"
34
+ end
35
+
36
+ def arguments_spec(command, position = 2)
37
+ str = ""
38
+ command.usage.split(/\s+/)[1..-1].each do |arg|
39
+ pos = position
40
+ optional = ""
41
+
42
+ if /^\[(.*)\]/ =~ arg
43
+ arg = $1
44
+ optional = ":"
45
+ end
46
+
47
+ multi = ""
48
+ if /(.*)\.\.\.$/ =~ arg
49
+ arg = $1
50
+ pos = "*"
51
+ multi = ":"
52
+ end
53
+
54
+ str << "#{pos}:#{optional}#{arg}\\::#{possible_values(arg)}#{multi}\n"
55
+ position += 1
56
+ end
57
+ return str
58
+ end
59
+
60
+ def options_spec(options)
61
+ str = ""
62
+
63
+ options.each do |name, opt|
64
+ name = name.to_s.gsub("_", "-")
65
+
66
+ if opt.type == :boolean
67
+ str << "(--#{name})--#{name}[#{opt.description}]\n"
68
+ else
69
+ str << "(--#{name})--#{name}=-[#{opt.description}]:#{opt.banner}:#{possible_values_for_opt(opt)}\n"
70
+ end
71
+ end
72
+ return str
73
+ end
74
+
75
+ def possible_values(banner)
76
+ if @custom_completion_proc
77
+ @custom_completion_proc.call(banner)
78
+ end || default_possible_values(banner)
79
+ end
80
+
81
+ def possible_values_for_opt(option)
82
+ return "(" + option.enum.join(" ") + ")" if option.enum
83
+ return possible_values(option.banner)
84
+ end
85
+
86
+ def default_possible_values(banner)
87
+ case banner
88
+ when /^(FILE|CONF)/
89
+ "_files"
90
+ when /^DIR/
91
+ "_files -/"
92
+ when "COMMAND"
93
+ possible_commands
94
+ when /^NUM/
95
+ "_guard '[0-9]#' 'Number'"
96
+ else
97
+ "{_message '#{banner} is required'}"
98
+ end
99
+ end
100
+
101
+ end # class Completions
102
+ end # module Command
103
+ end # module Clian
@@ -1,3 +1,3 @@
1
1
  module Clian
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clian
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoshinari Nomura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-09-25 00:00:00.000000000 Z
11
+ date: 2018-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -141,6 +141,8 @@ files:
141
141
  - lib/clian.rb
142
142
  - lib/clian/authorizer.rb
143
143
  - lib/clian/cli.rb
144
+ - lib/clian/command.rb
145
+ - lib/clian/command/completions.rb
144
146
  - lib/clian/config.rb
145
147
  - lib/clian/version.rb
146
148
  homepage: https://github.com/yoshinari-nomura/clian
@@ -163,7 +165,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
163
165
  version: '0'
164
166
  requirements: []
165
167
  rubyforge_project:
166
- rubygems_version: 2.6.11
168
+ rubygems_version: 2.6.13
167
169
  signing_key:
168
170
  specification_version: 4
169
171
  summary: Small set of Ruby classes helpful for creation of CLI tools.