prompt 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md ADDED
@@ -0,0 +1,23 @@
1
+ # Changelog
2
+
3
+ This is updated when a new version is pushed to http://rubygems.org
4
+
5
+ ## 0.0.3 (Feb 22, 2011)
6
+
7
+ * DSL change: `variable` & `dynamic_variable` were renamed to `param`
8
+ * A glob parameter (e.g. `*param`) will now return its matches as an array of strings
9
+ * Quoted strings will now work as expected with glob parameters
10
+ * Dynamic variables are cached until the next command is run
11
+ * Added LICENSE.txt
12
+
13
+ ## 0.0.2 (Feb 17, 2011)
14
+
15
+ * Allow the command prompt to be changed while it's running
16
+ * Quoted strings will now match to a single `:param`
17
+ * Use a `*param` to match one or more words
18
+ * [BUG] Don't memoize Command#regex. This prevented dynamic variables
19
+ from working correctly after the values changed
20
+
21
+ ## 0.0.1 (Feb 16, 2011)
22
+
23
+ * Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Mu Dynamics
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -18,7 +18,7 @@ extend Prompt::DSL
18
18
 
19
19
  desc "Move"
20
20
 
21
- variable :direction, "A cardinal direction", %w(north east south west)
21
+ param :direction, "A cardinal direction", %w(north east south west)
22
22
 
23
23
  command "go :direction", "Walk in the specified direction" do |direction|
24
24
  puts "You walked #{direction} and were eaten by a grue."
@@ -39,7 +39,7 @@ Prompt::Console.start
39
39
 
40
40
  ### Tab completion
41
41
 
42
- Tab completion is hooked up automatically after you define your commands and variables
42
+ Tab completion is hooked up automatically after you define your commands and parameters
43
43
 
44
44
  $ my_app
45
45
  > g<TAB>
@@ -68,8 +68,8 @@ The `help` command is built-in. It will print all of the commands that you've d
68
68
  > help
69
69
  Console commands
70
70
 
71
- help -v
72
- help
71
+ help List all commands
72
+ help -v List all commands, including parameters
73
73
  exit
74
74
 
75
75
  Move
@@ -96,9 +96,9 @@ desc "Burger commands"
96
96
  command ...
97
97
  ```
98
98
 
99
- ## Using Variables
99
+ ## Using Parameters
100
100
 
101
- Variables can be used in a command.
101
+ Parameters can be used in a command:
102
102
 
103
103
  ```ruby
104
104
  command "name :first :last" do |first, last|
@@ -106,39 +106,39 @@ command "name :first :last" do |first, last|
106
106
  end
107
107
  ```
108
108
 
109
- Here, the variables are named `first` and `last`. Their values are be passed as arguments to the command's block, in the order in which they appear.
109
+ Here, the parameters are named `first` and `last`. Their values are be passed as arguments to the command's block, in the order in which they appear.
110
110
 
111
111
 
112
- Each `:variable` only matches a single word. If you want to match multiple words to one variable, use a `*variable`.
112
+ Each `:parameter` only matches a single word. If you want to match multiple words to one parameter, use a `*parameter`.
113
113
 
114
114
  ```ruby
115
115
  command "say *sentence" do |sentence|
116
- puts "You say '#{sentence}'"
116
+ puts "You said #{sentence.length} words"
117
117
  end
118
118
  ```
119
119
 
120
- ### Defining variables
120
+ ### Defining parameters
121
121
 
122
- It's not necessary to define a variable before using it in a command, but doing so will allow you to provide a useful description and valid values for the variable.
122
+ It's not necessary to define a parameter before using it in a command, but doing so will allow you to provide a useful description and valid values for the parameter.
123
123
 
124
124
  ```ruby
125
- variable :name, "Description"
125
+ param :name, "Description"
126
126
  ```
127
127
 
128
128
  ### Specifying a static list of valid values
129
129
 
130
- You can specify a static list of valid values for a variable. These will be expanded when using tab completion.
130
+ You can specify a static list of valid values for a parameter. These will be expanded when using tab completion.
131
131
 
132
132
  ```ruby
133
- variable :name, "Description", %w(value1 value2)
133
+ param :name, "Description", %w(value1 value2)
134
134
  ```
135
135
 
136
136
  ### Specifying a dynamic list of valid values
137
137
 
138
- Instead of specifying a static list, you can specify a block that will dynamically return a list of valid values for a variable. These will be expanded when using tab completion.
138
+ Instead of a static list, you can specify a block that will dynamically return a list of valid values for a parameter. These will also be expanded when using tab completion.
139
139
 
140
140
  ```ruby
141
- dynamic_variable :file, "JPG file" do
141
+ param :file, "JPG file" do
142
142
  Dir.glob "*.jpg"
143
143
  end
144
144
  ```
@@ -11,7 +11,7 @@ end
11
11
  @pwd = File.absolute_path Dir.pwd
12
12
  change_prompt @pwd
13
13
 
14
- dynamic_variable :dir, "Directory" do
14
+ param :dir, "Directory" do
15
15
  Dir.entries(@pwd).select do |e|
16
16
  File.directory?(File.join(@pwd, e))
17
17
  end
@@ -38,4 +38,11 @@ command "pwd", "Print current directory" do
38
38
  puts @pwd
39
39
  end
40
40
 
41
+ param :files, "A list of files"
42
+
43
+ command "cp *files :dir", "Copy one or more files to dest" do |files, dir|
44
+ puts "Copying #{files.length} file(s) to #{dir}"
45
+ # doesn't actually do it...
46
+ end
47
+
41
48
  Prompt::Console.start
data/examples/mud CHANGED
@@ -9,7 +9,7 @@ GRUE = 3
9
9
 
10
10
  desc "Move"
11
11
 
12
- variable :direction, "A cardinal direction", %w(north east south west)
12
+ param :direction, "A cardinal direction", %w(north east south west)
13
13
 
14
14
  command "go :direction", "Walk in the specified direction" do |direction|
15
15
  puts "You walked #{direction}"
@@ -31,7 +31,7 @@ command "look", "Look around" do
31
31
  end
32
32
 
33
33
  command "say *something", "Say something" do |something|
34
- puts "You say '#{something}'"
34
+ puts "You say '#{something.join(" ")}'"
35
35
  end
36
36
 
37
37
  Prompt::Console.start
@@ -15,8 +15,8 @@ module Prompt
15
15
  @current_command_group_name = desc
16
16
  end
17
17
 
18
- def define_command name, desc = nil, variables, &block
19
- current_command_group.commands << Command.new(name, desc, variables, &block)
18
+ def add_command command
19
+ current_command_group.commands << command
20
20
  end
21
21
 
22
22
  def exec command_str
@@ -25,22 +25,28 @@ module Prompt
25
25
  return command.run(args) if args
26
26
  end
27
27
  raise CommandNotFound.new(command_str)
28
+ ensure
29
+ clear_cached_values
28
30
  end
29
31
 
30
- def completions starting_with = nil
31
- return all_expansions unless starting_with
32
-
32
+ def completions starting_with
33
33
  all_expansions.grep /^#{Regexp.escape(starting_with)}/
34
34
  end
35
35
 
36
36
  private
37
37
 
38
+ def clear_cached_values
39
+ commands.each do |c|
40
+ c.clear_cached_values
41
+ end
42
+ end
43
+
38
44
  def commands
39
- @command_groups.map(&:commands).reduce [] { |a, b| a + b }
45
+ @command_groups.map(&:commands).flatten(1)
40
46
  end
41
47
 
42
48
  def all_expansions
43
- commands.map(&:expansions).flatten
49
+ commands.map(&:expansions).flatten(1)
44
50
  end
45
51
 
46
52
  def current_command_group
@@ -1,38 +1,45 @@
1
+ require 'strscan'
2
+ require 'prompt/parameter'
3
+
1
4
  module Prompt
2
5
  class Command
3
6
 
7
+ SEP = "\036" # RS - record separator
8
+ S_QUOTED_ARG = /'([^']*)'/
9
+ D_QUOTED_ARG = /"([^"]*)"/
10
+ UNQUOTED_ARG = /[^\s]+/
11
+
4
12
  attr :name
5
13
  attr :desc
14
+ attr :parameters
6
15
 
7
- def initialize(name, desc = nil, all_variables = [], &block)
8
- @name = name
16
+ def initialize(words, desc = nil, &block)
17
+ @words = words
9
18
  @desc = desc
10
- @all_variables = all_variables
11
19
  @action = block
20
+
21
+ @name = words.join(" ")
22
+ @parameters = words.select {|w| w.kind_of? Parameter}
12
23
  end
13
24
 
14
25
  def run args
15
26
  @action.call *args
16
27
  end
17
28
 
18
- def match(str)
19
- if m = regex.match(str.strip)
20
- variables.map {|v| m[v.name.to_s] }
29
+ def match str
30
+ if m = regex.match(to_args(str).join(SEP))
31
+ @parameters.map {|v| v.matches(m[v.name]) }
21
32
  end
22
33
  end
23
34
 
24
- def variables
25
- @variables ||= words.select {|w| w.kind_of? Variable}
26
- end
27
-
28
35
  def expansions
29
- expand words
36
+ expand @words
30
37
  end
31
38
 
32
39
  def usage
33
- words.map do |word|
40
+ @words.map do |word|
34
41
  case word
35
- when Variable
42
+ when Parameter
36
43
  "<#{word.name}>"
37
44
  else
38
45
  word
@@ -40,33 +47,42 @@ module Prompt
40
47
  end.join(" ")
41
48
  end
42
49
 
50
+ def clear_cached_values
51
+ @parameters.each do |p|
52
+ p.clear_cached_value if p.respond_to?(:clear_cached_value)
53
+ end
54
+ end
55
+
43
56
  private
44
57
 
58
+ # Splits a command string into an argument list.
59
+ # This understands how to make "quoted strings" into a single arg
60
+ def to_args command
61
+ args = []
62
+ ss = StringScanner.new(command)
63
+ ss.scan(/\s+/)
64
+ until ss.eos?
65
+ if ss.scan(S_QUOTED_ARG) or ss.scan(D_QUOTED_ARG)
66
+ args << ss[1]
67
+ elsif arg = ss.scan(UNQUOTED_ARG)
68
+ args << arg
69
+ end
70
+ ss.scan(/\s+/)
71
+ end
72
+ args
73
+ end
74
+
45
75
  def regex
46
76
  begin
47
- regex_strs = words.map do |word|
77
+ regex_strs = @words.map do |word|
48
78
  case word
49
- when Variable
79
+ when Parameter
50
80
  word.regex
51
81
  else
52
82
  Regexp.escape(word)
53
83
  end
54
84
  end
55
- Regexp.new("^#{regex_strs.join("\s+")}$")
56
- end
57
- end
58
-
59
- def words
60
- @words ||= @name.split(/\s/).map do |word|
61
- if word[0] == ":"
62
- sym = word[1..-1].to_sym
63
- @all_variables.find {|v| v.name == sym} || Variable.new(sym, sym.to_s)
64
- elsif word[0] == "*"
65
- sym = word[1..-1].to_sym
66
- @all_variables.find {|v| v.name == sym} || GlobVariable.new(sym, sym.to_s)
67
- else
68
- word
69
- end
85
+ Regexp.new("^#{regex_strs.join(SEP)}$")
70
86
  end
71
87
  end
72
88
 
@@ -77,7 +93,7 @@ module Prompt
77
93
  tail = a[1..-1]
78
94
 
79
95
  case head
80
- when Variable
96
+ when Parameter
81
97
  head = head.expansions
82
98
  else
83
99
  head = [head]
@@ -8,12 +8,12 @@ module Prompt
8
8
 
9
9
  desc "Console commands"
10
10
 
11
- command "help -v" do
12
- print_help true
11
+ command "help", "List all commands" do
12
+ print_help
13
13
  end
14
14
 
15
- command "help" do
16
- print_help
15
+ command "help -v", "List all commands, including parameters" do
16
+ print_help true
17
17
  end
18
18
 
19
19
  command "exit" do
@@ -30,7 +30,7 @@ module Prompt
30
30
  cg.commands.each do |cmd|
31
31
  puts " %-40s %s" % [cmd.usage, cmd.desc]
32
32
  if verbose
33
- cmd.variables.each do |v|
33
+ cmd.parameters.each do |v|
34
34
  puts " "*43 + ("%-10s %s" % ["<#{v.name}>", "#{v.desc}"])
35
35
  end
36
36
  end
data/lib/prompt/dsl.rb CHANGED
@@ -1,6 +1,7 @@
1
- require 'prompt/variable'
2
- require 'prompt/proc_variable'
3
- require 'prompt/glob_variable'
1
+ require 'prompt/parameter'
2
+ require 'prompt/proc_parameter'
3
+ require 'prompt/glob_parameter'
4
+ require 'prompt/dsl_helper'
4
5
 
5
6
  module Prompt
6
7
  module DSL
@@ -19,19 +20,19 @@ module Prompt
19
20
  end
20
21
 
21
22
  def command(name, desc = nil, &block)
22
- Prompt.application.define_command(name, desc, @variables || {}, &block)
23
+ words = DSLHelper.words(name, @parameters || [])
24
+ command = Command.new(words, desc, &block)
25
+ Prompt.application.add_command(command)
23
26
  end
24
27
 
25
- def variable(name, desc, values = nil)
26
- @variables = [] unless defined? @variables
27
- raise "variable :#{name} is already defined" if @variables.find {|v| v.name == name}
28
- @variables << Variable.new(name, desc, values)
29
- end
30
-
31
- def dynamic_variable(name, desc, &block)
32
- @variables = [] unless defined? @variables
33
- raise "variable :#{name} is already defined" if @variables.find {|v| v.name == name}
34
- @variables << ProcVariable.new(name, desc, &block)
28
+ def param(name, desc, values = nil, &block)
29
+ @parameters = [] unless defined? @parameters
30
+ raise "parameter :#{name} is already defined" if @parameters.find {|v| v.name == name}
31
+ if block
32
+ @parameters << ProcParameter.new(name, desc, &block)
33
+ else
34
+ @parameters << Parameter.new(name, desc, values)
35
+ end
35
36
  end
36
37
 
37
38
  end
@@ -0,0 +1,25 @@
1
+ module Prompt
2
+ module DSLHelper
3
+
4
+ # Split command into an array of Strings and Parameters
5
+ def self.words command_name, parameters
6
+ command_name.strip.split(/\s+/).map do |word|
7
+ if word[0] == ":"
8
+ sym = word[1..-1].to_sym
9
+ parameters.find {|p| p.name == sym} || Parameter.new(sym)
10
+ elsif word[0] == "*"
11
+ sym = word[1..-1].to_sym
12
+ param = parameters.find {|p| p.name == sym}
13
+ # Since GlobParameters can't be defined ahead of time in the DSL, we
14
+ # always create a new one and copy the description
15
+ # This is kinda jankey
16
+ desc = param ? param.desc : nil
17
+ GlobParameter.new(sym, desc)
18
+ else
19
+ word
20
+ end
21
+ end
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,17 @@
1
+ module Prompt
2
+ class GlobParameter < Parameter
3
+
4
+ def initialize(name, desc = nil)
5
+ super(name, desc)
6
+ end
7
+
8
+ def regex
9
+ "(?<#{name}>(([^#{Prompt::Command::SEP}]*)#{Prompt::Command::SEP}?)+)"
10
+ end
11
+
12
+ def matches s
13
+ s.split(Prompt::Command::SEP)
14
+ end
15
+
16
+ end
17
+ end
@@ -1,11 +1,11 @@
1
1
  module Prompt
2
- class Variable
2
+ class Parameter
3
3
 
4
4
  attr :name
5
5
  attr :desc
6
6
  attr :values
7
7
 
8
- def initialize(name, desc, values = nil)
8
+ def initialize(name, desc = nil, values = nil)
9
9
  @name = name
10
10
  @desc = desc
11
11
  @values = values
@@ -15,12 +15,22 @@ module Prompt
15
15
  if values
16
16
  "(?<#{name}>#{values.map{|v| Regexp.escape(v)}.join("|")})"
17
17
  else
18
- "('(?<#{name}>[^\']*)'|\"(?<#{name}>[^\"]*)\"|(?<#{name}>[^\s]+))"
18
+ "(?<#{name}>([^#{Prompt::Command::SEP}]*))"
19
19
  end
20
20
  end
21
21
 
22
22
  def expansions
23
- values || ["<#{name}>"]
23
+ if values
24
+ values.map do |v|
25
+ (v =~ /\s/) ? "\"#{v}\"" : v
26
+ end
27
+ else
28
+ ["<#{name}>"]
29
+ end
30
+ end
31
+
32
+ def matches s
33
+ s
24
34
  end
25
35
 
26
36
  end
@@ -1,5 +1,5 @@
1
1
  module Prompt
2
- class ProcVariable < Variable
2
+ class ProcParameter < Parameter
3
3
 
4
4
  def initialize(name, desc, &block)
5
5
  super(name, desc, nil)
@@ -7,7 +7,11 @@ module Prompt
7
7
  end
8
8
 
9
9
  def values
10
- @proc.call
10
+ @cached_value ||= @proc.call
11
+ end
12
+
13
+ def clear_cached_value
14
+ @cached_value = nil
11
15
  end
12
16
 
13
17
  end
@@ -1,5 +1,5 @@
1
1
  module Prompt
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
 
4
4
  class << self
5
5
 
@@ -1,6 +1,6 @@
1
1
  require 'prompt/command'
2
- require 'prompt/variable'
3
- require 'prompt/glob_variable'
2
+ require 'prompt/parameter'
3
+ require 'prompt/glob_parameter'
4
4
 
5
5
  include Prompt
6
6
 
@@ -10,119 +10,158 @@ describe Prompt::Command do
10
10
  SPEEDS = %w{quickly slowly}
11
11
 
12
12
  describe "#match" do
13
- it "matches correctly" do
14
- c = Command.new("hi")
15
13
 
16
- c.match("hi").should == []
17
- c.match("hi ").should == []
18
- c.match(" hi").should == []
14
+ describe "hi" do
15
+
16
+ it "matches correctly" do
17
+ c = Command.new ["hi"]
18
+
19
+ c.match("hi").should == []
20
+ c.match("hi ").should == []
21
+ c.match(" hi").should == []
22
+
23
+ c.match("bye").should be_nil
24
+ c.match("h").should be_nil
25
+ c.match("").should be_nil
26
+ end
19
27
 
20
- c.match("bye").should be_nil
21
- c.match("h").should be_nil
22
- c.match("").should be_nil
23
28
  end
24
29
 
25
- it "matches correctly with a variable" do
26
- c = Command.new("hi :name")
30
+ describe "hi :name" do
27
31
 
28
- c.match("hi guy").should == ["guy"]
29
- c.match("hi 'some guy'").should == ["some guy"]
30
- c.match("hi ''").should == [""]
31
- c.match('hi "some guy"').should == ["some guy"]
32
- c.match('hi ""').should == [""]
32
+ it "matches correctly" do
33
+ c = Command.new ["hi", Parameter.new(:name)]
33
34
 
34
- c.match("hi '").should == ["'"]
35
- c.match('hi "').should == ['"']
36
- c.match('hi \'"').should == ['\'"']
35
+ c.match("hi guy").should == ["guy"]
36
+ c.match("hi 'some guy'").should == ["some guy"]
37
+ c.match("hi ''").should == [""]
38
+ c.match('hi "some guy"').should == ["some guy"]
39
+ c.match('hi ""').should == [""]
37
40
 
38
- c.match("higuy").should be_nil
39
- c.match("higuy guy").should be_nil
40
- end
41
+ c.match("hi '").should == ["'"]
42
+ c.match('hi "').should == ['"']
43
+ c.match('hi \'"').should == ['\'"']
44
+
45
+ c.match("higuy").should be_nil
46
+ c.match("higuy guy").should be_nil
41
47
 
42
- it "matches correctly with a variable and multiple spaces" do
43
- c = Command.new("hi :name")
48
+ c.match(" hi guy").should == ["guy"]
49
+ c.match("hi guy").should == ["guy"]
50
+ c.match("hi guy ").should == ["guy"]
51
+ end
52
+
53
+ it "matches correctly (with parameter value constraint)" do
54
+ c = Command.new ["hi", Parameter.new(:name, "", ["alice", "bob", "charlie rose"])]
55
+
56
+ c.match("hi alice").should == ["alice"]
57
+ c.match("hi bob").should == ["bob"]
58
+ c.match("hi 'charlie rose'").should == ["charlie rose"]
59
+ c.match("hi charlie rose").should be_nil
60
+ c.match("hi zack").should be_nil
61
+ c.match("hi ali").should be_nil
62
+ end
44
63
 
45
- c.match(" hi guy").should == ["guy"]
46
- c.match("hi guy").should == ["guy"]
47
- c.match("hi guy ").should == ["guy"]
48
64
  end
49
65
 
50
- it "matches correctly with 2 variables" do
51
- c = Command.new("hi :first :last")
66
+ describe "hi :first :last" do
67
+
68
+ it "matches correctly" do
69
+ c = Command.new ["hi", Parameter.new(:first), Parameter.new(:last)]
70
+
71
+ c.match("hi agent smith").should == ["agent", "smith"]
72
+ c.match("hi agent").should be_nil
73
+ c.match("hi agent smith guy").should be_nil
74
+ end
52
75
 
53
- c.match("hi agent smith").should == ["agent", "smith"]
54
- c.match("hi agent").should be_nil
55
- c.match("hi agent smith guy").should be_nil
56
76
  end
57
77
 
58
- it "matches correctly with variable value constraint" do
59
- v = [Variable.new(:dir, "", DIRECTIONS)]
60
- c = Command.new("go :dir", nil, v)
78
+ describe "say *stuff" do
79
+
80
+ it "matches correctly" do
81
+ c = Command.new ["say", GlobParameter.new(:stuff)]
82
+
83
+ c.match("say hello").should == [["hello"]]
84
+ c.match("say hello world").should == [["hello", "world"]]
85
+ c.match("say hello world").should == [["hello", "world"]]
86
+ c.match("say hello world").should == [["hello", "world"]]
87
+ c.match("say 'hello world'").should == [["hello world"]]
88
+ c.match('say "hello world"').should == [["hello world"]]
89
+ end
61
90
 
62
- c.match("go n").should == ["n"]
63
- c.match("go s").should == ["s"]
64
- c.match("go x").should be_nil
65
- c.match("go nn").should be_nil
66
91
  end
67
92
 
68
- it "matches correctly with glob variable" do
69
- c = Command.new("say *stuff")
93
+ describe "say *stuff :adverb" do
94
+
95
+ it "matches correctly" do
96
+ c = Command.new ["say", GlobParameter.new(:stuff), Parameter.new(:adverb)]
97
+
98
+ c.match("say hello").should be_nil
99
+ c.match("say hello loudly").should == [["hello"], "loudly"]
100
+ c.match("say hello world loudly").should == [["hello", "world"], "loudly"]
101
+ c.match("say hello 'world loudly'").should == [["hello"], "world loudly"]
102
+ end
70
103
 
71
- c.match("say hello").should == ["hello"]
72
- c.match("say hello world").should == ["hello world"]
73
104
  end
74
105
 
75
- it "matches correctly with glob variable and other variable" do
76
- c = Command.new("say *stuff :adverb")
106
+ describe "say *first *second" do
107
+
108
+ it "matches correctly" do
109
+ c = Command.new ["say", GlobParameter.new(:first), GlobParameter.new(:second)]
110
+
111
+ c.match("say one").should be_nil
112
+ c.match("say one two").should == [["one"], ["two"]]
113
+ c.match("say one two three").should == [["one", "two"], ["three"]]
114
+ c.match("say one 'two three'").should == [["one"], ["two three"]]
115
+ end
77
116
 
78
- c.match("say hello").should be_nil
79
- c.match("say hello loudly").should == ["hello", "loudly"]
80
- c.match("say hello world loudly").should == ["hello world", "loudly"]
81
117
  end
82
- end
83
118
 
84
- describe "#variables" do
119
+ end # describe "#match"
120
+
121
+ describe "#parameters" do
85
122
  it "returns correctly" do
86
- color = Variable.new(:color, "")
87
- flavor = Variable.new(:flavor, "")
88
-
89
- Command.new("one").variables.should == []
90
- vs = Command.new("one :color").variables
91
- vs.length.should == 1
92
- vs.first.name.should == :color
93
- Command.new("one :color", nil, [color]).variables.should == [color]
94
- Command.new("one :color", nil, [color, flavor]).variables.should == [color]
123
+ color = Parameter.new(:color, "")
124
+ flavor = GlobParameter.new(:flavor, "")
125
+
126
+ Command.new(["one"]).parameters.should == []
127
+ Command.new(["one", color]).parameters.should == [color]
128
+ Command.new(["one", color, flavor]).parameters == [color, flavor]
95
129
  end
96
130
  end
97
131
 
98
132
  describe "#expansions" do
99
- it "expands correctly with no variables" do
100
- Command.new("one").expansions.should == ["one"]
133
+ it "expands correctly with no parameters" do
134
+ Command.new(["one"]).expansions.should == ["one"]
101
135
  end
102
136
 
103
- it "expands correctly with undefined variables" do
104
- Command.new("go :dir").expansions.should == ["go <dir>"]
137
+ it "expands correctly with undefined parameters" do
138
+ Command.new(["go", Parameter.new(:dir)]).expansions.should == ["go <dir>"]
105
139
  end
106
140
 
107
- it "expands correctly with defined variables" do
108
- v = [Variable.new(:dir, "", DIRECTIONS), Variable.new(:speed, "", SPEEDS)]
109
- Command.new("go :dir", nil, v).expansions.should == ["go n", "go e", "go s", "go w"]
110
- Command.new("go :dir :speed", nil, v).expansions.should ==
141
+ it "expands correctly with defined parameters" do
142
+ dir = Parameter.new(:dir, "", DIRECTIONS)
143
+ speed = Parameter.new(:speed, "", SPEEDS)
144
+ Command.new(["go", dir]).expansions.should == ["go n", "go e", "go s", "go w"]
145
+ Command.new(["go", dir, speed]).expansions.should ==
111
146
  ["go n quickly", "go n slowly",
112
147
  "go e quickly", "go e slowly",
113
148
  "go s quickly", "go s slowly",
114
149
  "go w quickly", "go w slowly"]
115
150
  end
151
+
152
+ it"expands correctly if parameter values have spaces" do
153
+ speed = Parameter.new(:speed, "", ["fast", "very fast"])
154
+ Command.new(["go", speed]).expansions.should == ['go fast', 'go "very fast"']
155
+ end
116
156
  end
117
157
 
118
158
  describe "#usage" do
119
159
  it "returns correctly" do
120
- color = Variable.new(:color, "")
160
+ color = Parameter.new(:color, "")
121
161
 
122
- Command.new("one").usage.should == "one"
123
- Command.new("one :color").usage.should == "one <color>"
124
- Command.new("one :color", nil, [color]).usage.should == "one <color>"
125
- Command.new("one :color three").usage.should == "one <color> three"
162
+ Command.new(["one"]).usage.should == "one"
163
+ Command.new(["one", color]).usage.should == "one <color>"
164
+ Command.new(["one", color, "three"]).usage.should == "one <color> three"
126
165
  end
127
166
  end
128
167
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prompt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-17 00:00:00.000000000 Z
12
+ date: 2012-02-23 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Prompt makes it easy to build slick command-line applications with Tab
15
15
  Completion, Command History, and Built-in Help
@@ -19,6 +19,8 @@ extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
21
  - README.md
22
+ - CHANGELOG.md
23
+ - LICENSE.txt
22
24
  - lib/prompt/application.rb
23
25
  - lib/prompt/command.rb
24
26
  - lib/prompt/command_group.rb
@@ -27,10 +29,11 @@ files:
27
29
  - lib/prompt/console/console_module.rb
28
30
  - lib/prompt/console.rb
29
31
  - lib/prompt/dsl.rb
30
- - lib/prompt/glob_variable.rb
31
- - lib/prompt/proc_variable.rb
32
+ - lib/prompt/dsl_helper.rb
33
+ - lib/prompt/glob_parameter.rb
34
+ - lib/prompt/parameter.rb
35
+ - lib/prompt/proc_parameter.rb
32
36
  - lib/prompt/prompt_module.rb
33
- - lib/prompt/variable.rb
34
37
  - lib/prompt.rb
35
38
  - spec/prompt/command_spec.rb
36
39
  - examples/file_manager
@@ -55,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
58
  version: 1.3.6
56
59
  requirements: []
57
60
  rubyforge_project:
58
- rubygems_version: 1.8.10
61
+ rubygems_version: 1.8.15
59
62
  signing_key:
60
63
  specification_version: 3
61
64
  summary: A small framework that makes it easy to build slick command-line applications
@@ -1,14 +0,0 @@
1
- module Prompt
2
- class GlobVariable < Variable
3
-
4
- def initialize(name, desc, &block)
5
- super(name, desc, nil)
6
- @proc = block
7
- end
8
-
9
- def regex
10
- "(?<#{name}>.+)"
11
- end
12
-
13
- end
14
- end