moister 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b02c0dfdcf4ec83c6750efcb5589a09173de2293
4
- data.tar.gz: 887ee4e75b941f2eefade7f92bab78c11164c623
3
+ metadata.gz: 7f34abd6542354861f7906c23a802a75825f360e
4
+ data.tar.gz: 0a2ed87afece35b57db47b54a672babdbb74fe33
5
5
  SHA512:
6
- metadata.gz: dc1da69c1ac94657a716d7f5c36c63f34f8b06a5eaae1c0780a98c9313c6e89b626fe8d4e2dd05a4ae442137f4d8461c44979fa8325a506a8b04d07d6cbe0259
7
- data.tar.gz: a29a1316c3f16077b66e199f66d45efc3c0fafc53c63c97cc85cc011d87aaebb74657012dd0745cc4d8641fc16c9cd1169b87ab04b4d643bd30849a65388ae3b
6
+ metadata.gz: 71dfa2354c457163c5059e95aab6d86c72f683a2d3ae038562748303f122c418742fa7a53f825b30d8d7126627803c942fae4b3b2e250bbf9b8760247784b68a
7
+ data.tar.gz: cc489bf49fa04bcb378c97e0943da37a8a4f296e49f34d0d30b02578b839f772fde371f92ff24fd997ded7fc7e9bd9fe0f5a38e0cb84930c1f19d01b79d2f761
data/lib/moister.rb CHANGED
@@ -28,11 +28,17 @@ module Moister
28
28
  # options applicable to all subcommands
29
29
  @for_all = []
30
30
  @subcommands = {}
31
+ @aliases = {}
31
32
  super
32
33
  end
33
34
 
34
35
  def subcommand name, banner, &block
35
- @subcommands[name] = { name: name, banner: banner, parse_cmdline: block }
36
+ name, *positionals = name.split ' '
37
+ name, *aliases = name.split(',')
38
+ subcmd = { name: name, banner: banner, parse_cmdline: block }
39
+ subcmd[:positionals] = positionals unless positionals.empty?
40
+ @subcommands[name] = subcmd
41
+ aliases.each { |_alias| @aliases[_alias] = name }
36
42
  end
37
43
 
38
44
  # add a block to configure every subcommand
@@ -42,10 +48,16 @@ module Moister
42
48
 
43
49
  def to_s
44
50
  ret = super
45
- max_len = @subcommands.values.map { |subcmd| subcmd[:name].length }.max
46
- ret += "\ncommands:\n"
47
- @subcommands.values.each do |subcmd|
51
+ prefixes = @subcommands.values.map do |subcmd|
48
52
  prefix = subcmd[:name]
53
+ prefix += ' ' + subcmd[:positionals].join(' ') if subcmd.has_key? :positionals
54
+ prefix
55
+ end
56
+ max_len = prefixes.map(&:length).max
57
+
58
+ ret += "\ncommands:\n"
59
+ @subcommands.values.each_with_index do |subcmd, idx|
60
+ prefix = prefixes[idx]
49
61
  prefix += ' ' * (max_len - prefix.length + 2)
50
62
  ret += " #{prefix} #{subcmd[:banner]}\n"
51
63
  end
@@ -61,17 +73,56 @@ module Moister
61
73
  ParseResults.new(nil, [], @config)
62
74
  else
63
75
  cmd = args.first
76
+ _alias = @aliases[cmd]
77
+ cmd = _alias if _alias
64
78
  subcmd_meta = @subcommands[cmd]
65
79
  raise "invalid subcommand: #{cmd}" unless @subcommands.has_key? cmd
66
80
  args.shift
67
81
 
68
- positionals = OptionParserExtra.new(@config) do |subop|
82
+ subcmd_config = @config[cmd] = {}
83
+ positionals = OptionParserExtra.new(@config[cmd]) do |subop|
69
84
  apply_for_all subop
70
85
  subop.banner = subcmd_meta[:banner]
71
86
  parse_cmdline = subcmd_meta[:parse_cmdline]
72
87
  parse_cmdline.call(subop) if parse_cmdline
73
88
  end.order! args
74
89
 
90
+ positionals_meta = subcmd_meta[:positionals]
91
+ if positionals_meta
92
+ positionals_meta.each do |positional_meta|
93
+ array_match = false
94
+ optional = if positional_meta =~ /^\[.+\]$/
95
+ optional = true
96
+ positional_meta = positional_meta[1..-2]
97
+ end
98
+
99
+ positional_name = if positional_meta =~ /^\*[a-z\-]+$/
100
+ array_match = true
101
+ positional_meta[1..-1]
102
+ else
103
+ positional_meta
104
+ end
105
+
106
+ if array_match
107
+ if positionals.empty?
108
+ if optional
109
+ subcmd_config[positional_name] = []
110
+ next
111
+ end
112
+ raise "`#{cmd}' subcommand requires at least one `#{positional_name}' parameter"
113
+ end
114
+ subcmd_config[positional_name] = positionals
115
+ positionals = []
116
+ else
117
+ if positionals.empty?
118
+ next if optional
119
+ raise "`#{cmd}' subcommand requires `#{positional_name}' parameter"
120
+ end
121
+ subcmd_config[positional_name] = positionals.shift
122
+ end
123
+ end
124
+ end
125
+
75
126
  ParseResults.new(cmd, positionals, @config)
76
127
  end
77
128
  end
@@ -1,3 +1,3 @@
1
1
  module Moister
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moister
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Pike
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-01-09 00:00:00.000000000 Z
11
+ date: 2016-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -91,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
91
  version: '0'
92
92
  requirements: []
93
93
  rubyforge_project:
94
- rubygems_version: 2.4.5.1
94
+ rubygems_version: 2.5.1
95
95
  signing_key:
96
96
  specification_version: 4
97
97
  summary: An OptionParser wrapper that supports subcommands and shorter syntax.