cli 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.
data/README.md CHANGED
@@ -96,6 +96,7 @@ Example version output:
96
96
 
97
97
  ```ruby
98
98
  require 'cli'
99
+ require 'yaml'
99
100
 
100
101
  options = CLI.new do
101
102
  description 'Generate blog posts in given Jekyll directory from input statistics'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "cli"
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jakub Pastuszek"]
12
- s.date = "2011-12-16"
12
+ s.date = "2011-12-19"
13
13
  s.description = "Provides DSL for command-line options, switches and arguments parser and stdin handling with generated usage printer"
14
14
  s.email = "jpastuszek@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -4,6 +4,7 @@ cli_root = Pathname.new(__FILE__).dirname + '..'
4
4
  $LOAD_PATH.unshift(cli_root + 'lib')
5
5
 
6
6
  require 'cli'
7
+ require 'yaml'
7
8
 
8
9
  options = CLI.new do
9
10
  description 'Generate blog posts in given Jekyll directory from input statistics'
data/lib/cli.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require 'ostruct'
2
2
  require 'stringio'
3
- require 'yaml'
4
3
 
5
4
  require 'cli/dsl'
6
5
  require 'cli/arguments'
@@ -23,31 +22,31 @@ class CLI
23
22
 
24
23
  class ArgumentNameSpecifiedTwice < ParserError
25
24
  def initialize(arg)
26
- super("argument #{arg} specified twice")
25
+ super("argument '#{arg}' specified twice")
27
26
  end
28
27
  end
29
28
 
30
29
  class LongNameSpecifiedTwiceError < ParserError
31
- def initialize(what, name)
32
- super("#{what} #{name} specified twice")
30
+ def initialize(what, switch_dsl)
31
+ super("#{what} #{switch_dsl.switch} specified twice")
33
32
  end
34
33
  end
35
34
 
36
35
  class ShortNameSpecifiedTwiceError < ParserError
37
- def initialize(what, name)
38
- super("short #{what} #{name} specified twice")
36
+ def initialize(what, switch_dsl)
37
+ super("short #{what} #{switch_dsl.switch_short} specified twice")
39
38
  end
40
39
  end
41
40
 
42
41
  class ShortNameNotSymbolError < ParserError
43
- def initialize(short)
44
- super("short name has to be of type Symbol, got #{short.class.name}")
42
+ def initialize(switch_dsl, short)
43
+ super("short name for #{switch_dsl.switch} has to be of type Symbol, got #{short.class.name}")
45
44
  end
46
45
  end
47
46
 
48
47
  class ShortNameIsInvalidError < ParserError
49
- def initialize(short)
50
- super("short name has to be one letter symbol, got #{short}")
48
+ def initialize(switch_dsl, short)
49
+ super("short name for #{switch_dsl.switch} has to be one letter symbol, got #{short.inspect}")
51
50
  end
52
51
  end
53
52
  end
@@ -73,13 +72,13 @@ class CLI
73
72
 
74
73
  class MandatoryArgumentNotSpecifiedError < ParsingError
75
74
  def initialize(arg)
76
- super("mandatory argument #{arg} not given")
75
+ super("mandatory argument '#{arg}' not given")
77
76
  end
78
77
  end
79
78
 
80
79
  class CastError < ParsingError
81
80
  def initialize(arg, cast_name, error)
82
- super("failed to cast: #{arg} to type: #{cast_name}: #{error}")
81
+ super("failed to cast: '#{arg}' to type: #{cast_name}: #{error}")
83
82
  end
84
83
  end
85
84
  end
@@ -128,10 +127,10 @@ class CLI
128
127
  def switch(name, options = {})
129
128
  switch_dsl = DSL::Switch.new(name, options)
130
129
 
131
- raise ParserError::LongNameSpecifiedTwiceError.new('switch', switch_dsl.name) if @switches.has_long?(switch_dsl)
132
- raise ParserError::LongNameSpecifiedTwiceError.new('option and switch', switch_dsl.name) if @options.has_long?(switch_dsl)
133
- raise ParserError::ShortNameSpecifiedTwiceError.new('switch', switch_dsl.short) if @switches.has_short?(switch_dsl)
134
- raise ParserError::ShortNameSpecifiedTwiceError.new('option and switch', switch_dsl.short) if @options.has_short?(switch_dsl)
130
+ raise ParserError::LongNameSpecifiedTwiceError.new('switch', switch_dsl) if @switches.has_long?(switch_dsl)
131
+ raise ParserError::LongNameSpecifiedTwiceError.new('option and switch', switch_dsl) if @options.has_long?(switch_dsl)
132
+ raise ParserError::ShortNameSpecifiedTwiceError.new('switch', switch_dsl) if @switches.has_short?(switch_dsl)
133
+ raise ParserError::ShortNameSpecifiedTwiceError.new('option and switch', switch_dsl) if @options.has_short?(switch_dsl)
135
134
 
136
135
  @switches << switch_dsl
137
136
  end
@@ -139,10 +138,10 @@ class CLI
139
138
  def option(name, options = {})
140
139
  option_dsl = DSL::Option.new(name, options)
141
140
 
142
- raise ParserError::LongNameSpecifiedTwiceError.new('option', option_dsl.name) if @options.has_long?(option_dsl)
143
- raise ParserError::LongNameSpecifiedTwiceError.new('switch and option', option_dsl.name) if @switches.has_long?(option_dsl)
144
- raise ParserError::ShortNameSpecifiedTwiceError.new('option', option_dsl.short) if @options.has_short?(option_dsl)
145
- raise ParserError::ShortNameSpecifiedTwiceError.new('switch and option', option_dsl.short) if @switches.has_short?(option_dsl)
141
+ raise ParserError::LongNameSpecifiedTwiceError.new('option', option_dsl) if @options.has_long?(option_dsl)
142
+ raise ParserError::LongNameSpecifiedTwiceError.new('switch and option', option_dsl) if @switches.has_long?(option_dsl)
143
+ raise ParserError::ShortNameSpecifiedTwiceError.new('option', option_dsl) if @options.has_short?(option_dsl)
144
+ raise ParserError::ShortNameSpecifiedTwiceError.new('switch and option', option_dsl) if @switches.has_short?(option_dsl)
146
145
 
147
146
  @options << option_dsl
148
147
  end
@@ -22,10 +22,12 @@ class CLI
22
22
  value.to_i
23
23
  elsif cast_to == Float
24
24
  value.to_f
25
- elsif cast_to == YAML
26
- YAML.load(value)
27
- else
25
+ elsif cast_to.respond_to? :new
28
26
  cast_to.new(value)
27
+ elsif cast_to.respond_to? :load
28
+ cast_to.load(value)
29
+ else
30
+ raise ArgumentError, "can't cast to class or module #{cast_to.class.name}"
29
31
  end
30
32
  else
31
33
  if cast_to.is_a? Proc
@@ -87,8 +89,8 @@ class CLI
87
89
  def initialize(name, options = {})
88
90
  super(name, options)
89
91
  if short = options[:short]
90
- raise ParserError::ShortNameNotSymbolError.new(short) if not short.is_a? Symbol
91
- raise ParserError::ShortNameIsInvalidError.new(short) if short.to_s.length > 1
92
+ raise ParserError::ShortNameNotSymbolError.new(self, short) if not short.is_a? Symbol
93
+ raise ParserError::ShortNameIsInvalidError.new(self, short) if short.to_s.length > 1
92
94
  end
93
95
  end
94
96
 
@@ -63,7 +63,7 @@ describe CLI do
63
63
  argument :number
64
64
  argument :number
65
65
  end
66
- }.should raise_error CLI::ParserError::ArgumentNameSpecifiedTwice, 'argument number specified twice'
66
+ }.should raise_error CLI::ParserError::ArgumentNameSpecifiedTwice, "argument 'number' specified twice"
67
67
  end
68
68
 
69
69
  it "should be required by default and raise error if not given" do
@@ -71,7 +71,7 @@ describe CLI do
71
71
  ps = CLI.new do
72
72
  argument :log
73
73
  end.parse([])
74
- }.should raise_error CLI::ParsingError::MandatoryArgumentNotSpecifiedError, 'mandatory argument log not given'
74
+ }.should raise_error CLI::ParsingError::MandatoryArgumentNotSpecifiedError, "mandatory argument 'log' not given"
75
75
  end
76
76
 
77
77
  it "should raise error if casting fail" do
@@ -80,7 +80,7 @@ describe CLI do
80
80
  ps = CLI.new do
81
81
  argument :log, :cast => IP
82
82
  end.parse(['abc'])
83
- }.should raise_error CLI::ParsingError::CastError, 'failed to cast: log to type: IP: invalid address'
83
+ }.should raise_error CLI::ParsingError::CastError, "failed to cast: 'log' to type: IP: invalid address"
84
84
  end
85
85
 
86
86
  describe "with defaults" do
@@ -9,28 +9,28 @@ describe CLI do
9
9
  switch :location
10
10
  switch :location
11
11
  end
12
- }.should raise_error CLI::ParserError::LongNameSpecifiedTwiceError, 'switch location specified twice'
12
+ }.should raise_error CLI::ParserError::LongNameSpecifiedTwiceError, 'switch --location specified twice'
13
13
 
14
14
  lambda {
15
15
  ps = CLI.new do
16
16
  option :location
17
17
  option :location
18
18
  end
19
- }.should raise_error CLI::ParserError::LongNameSpecifiedTwiceError, 'option location specified twice'
19
+ }.should raise_error CLI::ParserError::LongNameSpecifiedTwiceError, 'option --location specified twice'
20
20
 
21
21
  lambda {
22
22
  ps = CLI.new do
23
23
  switch :location
24
24
  option :location
25
25
  end
26
- }.should raise_error CLI::ParserError::LongNameSpecifiedTwiceError, 'switch and option location specified twice'
26
+ }.should raise_error CLI::ParserError::LongNameSpecifiedTwiceError, 'switch and option --location specified twice'
27
27
 
28
28
  lambda {
29
29
  ps = CLI.new do
30
30
  option :location
31
31
  switch :location
32
32
  end
33
- }.should raise_error CLI::ParserError::LongNameSpecifiedTwiceError, 'option and switch location specified twice'
33
+ }.should raise_error CLI::ParserError::LongNameSpecifiedTwiceError, 'option and switch --location specified twice'
34
34
  end
35
35
  end
36
36
 
@@ -41,28 +41,28 @@ describe CLI do
41
41
  switch :location, :short => :l
42
42
  switch :location2, :short => :l
43
43
  end
44
- }.should raise_error CLI::ParserError::ShortNameSpecifiedTwiceError, 'short switch l specified twice'
44
+ }.should raise_error CLI::ParserError::ShortNameSpecifiedTwiceError, 'short switch -l specified twice'
45
45
 
46
46
  lambda {
47
47
  ps = CLI.new do
48
48
  option :location, :short => :l
49
49
  option :location2, :short => :l
50
50
  end
51
- }.should raise_error CLI::ParserError::ShortNameSpecifiedTwiceError, 'short option l specified twice'
51
+ }.should raise_error CLI::ParserError::ShortNameSpecifiedTwiceError, 'short option -l specified twice'
52
52
 
53
53
  lambda {
54
54
  ps = CLI.new do
55
55
  switch :location, :short => :l
56
56
  option :location2, :short => :l
57
57
  end
58
- }.should raise_error CLI::ParserError::ShortNameSpecifiedTwiceError, 'short switch and option l specified twice'
58
+ }.should raise_error CLI::ParserError::ShortNameSpecifiedTwiceError, 'short switch and option -l specified twice'
59
59
 
60
60
  lambda {
61
61
  ps = CLI.new do
62
62
  option :location2, :short => :l
63
63
  switch :location, :short => :l
64
64
  end
65
- }.should raise_error CLI::ParserError::ShortNameSpecifiedTwiceError, 'short option and switch l specified twice'
65
+ }.should raise_error CLI::ParserError::ShortNameSpecifiedTwiceError, 'short option and switch -l specified twice'
66
66
  end
67
67
  end
68
68
  end
@@ -1,4 +1,5 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'yaml'
2
3
 
3
4
  describe CLI do
4
5
  describe 'STDIN handling' do
@@ -23,7 +24,7 @@ EOF
23
24
  ps.stdin.should be_a IO
24
25
  end
25
26
 
26
- it "should support casting to YAML" do
27
+ it "should support casting to module responding to load" do
27
28
  ps = nil
28
29
  ss = CLI.new do
29
30
  stdin :log_data, :cast => YAML, :description => 'log statistic data in YAML format'
@@ -39,13 +39,13 @@ describe CLI do
39
39
  ps = CLI.new do
40
40
  switch :location, :short => "l"
41
41
  end
42
- }.should raise_error CLI::ParserError::ShortNameNotSymbolError, 'short name has to be of type Symbol, got String'
42
+ }.should raise_error CLI::ParserError::ShortNameNotSymbolError, 'short name for --location has to be of type Symbol, got String'
43
43
 
44
44
  lambda {
45
45
  ps = CLI.new do
46
46
  switch :location, :short => :abc
47
47
  end
48
- }.should raise_error CLI::ParserError::ShortNameIsInvalidError, 'short name has to be one letter symbol, got abc'
48
+ }.should raise_error CLI::ParserError::ShortNameIsInvalidError, 'short name for --location has to be one letter symbol, got :abc'
49
49
  end
50
50
 
51
51
  it "should raise error on unrecognized switch" do
@@ -64,13 +64,13 @@ describe CLI do
64
64
  ps = CLI.new do
65
65
  switch :help
66
66
  end
67
- }.should raise_error CLI::ParserError::LongNameSpecifiedTwiceError, 'switch help specified twice'
67
+ }.should raise_error CLI::ParserError::LongNameSpecifiedTwiceError, 'switch --help specified twice'
68
68
 
69
69
  lambda {
70
70
  ps = CLI.new do
71
71
  switch :help2, :short => :h
72
72
  end
73
- }.should raise_error CLI::ParserError::ShortNameSpecifiedTwiceError, 'short switch h specified twice'
73
+ }.should raise_error CLI::ParserError::ShortNameSpecifiedTwiceError, 'short switch -h specified twice'
74
74
  end
75
75
 
76
76
  it "should display help switch in the help message as the last entry" do
@@ -119,14 +119,14 @@ describe CLI do
119
119
  version '1.0.2'
120
120
  switch :version
121
121
  end
122
- }.should raise_error CLI::ParserError::LongNameSpecifiedTwiceError, 'switch version specified twice'
122
+ }.should raise_error CLI::ParserError::LongNameSpecifiedTwiceError, 'switch --version specified twice'
123
123
 
124
124
  lambda {
125
125
  ps = CLI.new do
126
126
  version '1.0.2'
127
127
  option :version
128
128
  end
129
- }.should raise_error CLI::ParserError::LongNameSpecifiedTwiceError, 'option and switch version specified twice'
129
+ }.should raise_error CLI::ParserError::LongNameSpecifiedTwiceError, 'option and switch --version specified twice'
130
130
  end
131
131
  end
132
132
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cli
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jakub Pastuszek
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-12-16 00:00:00 Z
18
+ date: 2011-12-19 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  type: :development