Skip to content

Commit

Permalink
Fix propagation of default values during parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Jun 20, 2016
1 parent c453690 commit 71a5f72
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ One of the other issues I had with existing frameworks is testability. Most fram

- [Teapot](https://github.com/ioquatix/teapot/blob/master/lib/teapot/command.rb) is a build system and uses multiple top-level commands.
- [Utopia](https://github.com/ioquatix/utopia/blob/master/lib/utopia/command.rb) is a web application platform and uses nested commands.
- [LSync](https://github.com/ioquatix/lsync/blob/master/lib/lsync/command.rb) is a backup tool and sends commands across the network.
- [LSync](https://github.com/ioquatix/lsync/blob/master/lib/lsync/command.rb) is a backup tool and sends commands across the network and has lots of options with default values.

## Installation

Expand Down
14 changes: 13 additions & 1 deletion lib/samovar/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def initialize(flags, description, key: nil, default: nil, value: nil)
attr :description
attr :type

attr :default

attr :key

def parse(input)
Expand Down Expand Up @@ -77,11 +79,17 @@ def self.parse(*args, **options, &block)
def initialize(title = "Options", key: :options)
@title = title
@ordered = []

# We use this flag to option cache to improve parsing performance:
@keyed = {}

@key = key

@defaults = {}
end

attr :key
attr :defaults

def option(*args, **options)
self << Option.new(*args, **options)
Expand All @@ -96,10 +104,14 @@ def << option
@keyed[alternative] = option
end
end

if default = option.default
@defaults[option.key] = option.default
end
end

def parse(input)
values = Hash.new
values = @defaults.dup

while option = @keyed[input.first]
if result = option.parse(input)
Expand Down
2 changes: 1 addition & 1 deletion lib/samovar/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
# THE SOFTWARE.

module Samovar
VERSION = "1.1.0"
VERSION = "1.1.1"
end
7 changes: 6 additions & 1 deletion spec/samovar/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Top < Samovar::Command
self.description = "A decentralised package manager and build tool."

options do
option '-c/--configuration <name>', "Specify a specific build configuration.", default: ENV['TEAPOT_CONFIGURATION']
option '-c/--configuration <name>', "Specify a specific build configuration.", default: 'TEAPOT_CONFIGURATION'
option '-i/--in/--root <path>', "Work in the given root directory."
option '--verbose | --quiet', "Verbosity of output for debugging.", key: :logging
option '-h/--help', "Print out help information."
Expand All @@ -28,6 +28,11 @@ class Top < Samovar::Command
end

describe Samovar::Command do
it "should use default value" do
top = Command::Top.parse([])
expect(top.options[:configuration]).to be == 'TEAPOT_CONFIGURATION'
end

it "should parse a simple command" do
top = Command::Top.parse(["-c", "path", "bottom", "foobar", "A", "B", "--", "args", "args"])

Expand Down

0 comments on commit 71a5f72

Please sign in to comment.