wpb 0.0.5 → 0.0.6.pre

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/lib/wpb/bash.rb CHANGED
@@ -1,18 +1,28 @@
1
- require "wpb/clamp/command"
1
+ require "clamp"
2
2
 
3
3
  class Bash < Clamp::Command
4
- option ["-v", "--version"], :flag, "print version"
4
+ option ["-v", "--version"], :flag, "Show version" do
5
+ require "wpb/version"
6
+ puts "WordPress Bash #{WPB::VERSION} (c) James Birtles"
7
+ exit 0
8
+ end
9
+
10
+ # self.default_subcommand = "console"
5
11
 
6
- def execute
7
- if version?
8
- require "wpb/version"
9
- print "WordPress Bash #{WPB::VERSION} (c) James Birtles"
10
- else
11
- irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
12
- libs = " -r irb/completion"
13
- libs << " -r #{File.expand_path('../../wpb.rb', __FILE__)}"
14
- puts "Connecting to database..."
15
- exec "#{irb} #{libs}"
16
- end
12
+ subcommand ["console", "c"], "Start the wpb console" do
13
+ def execute
14
+ require "pry"
15
+ require "wpb"
16
+ WPB::App.run
17
+ WPB.pry
18
+ end
17
19
  end
20
+
21
+ subcommand "init", "Commands to be run when first using WPB with your wordpress instance" do
22
+ def execute
23
+ require "wpb"
24
+ WPB::App.connect
25
+ WPB::AddTypeToWpPosts.up
26
+ end
27
+ end
18
28
  end
data/lib/wpb/version.rb CHANGED
@@ -2,5 +2,5 @@ module WPB
2
2
  ##
3
3
  # The version of WPB you are using
4
4
 
5
- VERSION = "0.0.5"
5
+ VERSION = "0.0.6.pre"
6
6
  end
data/lib/wpb.rb CHANGED
@@ -11,12 +11,14 @@ require "active_record"
11
11
  # The wrapper for the wpb gem
12
12
 
13
13
  module WPB
14
- class AddType < ActiveRecord::Migration
14
+ class AddTypeToWpPosts < ActiveRecord::Migration
15
15
  def self.up
16
16
  suppress_messages do
17
17
  unless column_exists? :wp_posts, :type
18
18
  add_column :wp_posts, :type, :string
19
19
  PagePost.reset_column_information
20
+ else
21
+ puts "\tWordPress Database Already Initialized"
20
22
  end
21
23
  end
22
24
  end
@@ -27,10 +29,10 @@ module WPB
27
29
  end
28
30
 
29
31
  class App
30
- def self.run
31
- config = {
32
+ def self.connect
33
+ config = {
32
34
  :adapter => "mysql",
33
- :socket => "/Applications/MAMP/tmp/mysql/mysql.sock", #/tmp/mysql.sock
35
+ # :socket => "/tmp/mysql.sock",
34
36
  :host => "localhost",
35
37
  :username => "root",
36
38
  :password => "root",
@@ -38,8 +40,11 @@ module WPB
38
40
  }
39
41
 
40
42
  ActiveRecord::Base.establish_connection(config)
43
+ end
44
+
45
+ def self.run
46
+ self.connect
41
47
 
42
- AddType::up
43
48
  PagePost.all.each do |p|
44
49
  p.type = p.post_type.capitalize
45
50
  if p.post_type != "post" && p.post_type != "page"
@@ -50,5 +55,3 @@ module WPB
50
55
  end
51
56
  end
52
57
  end
53
-
54
- WPB::App::run
data/wpb.gemspec CHANGED
@@ -8,14 +8,17 @@ Gem::Specification.new do |s|
8
8
  s.platform = Gem::Platform::RUBY
9
9
  s.authors = ["James Birtles"]
10
10
  s.email = ["itsme@jamesbirtles.com"]
11
- s.homepage = "http://github.com/jamesbirtles/wpb"
11
+ s.homepage = "http://jamesbirtles.org/wpb"
12
12
  s.summary = %q{A Terminal interface to WordPress}
13
- s.description = %q{WPB (WordPress Bash) provides a Terminal interface to WordPress. It tries to achieve a similar feel to the rails console.}
13
+ s.description = %q{WPB (WordPress Bash) provides a Terminal interface to WordPress. It uses ActiveRecord, which means the sytax is mostly the same}
14
14
 
15
15
  s.files = `git ls-files`.split("\n")
16
16
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
17
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
18
  s.require_paths = ["lib"]
19
19
 
20
- s.add_dependency('activerecord', '>= 3.0.0')
20
+ s.add_dependency('activerecord', '3.0.0')
21
+ s.add_dependency('clamp', '>= 0.3.0')
22
+ s.add_dependency('pry', '>= 0.9.7')
23
+ s.add_dependency('mysql')
21
24
  end
metadata CHANGED
@@ -1,39 +1,69 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: wpb
3
- version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.0.5
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.6.pre
5
+ prerelease: 6
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - James Birtles
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-05-20 00:00:00 +01:00
14
- default_executable:
15
- dependencies:
16
- - !ruby/object:Gem::Dependency
12
+ date: 2011-11-28 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
17
15
  name: activerecord
18
- prerelease: false
19
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70301384249100 !ruby/object:Gem::Requirement
20
17
  none: false
21
- requirements:
22
- - - ">="
23
- - !ruby/object:Gem::Version
18
+ requirements:
19
+ - - =
20
+ - !ruby/object:Gem::Version
24
21
  version: 3.0.0
25
22
  type: :runtime
26
- version_requirements: *id001
27
- description: WPB (WordPress Bash) provides a Terminal interface to WordPress. It tries to achieve a similar feel to the rails console.
28
- email:
23
+ prerelease: false
24
+ version_requirements: *70301384249100
25
+ - !ruby/object:Gem::Dependency
26
+ name: clamp
27
+ requirement: &70301384248580 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 0.3.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70301384248580
36
+ - !ruby/object:Gem::Dependency
37
+ name: pry
38
+ requirement: &70301384248040 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: 0.9.7
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70301384248040
47
+ - !ruby/object:Gem::Dependency
48
+ name: mysql
49
+ requirement: &70301384247620 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *70301384247620
58
+ description: WPB (WordPress Bash) provides a Terminal interface to WordPress. It uses
59
+ ActiveRecord, which means the sytax is mostly the same
60
+ email:
29
61
  - itsme@jamesbirtles.com
30
- executables:
62
+ executables:
31
63
  - wpb
32
64
  extensions: []
33
-
34
65
  extra_rdoc_files: []
35
-
36
- files:
66
+ files:
37
67
  - .gitignore
38
68
  - Changelog.md
39
69
  - Gemfile
@@ -67,21 +97,6 @@ files:
67
97
  - doc/rdoc.css
68
98
  - lib/wpb.rb
69
99
  - lib/wpb/bash.rb
70
- - lib/wpb/clamp/attribute.rb
71
- - lib/wpb/clamp/attribute_declaration.rb
72
- - lib/wpb/clamp/command.rb
73
- - lib/wpb/clamp/errors.rb
74
- - lib/wpb/clamp/help.rb
75
- - lib/wpb/clamp/option.rb
76
- - lib/wpb/clamp/option/declaration.rb
77
- - lib/wpb/clamp/option/parsing.rb
78
- - lib/wpb/clamp/parameter.rb
79
- - lib/wpb/clamp/parameter/declaration.rb
80
- - lib/wpb/clamp/parameter/parsing.rb
81
- - lib/wpb/clamp/subcommand.rb
82
- - lib/wpb/clamp/subcommand/declaration.rb
83
- - lib/wpb/clamp/subcommand/execution.rb
84
- - lib/wpb/clamp/version.rb
85
100
  - lib/wpb/comment.rb
86
101
  - lib/wpb/page.rb
87
102
  - lib/wpb/pagepost.rb
@@ -90,33 +105,28 @@ files:
90
105
  - lib/wpb/user.rb
91
106
  - lib/wpb/version.rb
92
107
  - wpb.gemspec
93
- has_rdoc: true
94
- homepage: http://github.com/jamesbirtles/wpb
108
+ homepage: http://jamesbirtles.org/wpb
95
109
  licenses: []
96
-
97
110
  post_install_message:
98
111
  rdoc_options: []
99
-
100
- require_paths:
112
+ require_paths:
101
113
  - lib
102
- required_ruby_version: !ruby/object:Gem::Requirement
114
+ required_ruby_version: !ruby/object:Gem::Requirement
103
115
  none: false
104
- requirements:
105
- - - ">="
106
- - !ruby/object:Gem::Version
107
- version: "0"
108
- required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
121
  none: false
110
- requirements:
111
- - - ">="
112
- - !ruby/object:Gem::Version
113
- version: "0"
122
+ requirements:
123
+ - - ! '>'
124
+ - !ruby/object:Gem::Version
125
+ version: 1.3.1
114
126
  requirements: []
115
-
116
127
  rubyforge_project:
117
- rubygems_version: 1.6.2
128
+ rubygems_version: 1.8.10
118
129
  signing_key:
119
130
  specification_version: 3
120
131
  summary: A Terminal interface to WordPress
121
132
  test_files: []
122
-
@@ -1,37 +0,0 @@
1
- module Clamp
2
-
3
- class Attribute
4
-
5
- attr_reader :description, :attribute_name, :default_value
6
-
7
- def help_rhs
8
- rhs = description
9
- if defined?(@default_value)
10
- rhs += " (default: #{@default_value.inspect})"
11
- end
12
- rhs
13
- end
14
-
15
- def help
16
- [help_lhs, help_rhs]
17
- end
18
-
19
- def ivar_name
20
- "@#{attribute_name}"
21
- end
22
-
23
- def read_method
24
- attribute_name
25
- end
26
-
27
- def default_method
28
- "default_#{read_method}"
29
- end
30
-
31
- def write_method
32
- "#{attribute_name}="
33
- end
34
-
35
- end
36
-
37
- end
@@ -1,40 +0,0 @@
1
- module Clamp
2
-
3
- module AttributeDeclaration
4
-
5
- protected
6
-
7
- def define_accessors_for(attribute, &block)
8
- define_reader_for(attribute)
9
- define_default_for(attribute)
10
- define_writer_for(attribute, &block)
11
- end
12
-
13
- def define_reader_for(attribute)
14
- define_method(attribute.read_method) do
15
- if instance_variable_defined?(attribute.ivar_name)
16
- instance_variable_get(attribute.ivar_name)
17
- elsif respond_to?(attribute.default_method)
18
- send(attribute.default_method)
19
- end
20
- end
21
- end
22
-
23
- def define_default_for(attribute)
24
- define_method(attribute.default_method) do
25
- attribute.default_value
26
- end
27
- end
28
-
29
- def define_writer_for(attribute, &block)
30
- define_method(attribute.write_method) do |value|
31
- if block
32
- value = instance_exec(value, &block)
33
- end
34
- instance_variable_set(attribute.ivar_name, value)
35
- end
36
- end
37
-
38
- end
39
-
40
- end
@@ -1,137 +0,0 @@
1
- require 'clamp/errors'
2
- require 'clamp/help'
3
- require 'clamp/option/declaration'
4
- require 'clamp/option/parsing'
5
- require 'clamp/parameter/declaration'
6
- require 'clamp/parameter/parsing'
7
- require 'clamp/subcommand/declaration'
8
- require 'clamp/subcommand/execution'
9
-
10
- module Clamp
11
-
12
- # {Command} models a shell command. Each command invocation is a new object.
13
- # Command options and parameters are represented as attributes
14
- # (see {Command::Declaration}).
15
- #
16
- # The main entry-point is {#run}, which uses {#parse} to populate attributes based
17
- # on an array of command-line arguments, then calls {#execute} (which you provide)
18
- # to make it go.
19
- #
20
- class Command
21
-
22
- # Create a command execution.
23
- #
24
- # @param [String] invocation_path the path used to invoke the command
25
- # @param [Hash] context additional data the command may need
26
- #
27
- def initialize(invocation_path, context = {})
28
- @invocation_path = invocation_path
29
- @context = context
30
- end
31
-
32
- # @return [String] the path used to invoke this command
33
- #
34
- attr_reader :invocation_path
35
-
36
- # @return [Array<String>] unconsumed command-line arguments
37
- #
38
- def remaining_arguments
39
- @remaining_arguments
40
- end
41
-
42
- # Parse command-line arguments.
43
- #
44
- # @param [Array<String>] arguments command-line arguments
45
- # @return [Array<String>] unconsumed arguments
46
- #
47
- def parse(arguments)
48
- @remaining_arguments = arguments.dup
49
- parse_options
50
- parse_parameters
51
- @remaining_arguments
52
- end
53
-
54
- # Run the command, with the specified arguments.
55
- #
56
- # This calls {#parse} to process the command-line arguments,
57
- # then delegates to {#execute}.
58
- #
59
- # @param [Array<String>] arguments command-line arguments
60
- #
61
- def run(arguments)
62
- parse(arguments)
63
- execute
64
- execute_subcommands
65
- end
66
-
67
- # Execute the command (assuming that all options/parameters have been set).
68
- #
69
- # This method is designed to be overridden in sub-classes.
70
- #
71
- def execute
72
- raise "you need to define #execute"
73
- end
74
-
75
- def execute_subcommands
76
- if self.class.has_subcommands?
77
- execute_subcommand
78
- end
79
- end
80
-
81
- # @return [String] usage documentation for this command
82
- #
83
- def help
84
- self.class.help(invocation_path)
85
- end
86
-
87
- include Clamp::Option::Parsing
88
- include Clamp::Parameter::Parsing
89
- include Clamp::Subcommand::Execution
90
-
91
- protected
92
-
93
- attr_accessor :context
94
-
95
- private
96
-
97
- def signal_usage_error(message)
98
- e = UsageError.new(message, self)
99
- e.set_backtrace(caller)
100
- raise e
101
- end
102
-
103
- def help_requested=(value)
104
- raise Clamp::HelpWanted.new(self)
105
- end
106
-
107
- class << self
108
-
109
- include Clamp::Option::Declaration
110
- include Clamp::Parameter::Declaration
111
- include Clamp::Subcommand::Declaration
112
- include Help
113
-
114
- # Create an instance of this command class, and run it.
115
- #
116
- # @param [String] invocation_path the path used to invoke the command
117
- # @param [Array<String>] arguments command-line arguments
118
- # @param [Hash] context additional data the command may need
119
- #
120
- def run(invocation_path = File.basename($0), arguments = ARGV, context = {})
121
- begin
122
- new(invocation_path, context).run(arguments)
123
- rescue Clamp::UsageError => e
124
- $stderr.puts "ERROR: #{e.message}"
125
- $stderr.puts ""
126
- $stderr.puts "See: '#{e.command.invocation_path} --help'"
127
- exit(1)
128
- rescue Clamp::HelpWanted => e
129
- puts e.command.help
130
- end
131
- end
132
-
133
- end
134
-
135
- end
136
-
137
- end
@@ -1,26 +0,0 @@
1
- module Clamp
2
-
3
- class Error < StandardError
4
-
5
- def initialize(message, command)
6
- super(message)
7
- @command = command
8
- end
9
-
10
- attr_reader :command
11
-
12
- end
13
-
14
- # raise to signal incorrect command usage
15
- class UsageError < Error; end
16
-
17
- # raise to request usage help
18
- class HelpWanted < Error
19
-
20
- def initialize(command)
21
- super("I need help", command)
22
- end
23
-
24
- end
25
-
26
- end
@@ -1,69 +0,0 @@
1
- require 'stringio'
2
-
3
- module Clamp
4
-
5
- module Help
6
-
7
- def usage(usage)
8
- @declared_usage_descriptions ||= []
9
- @declared_usage_descriptions << usage
10
- end
11
-
12
- attr_reader :declared_usage_descriptions
13
-
14
- def description=(description)
15
- @description = description.dup
16
- if @description =~ /^\A\n*( +)/
17
- indent = $1
18
- @description.gsub!(/^#{indent}/, '')
19
- end
20
- @description.strip!
21
- end
22
-
23
- attr_reader :description
24
-
25
- def derived_usage_description
26
- parts = parameters.map { |a| a.name }
27
- parts.unshift("[OPTIONS]") if has_options?
28
- parts.join(" ")
29
- end
30
-
31
- def usage_descriptions
32
- declared_usage_descriptions || [derived_usage_description]
33
- end
34
-
35
- def help(invocation_path)
36
- help = StringIO.new
37
- help.puts "Usage:"
38
- usage_descriptions.each_with_index do |usage, i|
39
- help.puts " #{invocation_path} #{usage}".rstrip
40
- end
41
- if description
42
- help.puts ""
43
- help.puts description.gsub(/^/, " ")
44
- end
45
- detail_format = " %-29s %s"
46
- if has_parameters?
47
- help.puts "\nParameters:"
48
- parameters.each do |parameter|
49
- help.puts detail_format % parameter.help
50
- end
51
- end
52
- if has_subcommands?
53
- help.puts "\nSubcommands:"
54
- recognised_subcommands.each do |subcommand|
55
- help.puts detail_format % subcommand.help
56
- end
57
- end
58
- if has_options?
59
- help.puts "\nOptions:"
60
- recognised_options.each do |option|
61
- help.puts detail_format % option.help
62
- end
63
- end
64
- help.string
65
- end
66
-
67
- end
68
-
69
- end
@@ -1,58 +0,0 @@
1
- require 'clamp/attribute_declaration'
2
- require 'clamp/option'
3
-
4
- module Clamp
5
- class Option
6
-
7
- module Declaration
8
-
9
- include Clamp::AttributeDeclaration
10
-
11
- def option(switches, type, description, opts = {}, &block)
12
- option = Clamp::Option.new(switches, type, description, opts)
13
- declared_options << option
14
- define_accessors_for(option, &block)
15
- end
16
-
17
- def has_options?
18
- !documented_options.empty?
19
- end
20
-
21
- def find_option(switch)
22
- recognised_options.find { |o| o.handles?(switch) }
23
- end
24
-
25
- def declared_options
26
- @declared_options ||= []
27
- end
28
-
29
- def documented_options
30
- declared_options + inherited_declared_options
31
- end
32
-
33
- def recognised_options
34
- documented_options + standard_options
35
- end
36
-
37
- private
38
-
39
- def inherited_declared_options
40
- ancestors.inject([]) do |options, ancestor|
41
- if ancestor.kind_of?(Clamp::Option::Declaration)
42
- options + ancestor.declared_options
43
- else
44
- options
45
- end
46
- end
47
- end
48
-
49
- HELP_OPTION = Clamp::Option.new("--help", :flag, "print help", :attribute_name => :help_requested)
50
-
51
- def standard_options
52
- [HELP_OPTION]
53
- end
54
-
55
- end
56
-
57
- end
58
- end
@@ -1,45 +0,0 @@
1
- module Clamp
2
- class Option
3
-
4
- module Parsing
5
-
6
- protected
7
-
8
- def parse_options
9
- while remaining_arguments.first =~ /^-/
10
-
11
- switch = remaining_arguments.shift
12
- break if switch == "--"
13
-
14
- case switch
15
- when /^(-\w)(.+)$/ # combined short options
16
- switch = $1
17
- remaining_arguments.unshift("-#{$2}")
18
- when /^(--[^=]+)=(.*)/
19
- switch = $1
20
- remaining_arguments.unshift($2)
21
- end
22
-
23
- option = find_option(switch)
24
- value = option.extract_value(switch, remaining_arguments)
25
-
26
- begin
27
- send("#{option.attribute_name}=", value)
28
- rescue ArgumentError => e
29
- signal_usage_error "option '#{switch}': #{e.message}"
30
- end
31
-
32
- end
33
- end
34
-
35
- private
36
-
37
- def find_option(switch)
38
- self.class.find_option(switch) ||
39
- signal_usage_error("Unrecognised option '#{switch}'")
40
- end
41
-
42
- end
43
-
44
- end
45
- end
@@ -1,77 +0,0 @@
1
- require 'clamp/attribute'
2
-
3
- module Clamp
4
-
5
- class Option < Attribute
6
-
7
- def initialize(switches, type, description, options = {})
8
- @switches = Array(switches)
9
- @type = type
10
- @description = description
11
- if options.has_key?(:attribute_name)
12
- @attribute_name = options[:attribute_name].to_s
13
- end
14
- if options.has_key?(:default)
15
- @default_value = options[:default]
16
- end
17
- end
18
-
19
- attr_reader :switches, :type
20
-
21
- def attribute_name
22
- @attribute_name ||= long_switch.sub(/^--(\[no-\])?/, '').tr('-', '_')
23
- end
24
-
25
- def long_switch
26
- switches.find { |switch| switch =~ /^--/ }
27
- end
28
-
29
- def handles?(switch)
30
- recognised_switches.member?(switch)
31
- end
32
-
33
- def flag?
34
- @type == :flag
35
- end
36
-
37
- def flag_value(switch)
38
- !(switch =~ /^--no-(.*)/ && switches.member?("--\[no-\]#{$1}"))
39
- end
40
-
41
- def read_method
42
- if flag?
43
- super + "?"
44
- else
45
- super
46
- end
47
- end
48
-
49
- def extract_value(switch, arguments)
50
- if flag?
51
- flag_value(switch)
52
- else
53
- arguments.shift
54
- end
55
- end
56
-
57
- def help_lhs
58
- lhs = switches.join(", ")
59
- lhs += " " + type unless flag?
60
- lhs
61
- end
62
-
63
- private
64
-
65
- def recognised_switches
66
- switches.map do |switch|
67
- if switch =~ /^--\[no-\](.*)/
68
- ["--#{$1}", "--no-#{$1}"]
69
- else
70
- switch
71
- end
72
- end.flatten
73
- end
74
-
75
- end
76
-
77
- end
@@ -1,28 +0,0 @@
1
- require 'clamp/attribute_declaration'
2
- require 'clamp/parameter'
3
-
4
- module Clamp
5
- class Parameter
6
-
7
- module Declaration
8
-
9
- include Clamp::AttributeDeclaration
10
-
11
- def parameters
12
- @parameters ||= []
13
- end
14
-
15
- def has_parameters?
16
- !parameters.empty?
17
- end
18
-
19
- def parameter(name, description, options = {}, &block)
20
- parameter = Parameter.new(name, description, options)
21
- parameters << parameter
22
- define_accessors_for(parameter, &block)
23
- end
24
-
25
- end
26
-
27
- end
28
- end
@@ -1,28 +0,0 @@
1
- module Clamp
2
- class Parameter
3
-
4
- module Parsing
5
-
6
- protected
7
-
8
- def parse_parameters
9
-
10
- self.class.parameters.each do |parameter|
11
- begin
12
- value = parameter.consume(remaining_arguments)
13
- send("#{parameter.attribute_name}=", value) unless value.nil?
14
- rescue ArgumentError => e
15
- signal_usage_error "parameter '#{parameter.name}': #{e.message}"
16
- end
17
- end
18
-
19
- unless remaining_arguments.empty?
20
- signal_usage_error "too many arguments"
21
- end
22
-
23
- end
24
-
25
- end
26
-
27
- end
28
- end
@@ -1,70 +0,0 @@
1
- require 'clamp/attribute'
2
-
3
- module Clamp
4
-
5
- class Parameter < Attribute
6
-
7
- def initialize(name, description, options = {})
8
- @name = name
9
- @description = description
10
- infer_attribute_name_and_multiplicity
11
- if options.has_key?(:attribute_name)
12
- @attribute_name = options[:attribute_name].to_s
13
- end
14
- if options.has_key?(:default)
15
- @default_value = options[:default]
16
- end
17
- end
18
-
19
- attr_reader :name, :attribute_name
20
-
21
- def help_lhs
22
- name
23
- end
24
-
25
- def consume(arguments)
26
- if required? && arguments.empty?
27
- raise ArgumentError, "no value provided"
28
- end
29
- if multivalued?
30
- arguments.shift(arguments.length)
31
- else
32
- arguments.shift
33
- end
34
- end
35
-
36
- private
37
-
38
- NAME_PATTERN = "([A-Za-z0-9_-]+)"
39
-
40
- def infer_attribute_name_and_multiplicity
41
- case @name
42
- when /^\[#{NAME_PATTERN}\]$/
43
- @attribute_name = $1
44
- when /^\[#{NAME_PATTERN}\] ...$/
45
- @attribute_name = "#{$1}_list"
46
- @multivalued = true
47
- when /^#{NAME_PATTERN} ...$/
48
- @attribute_name = "#{$1}_list"
49
- @multivalued = true
50
- @required = true
51
- when /^#{NAME_PATTERN}$/
52
- @attribute_name = @name
53
- @required = true
54
- else
55
- raise "invalid parameter name: '#{name}'"
56
- end
57
- @attribute_name = @attribute_name.downcase.tr('-', '_')
58
- end
59
-
60
- def multivalued?
61
- @multivalued
62
- end
63
-
64
- def required?
65
- @required
66
- end
67
-
68
- end
69
-
70
- end
@@ -1,40 +0,0 @@
1
- require 'clamp/subcommand'
2
-
3
- module Clamp
4
- class Subcommand
5
-
6
- module Declaration
7
-
8
- def recognised_subcommands
9
- @recognised_subcommands ||= []
10
- end
11
-
12
- def subcommand(name, description, subcommand_class = self, &block)
13
- has_subcommands!
14
- if block
15
- # generate a anonymous sub-class
16
- subcommand_class = Class.new(subcommand_class, &block)
17
- end
18
- recognised_subcommands << Subcommand.new(name, description, subcommand_class)
19
- end
20
-
21
- def has_subcommands?
22
- !recognised_subcommands.empty?
23
- end
24
-
25
- def find_subcommand(name)
26
- recognised_subcommands.find { |sc| sc.is_called?(name) }
27
- end
28
-
29
- def has_subcommands!
30
- unless @has_subcommands
31
- parameter "[SUBCOMMAND]", "subcommand name", :attribute_name => :subcommand_name
32
- parameter "[ARGS] ...", "subcommand arguments", :attribute_name => :subcommand_arguments
33
- @has_subcommands = true
34
- end
35
- end
36
-
37
- end
38
-
39
- end
40
- end
@@ -1,37 +0,0 @@
1
- module Clamp
2
- class Subcommand
3
-
4
- module Execution
5
-
6
- protected
7
-
8
- def execute_subcommand
9
- if subcommand_name
10
- subcommand_class = find_subcommand_class(subcommand_name)
11
- subcommand = subcommand_class.new("#{invocation_path} #{subcommand_name}", context)
12
- self.class.recognised_options.each do |option|
13
- option_set = instance_variable_defined?(option.ivar_name)
14
- if option_set && subcommand.respond_to?(option.write_method)
15
- subcommand.send(option.write_method, self.send(option.read_method))
16
- end
17
- end
18
- subcommand.run(subcommand_arguments)
19
- end
20
- end
21
-
22
- private
23
-
24
- def find_subcommand(name)
25
- self.class.find_subcommand(name) ||
26
- signal_usage_error("No such sub-command '#{name}'")
27
- end
28
-
29
- def find_subcommand_class(name)
30
- subcommand = find_subcommand(name)
31
- subcommand.subcommand_class if subcommand
32
- end
33
-
34
- end
35
-
36
- end
37
- end
@@ -1,23 +0,0 @@
1
- module Clamp
2
-
3
- class Subcommand < Struct.new(:name, :description, :subcommand_class)
4
-
5
- def initialize(names, description, subcommand_class)
6
- @names = Array(names)
7
- @description = description
8
- @subcommand_class = subcommand_class
9
- end
10
-
11
- attr_reader :names, :description, :subcommand_class
12
-
13
- def is_called?(name)
14
- names.member?(name)
15
- end
16
-
17
- def help
18
- [names.join(", "), description]
19
- end
20
-
21
- end
22
-
23
- end
@@ -1,3 +0,0 @@
1
- module Clamp
2
- VERSION = "0.1.9.dev".freeze
3
- end