kerplutz 0.1.0 → 0.1.1

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.
@@ -0,0 +1,5 @@
1
+ == (0.1.1)
2
+ * Favor commands when parsing ambiguous flag arguments (Mike Sassak)
3
+
4
+ == (0.1.0)
5
+ * Initial release (Mike Sassak)
data/README.md CHANGED
@@ -11,8 +11,14 @@ generally a pretty slick library, even if its interface is a bit wonky.
11
11
  It seemed like a shame to let it go to waste, when getting from it what
12
12
  I needed requires only a small amount of code.
13
13
 
14
+ ## Usage
15
+
16
+ Real documentation will be forthcoming, but for now see
17
+ `features/kerplutz.feature`.
18
+
14
19
  ## Todo
15
20
 
21
+ * README docs for builder and option types
16
22
  * Read defaults from a config file
17
23
  * Argument type-casts
18
24
  * List arguments
data/Rakefile CHANGED
@@ -8,3 +8,7 @@ end
8
8
  task :spec do
9
9
  sh "rspec spec"
10
10
  end
11
+
12
+ task :focus do
13
+ sh "rspec -t focus spec"
14
+ end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'kerplutz'
3
- s.version = '0.1.0'
3
+ s.version = '0.1.1'
4
4
  s.authors = ["Mike Sassak"]
5
5
  s.description = "Command-line option parser with subcommands that won't leave you feeling Kerplutz"
6
6
  s.summary = "kerplutz #{s.version}"
@@ -18,6 +18,10 @@ module Kerplutz
18
18
  commands[display_name]
19
19
  end
20
20
 
21
+ def has_command?(display_name)
22
+ self.===(display_name)
23
+ end
24
+
21
25
  def ===(display_name)
22
26
  commands.has_key?(display_name)
23
27
  end
@@ -23,26 +23,32 @@ module Kerplutz
23
23
  top.add_option(option, false)
24
24
  end
25
25
 
26
+ # TODO: Extract this method into separate class that gives all
27
+ # the involved parties the chance to parse all of the args
26
28
  def parse(args)
27
- first, *rest = args
28
- remainder = []
29
-
30
- case first
31
-
32
- when help
33
- puts (rest.empty? ? banner : commands[rest.first].help)
34
-
35
- when option
36
- remainder = top.parse(args)
37
-
38
- when commands
39
- remainder = commands[first].parse(rest)
40
-
29
+ if args[0] =~ /^(--help|help)$/
30
+ first, *rest = args
31
+ if rest.empty?
32
+ puts banner
33
+ else
34
+ puts commands[rest.first].help
35
+ end
36
+ elsif cmd = args.find { |el| commands.has_command?(el) }
37
+ cmd_idx = args.index(cmd)
38
+ top_args, cmd_args = args.partition.with_index do |_arg, idx|
39
+ idx < cmd_idx
40
+ end
41
+ top.parse(top_args)
42
+ remainder = commands[cmd].parse(cmd_args[1..-1])
41
43
  else
42
- puts banner
44
+ if args.empty?
45
+ puts banner
46
+ else
47
+ remainder = top.parse(args)
48
+ end
43
49
  end
44
50
 
45
- [arguments, remainder]
51
+ [arguments, remainder || []]
46
52
  end
47
53
 
48
54
  def banner
@@ -51,15 +57,5 @@ module Kerplutz
51
57
  help << " Commands:\n" << commands.summary << "\n"
52
58
  help << "Type '#{name} help COMMAND' for help with a specific command.\n"
53
59
  end
54
-
55
- private
56
-
57
- def help
58
- /^(--help|help)$/
59
- end
60
-
61
- def option
62
- /^(--|-)[\w-]+$/
63
- end
64
60
  end
65
61
  end
@@ -2,12 +2,39 @@ require 'spec_helper'
2
2
 
3
3
  module Kerplutz
4
4
  describe Executable do
5
- subject { Executable.new("test") }
5
+ let(:option) { Flag.new(:foo, '', :bar) }
6
+ let(:command) { Command.new(:foo, '') }
7
+
8
+ subject do
9
+ exec = Executable.new("test")
10
+ exec.add_option(option)
11
+ exec.add_command(command)
12
+ exec
13
+ end
6
14
 
7
15
  describe "#parse" do
8
16
  it "extracts the options from the arguments" do
9
- subject.add_option(Switch.new(:foo, ''))
10
17
  subject.parse(["--foo"]).should == [{ :foo => true }, []]
18
+ subject.parse(["--foo", "bar"]).should == [{ :foo => "bar" }, []]
19
+ end
20
+
21
+ it "parses an unambiguous flag argument" do
22
+ subject.top.should_receive(:parse).with(["--foo", "bar"])
23
+ subject.parse(["--foo", "bar"])
24
+ end
25
+
26
+ it "parses an ambiguous flag argument" do
27
+ subject.top.should_receive(:parse).with(["--foo"])
28
+ command.should_receive(:parse).with([])
29
+ subject.parse(["--foo", "foo"])
30
+
31
+ subject.top.should_receive(:parse).with(["--foo"])
32
+ command.should_receive(:parse).with(["-x"])
33
+ subject.parse(["--foo", "foo", "-x"])
34
+
35
+ subject.top.should_receive(:parse).with(["--foo"])
36
+ command.should_receive(:parse).with(["foo", "-x"])
37
+ subject.parse(["--foo", "foo", "foo", "-x"])
11
38
  end
12
39
  end
13
40
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Mike Sassak
@@ -71,6 +71,7 @@ files:
71
71
  - .rvmrc
72
72
  - Gemfile
73
73
  - Gemfile.lock
74
+ - History.txt
74
75
  - LICENSE
75
76
  - README.md
76
77
  - Rakefile
@@ -120,7 +121,7 @@ rubyforge_project:
120
121
  rubygems_version: 1.3.7
121
122
  signing_key:
122
123
  specification_version: 3
123
- summary: kerplutz 0.1.0
124
+ summary: kerplutz 0.1.1
124
125
  test_files:
125
126
  - features/kerplutz.feature
126
127
  - features/step_definitions/kerplutz_steps.rb