coglius 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Cogfile +15 -0
- data/LICENSE +201 -0
- data/lib/coglius.rb +29 -0
- data/lib/coglius/app.rb +250 -0
- data/lib/coglius/app_support.rb +284 -0
- data/lib/coglius/command.rb +149 -0
- data/lib/coglius/command_line_option.rb +34 -0
- data/lib/coglius/command_line_token.rb +62 -0
- data/lib/coglius/command_support.rb +214 -0
- data/lib/coglius/commands/compound_command.rb +42 -0
- data/lib/coglius/commands/doc.rb +215 -0
- data/lib/coglius/commands/help.rb +73 -0
- data/lib/coglius/commands/help_modules/arg_name_formatter.rb +20 -0
- data/lib/coglius/commands/help_modules/command_finder.rb +60 -0
- data/lib/coglius/commands/help_modules/command_help_format.rb +138 -0
- data/lib/coglius/commands/help_modules/global_help_format.rb +70 -0
- data/lib/coglius/commands/help_modules/help_completion_format.rb +31 -0
- data/lib/coglius/commands/help_modules/list_formatter.rb +23 -0
- data/lib/coglius/commands/help_modules/one_line_wrapper.rb +18 -0
- data/lib/coglius/commands/help_modules/options_formatter.rb +49 -0
- data/lib/coglius/commands/help_modules/text_wrapper.rb +53 -0
- data/lib/coglius/commands/help_modules/tty_only_wrapper.rb +23 -0
- data/lib/coglius/commands/help_modules/verbatim_wrapper.rb +16 -0
- data/lib/coglius/commands/initconfig.rb +69 -0
- data/lib/coglius/commands/rdoc_document_listener.rb +116 -0
- data/lib/coglius/commands/scaffold.rb +401 -0
- data/lib/coglius/copy_options_to_aliases.rb +33 -0
- data/lib/coglius/dsl.rb +221 -0
- data/lib/coglius/exceptions.rb +54 -0
- data/lib/coglius/flag.rb +68 -0
- data/lib/coglius/gli_option_parser.rb +124 -0
- data/lib/coglius/option_parser_factory.rb +45 -0
- data/lib/coglius/options.rb +23 -0
- data/lib/coglius/switch.rb +35 -0
- data/lib/coglius/terminal.rb +94 -0
- data/lib/coglius/version.rb +5 -0
- data/templates/coglius/generator.rb.erb +26 -0
- metadata +208 -0
@@ -0,0 +1,45 @@
|
|
1
|
+
module Coglius
|
2
|
+
# Factory for creating an OptionParser based on app configuration and DSL calls
|
3
|
+
class OptionParserFactory
|
4
|
+
# Create an OptionParserFactory for the given
|
5
|
+
# flags, switches, and accepts
|
6
|
+
def initialize(flags,switches,accepts)
|
7
|
+
@flags = flags
|
8
|
+
@switches = switches
|
9
|
+
@accepts = accepts
|
10
|
+
end
|
11
|
+
|
12
|
+
# Return an option parser to parse the given flags, switches and accepts
|
13
|
+
def option_parser
|
14
|
+
options = {}
|
15
|
+
option_parser = OptionParser.new do |opts|
|
16
|
+
self.class.setup_accepts(opts,@accepts)
|
17
|
+
self.class.setup_options(opts,@switches,options)
|
18
|
+
self.class.setup_options(opts,@flags,options)
|
19
|
+
end
|
20
|
+
[option_parser,options]
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def self.setup_accepts(opts,accepts)
|
26
|
+
accepts.each do |object,block|
|
27
|
+
opts.accept(object) do |arg_as_string|
|
28
|
+
block.call(arg_as_string)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.setup_options(opts,tokens,options)
|
34
|
+
tokens.each do |ignore,token|
|
35
|
+
opts.on(*token.arguments_for_option_parser) do |arg|
|
36
|
+
[token.name,token.aliases].flatten.compact.map(&:to_s).each do |name|
|
37
|
+
options[name] = arg
|
38
|
+
options[name.to_sym] = arg
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
module Coglius
|
4
|
+
# Subclass of +OpenStruct+ that provides hash-like methods for #[] and #[]=. Note that is is *not* a Hash.
|
5
|
+
# By using Coglius::App#use_openstruct, your options will be coerced into one of these.
|
6
|
+
class Options < OpenStruct
|
7
|
+
|
8
|
+
# Return the value of an attribute
|
9
|
+
def [](k)
|
10
|
+
@table[k.to_sym]
|
11
|
+
end
|
12
|
+
|
13
|
+
# Set the value of an attribute
|
14
|
+
def []=(k, v)
|
15
|
+
@table[k.to_sym] = v
|
16
|
+
end
|
17
|
+
|
18
|
+
def map(&block)
|
19
|
+
@table.map(&block)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'coglius/command_line_option.rb'
|
2
|
+
|
3
|
+
module Coglius
|
4
|
+
# Defines a command line switch
|
5
|
+
class Switch < CommandLineOption #:nodoc:
|
6
|
+
|
7
|
+
attr_accessor :default_value
|
8
|
+
attr_reader :negatable
|
9
|
+
|
10
|
+
# Creates a new switch
|
11
|
+
#
|
12
|
+
# names - Array of symbols or strings representing the names of this switch
|
13
|
+
# options - hash of options:
|
14
|
+
# :desc - the short description
|
15
|
+
# :long_desc - the long description
|
16
|
+
# :negatable - true or false if this switch is negatable; defaults to true
|
17
|
+
# :default_value - default value if the switch is omitted
|
18
|
+
def initialize(names,options = {})
|
19
|
+
super(names,options)
|
20
|
+
@default_value = false if options[:default_value].nil?
|
21
|
+
@negatable = options[:negatable].nil? ? true : options[:negatable]
|
22
|
+
if @default_value != false && @negatable == false
|
23
|
+
raise "A switch with default #{@default_value} that isn't negetable is useless"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def arguments_for_option_parser
|
28
|
+
all_forms_a
|
29
|
+
end
|
30
|
+
|
31
|
+
def negatable?
|
32
|
+
@negatable
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
module Coglius
|
2
|
+
# Class to encapsulate stuff about the terminal. This is useful to application developers
|
3
|
+
# as a canonical means to get information about the user's current terminal configuraiton.
|
4
|
+
# Coglius uses this to determine the number of columns to use when printing to the screen.
|
5
|
+
#
|
6
|
+
# To access it, use Terminal.instance. This is a singleton mostly to facilitate testing, but
|
7
|
+
# it seems reasonable enough, since there's only one terminal in effect
|
8
|
+
#
|
9
|
+
# Example:
|
10
|
+
#
|
11
|
+
# Terminal.instance.size[0] # => columns in the terminal
|
12
|
+
# Terminal.default_size = [128,24] # => change default when we can't figure it out
|
13
|
+
# raise "no ls?!?!?" unless Terminal.instance.command_exists?("ls")
|
14
|
+
#
|
15
|
+
class Terminal
|
16
|
+
|
17
|
+
@@default_size = [80,24]
|
18
|
+
|
19
|
+
# Get the default size of the terminal when we can't figure it out
|
20
|
+
#
|
21
|
+
# Returns an array of int [cols,rows]
|
22
|
+
def self.default_size
|
23
|
+
@@default_size
|
24
|
+
end
|
25
|
+
|
26
|
+
# Set the default size of the terminal to use when we can't figure it out
|
27
|
+
#
|
28
|
+
# +size+:: array of two int [cols,rows]
|
29
|
+
def self.default_size=(size)
|
30
|
+
@@default_size = size
|
31
|
+
end
|
32
|
+
|
33
|
+
# Provide access to the shared instance.
|
34
|
+
def self.instance; @@instance ||= Terminal.new; end
|
35
|
+
|
36
|
+
# Call this to cause methods to throw exceptions rather than return a sane default. You
|
37
|
+
# probably don't want to call this unless you are writing tests
|
38
|
+
def make_unsafe! #:nodoc:
|
39
|
+
@unsafe = true
|
40
|
+
end
|
41
|
+
|
42
|
+
# Returns true if the given command exists on this system
|
43
|
+
#
|
44
|
+
# +command+:: The command, as a String, to check for, without any path information.
|
45
|
+
def self.command_exists?(command)
|
46
|
+
ENV['PATH'].split(File::PATH_SEPARATOR).any? {|dir| File.exists? File.join(dir, command) }
|
47
|
+
end
|
48
|
+
|
49
|
+
def command_exists?(command)
|
50
|
+
self.class.command_exists?(command)
|
51
|
+
end
|
52
|
+
|
53
|
+
SIZE_DETERMINERS = [
|
54
|
+
[
|
55
|
+
lambda { (ENV['COLUMNS'] =~ /^\d+$/) && (ENV['LINES'] =~ /^\d+$/) },
|
56
|
+
lambda { [ENV['COLUMNS'].to_i, ENV['LINES'].to_i] }
|
57
|
+
],
|
58
|
+
[
|
59
|
+
lambda { (jruby? || (!STDIN.tty? && ENV['TERM'])) && command_exists?('tput') },
|
60
|
+
lambda { [run_command('tput cols').to_i, run_command('tput lines').to_i] }
|
61
|
+
],
|
62
|
+
[
|
63
|
+
lambda { STDIN.tty? && command_exists?('stty') },
|
64
|
+
lambda { run_command('stty size').scan(/\d+/).map { |size_element| size_element.to_i }.reverse }
|
65
|
+
],
|
66
|
+
[
|
67
|
+
lambda { true },
|
68
|
+
lambda { Terminal.default_size },
|
69
|
+
],
|
70
|
+
]
|
71
|
+
|
72
|
+
# Get the size of the current terminal.
|
73
|
+
# Ripped from hirb[https://github.com/cldwalker/hirb/blob/master/lib/hirb/util.rb]
|
74
|
+
#
|
75
|
+
# Returns an Array of size two Ints representing the terminal width and height
|
76
|
+
def size
|
77
|
+
SIZE_DETERMINERS.select { |(predicate,ignore)| predicate.call }.first[1].call
|
78
|
+
rescue Exception => ex
|
79
|
+
raise ex if @unsafe
|
80
|
+
Terminal.default_size
|
81
|
+
end
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
# Runs a command using backticks. Extracted to allow for testing
|
86
|
+
def self.run_command(command)
|
87
|
+
`#{command}`
|
88
|
+
end
|
89
|
+
|
90
|
+
# True if we are JRuby; exposed to allow for testing
|
91
|
+
def self.jruby?; RUBY_PLATFORM =~ /java/; end
|
92
|
+
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
extend Coglius::App
|
2
|
+
|
3
|
+
program_desc 'Describe your application here'
|
4
|
+
|
5
|
+
version '0.0.1'
|
6
|
+
|
7
|
+
desc 'Describe some switch here'
|
8
|
+
switch [:s,:switch]
|
9
|
+
|
10
|
+
desc 'Describe some flag here'
|
11
|
+
default_value 'the default'
|
12
|
+
arg_name 'The name of the argument'
|
13
|
+
flag [:f,:flagname]
|
14
|
+
|
15
|
+
desc 'Describe my_command here'
|
16
|
+
arg_name 'Describe arguments to my_command here'
|
17
|
+
command :my_command do |c|
|
18
|
+
c.desc 'Describe a switch to my_command'
|
19
|
+
c.switch :s
|
20
|
+
|
21
|
+
c.desc 'Describe a flag to my_command'
|
22
|
+
c.default_value 'default'
|
23
|
+
c.flag :f
|
24
|
+
end
|
25
|
+
|
26
|
+
stamp_app
|
metadata
ADDED
@@ -0,0 +1,208 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: coglius
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Kevin Tonon
|
14
|
+
- David Copeland
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2012-12-30 00:00:00 Z
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 11
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
- 9
|
31
|
+
- 2
|
32
|
+
- 2
|
33
|
+
version: 0.9.2.2
|
34
|
+
version_requirements: *id001
|
35
|
+
name: rake
|
36
|
+
prerelease: false
|
37
|
+
type: :development
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 17
|
45
|
+
segments:
|
46
|
+
- 3
|
47
|
+
- 11
|
48
|
+
version: "3.11"
|
49
|
+
version_requirements: *id002
|
50
|
+
name: rdoc
|
51
|
+
prerelease: false
|
52
|
+
type: :development
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ~>
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 17
|
60
|
+
segments:
|
61
|
+
- 1
|
62
|
+
- 1
|
63
|
+
- 1
|
64
|
+
version: 1.1.1
|
65
|
+
version_requirements: *id003
|
66
|
+
name: rainbow
|
67
|
+
prerelease: false
|
68
|
+
type: :development
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
hash: 3
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
version: "0"
|
79
|
+
version_requirements: *id004
|
80
|
+
name: clean_test
|
81
|
+
prerelease: false
|
82
|
+
type: :development
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
hash: 3
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
version: "0"
|
93
|
+
version_requirements: *id005
|
94
|
+
name: aruba
|
95
|
+
prerelease: false
|
96
|
+
type: :development
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
hash: 3
|
104
|
+
segments:
|
105
|
+
- 0
|
106
|
+
version: "0"
|
107
|
+
version_requirements: *id006
|
108
|
+
name: sdoc
|
109
|
+
prerelease: false
|
110
|
+
type: :development
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - "="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
hash: 23
|
118
|
+
segments:
|
119
|
+
- 1
|
120
|
+
- 0
|
121
|
+
- 0
|
122
|
+
version: 1.0.0
|
123
|
+
version_requirements: *id007
|
124
|
+
name: faker
|
125
|
+
prerelease: false
|
126
|
+
type: :development
|
127
|
+
description: Build command-suite CLI apps that are awesome. Bootstrap your app, add commands, options and documentation while maintaining a well-tested idiomatic command-line app
|
128
|
+
email: kevin@betweenconcepts.com
|
129
|
+
executables: []
|
130
|
+
|
131
|
+
extensions: []
|
132
|
+
|
133
|
+
extra_rdoc_files: []
|
134
|
+
|
135
|
+
files:
|
136
|
+
- Cogfile
|
137
|
+
- LICENSE
|
138
|
+
- templates/coglius/generator.rb.erb
|
139
|
+
- lib/coglius/app.rb
|
140
|
+
- lib/coglius/app_support.rb
|
141
|
+
- lib/coglius/command.rb
|
142
|
+
- lib/coglius/command_line_option.rb
|
143
|
+
- lib/coglius/command_line_token.rb
|
144
|
+
- lib/coglius/command_support.rb
|
145
|
+
- lib/coglius/commands/compound_command.rb
|
146
|
+
- lib/coglius/commands/doc.rb
|
147
|
+
- lib/coglius/commands/help.rb
|
148
|
+
- lib/coglius/commands/help_modules/arg_name_formatter.rb
|
149
|
+
- lib/coglius/commands/help_modules/command_finder.rb
|
150
|
+
- lib/coglius/commands/help_modules/command_help_format.rb
|
151
|
+
- lib/coglius/commands/help_modules/global_help_format.rb
|
152
|
+
- lib/coglius/commands/help_modules/help_completion_format.rb
|
153
|
+
- lib/coglius/commands/help_modules/list_formatter.rb
|
154
|
+
- lib/coglius/commands/help_modules/one_line_wrapper.rb
|
155
|
+
- lib/coglius/commands/help_modules/options_formatter.rb
|
156
|
+
- lib/coglius/commands/help_modules/text_wrapper.rb
|
157
|
+
- lib/coglius/commands/help_modules/tty_only_wrapper.rb
|
158
|
+
- lib/coglius/commands/help_modules/verbatim_wrapper.rb
|
159
|
+
- lib/coglius/commands/initconfig.rb
|
160
|
+
- lib/coglius/commands/rdoc_document_listener.rb
|
161
|
+
- lib/coglius/commands/scaffold.rb
|
162
|
+
- lib/coglius/copy_options_to_aliases.rb
|
163
|
+
- lib/coglius/dsl.rb
|
164
|
+
- lib/coglius/exceptions.rb
|
165
|
+
- lib/coglius/flag.rb
|
166
|
+
- lib/coglius/gli_option_parser.rb
|
167
|
+
- lib/coglius/option_parser_factory.rb
|
168
|
+
- lib/coglius/options.rb
|
169
|
+
- lib/coglius/switch.rb
|
170
|
+
- lib/coglius/terminal.rb
|
171
|
+
- lib/coglius/version.rb
|
172
|
+
- lib/coglius.rb
|
173
|
+
homepage:
|
174
|
+
licenses: []
|
175
|
+
|
176
|
+
post_install_message:
|
177
|
+
rdoc_options: []
|
178
|
+
|
179
|
+
require_paths:
|
180
|
+
- lib
|
181
|
+
- lib
|
182
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
183
|
+
none: false
|
184
|
+
requirements:
|
185
|
+
- - ">="
|
186
|
+
- !ruby/object:Gem::Version
|
187
|
+
hash: 3
|
188
|
+
segments:
|
189
|
+
- 0
|
190
|
+
version: "0"
|
191
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
192
|
+
none: false
|
193
|
+
requirements:
|
194
|
+
- - ">="
|
195
|
+
- !ruby/object:Gem::Version
|
196
|
+
hash: 3
|
197
|
+
segments:
|
198
|
+
- 0
|
199
|
+
version: "0"
|
200
|
+
requirements: []
|
201
|
+
|
202
|
+
rubyforge_project:
|
203
|
+
rubygems_version: 1.8.24
|
204
|
+
signing_key:
|
205
|
+
specification_version: 3
|
206
|
+
summary: Not ready for use.
|
207
|
+
test_files: []
|
208
|
+
|