remi-simplecli 0.1.5 → 0.1.6
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/LICENSE +22 -0
- data/README.rdoc +133 -0
- data/Rakefile +57 -0
- data/VERSION.yml +4 -0
- data/lib/simplecli.rb +46 -5
- metadata +12 -16
- data/README +0 -118
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2009 ryan "remi" Taylor
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
= SimpleCLI
|
2
|
+
|
3
|
+
Super Simple RubyGems-like CLI
|
4
|
+
|
5
|
+
SimpleCLI gives you a stupidly simple way to implement command-line
|
6
|
+
interfaces like that of RubyGems with a basic interface like:
|
7
|
+
|
8
|
+
gem command [options]
|
9
|
+
|
10
|
+
SimpleCLI gives you a way of defining your commands (or actions) so
|
11
|
+
they'll automatically show up when you run `yourapp commands`
|
12
|
+
|
13
|
+
SimpleCLI also makes it really easy to add documentation to each of
|
14
|
+
your commands (or actions)
|
15
|
+
|
16
|
+
== Real Examples
|
17
|
+
|
18
|
+
I use SimpleCLI in most of my apps for quick and dirty command-line interfaces.
|
19
|
+
|
20
|
+
Here are a few real examples:
|
21
|
+
|
22
|
+
* {Syntax On}[http://github.com/remi/syntax-on/tree/master/lib/syntax-on/bin.rb]
|
23
|
+
* {Domain Finder}[http://github.com/remi/domain-finder/tree/master/lib/domain-finder/bin.rb]
|
24
|
+
* ADF[http://github.com/remi/adf/tree/master/lib/adf/bin.rb]
|
25
|
+
|
26
|
+
== Example
|
27
|
+
|
28
|
+
Here's a super simple SimpleCLI example:
|
29
|
+
|
30
|
+
#! /usr/bin/env ruby
|
31
|
+
|
32
|
+
require File.dirname(__FILE__) + '/../lib/simplecli'
|
33
|
+
|
34
|
+
class Hello
|
35
|
+
include SimpleCLI
|
36
|
+
|
37
|
+
def usage
|
38
|
+
puts <<doco
|
39
|
+
|
40
|
+
Hello CLI
|
41
|
+
|
42
|
+
Usage:
|
43
|
+
#{ script_name } command [options]
|
44
|
+
|
45
|
+
Futher help:
|
46
|
+
#{ script_name } commands # list all available commands
|
47
|
+
#{ script_name } help <COMMAND> # show help for COMMAND
|
48
|
+
#{ script_name } help # show this help message
|
49
|
+
|
50
|
+
doco
|
51
|
+
end
|
52
|
+
|
53
|
+
def sayhello_help
|
54
|
+
<<doco
|
55
|
+
Usage: #{ script_name } sayhello [SAY]
|
56
|
+
|
57
|
+
Arguments:
|
58
|
+
SAY: Something to say (default 'Hello World!')
|
59
|
+
|
60
|
+
Summary:
|
61
|
+
Says hello!
|
62
|
+
doco
|
63
|
+
end
|
64
|
+
def sayhello *args
|
65
|
+
puts args.empty? ? "Hello World!" : args.join(' ')
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
# POSTAMBLE
|
71
|
+
if __FILE__ == $0
|
72
|
+
Hello.new( ARGV, :default => 'sayhello' ).run
|
73
|
+
end
|
74
|
+
|
75
|
+
Example usage:
|
76
|
+
|
77
|
+
<em><tt>$ ./hello-cli</tt></em>
|
78
|
+
|
79
|
+
Hello CLI
|
80
|
+
|
81
|
+
Usage:
|
82
|
+
hello-cli command [options]
|
83
|
+
|
84
|
+
Futher help:
|
85
|
+
hello-cli commands # list all available commands
|
86
|
+
hello-cli help <COMMAND> # show help for COMMAND
|
87
|
+
hello-cli help # show this help message
|
88
|
+
|
89
|
+
<em><tt>$ ./hello-cli commands</tt></em>
|
90
|
+
|
91
|
+
hello-cli commands are:
|
92
|
+
|
93
|
+
DEFAULT COMMAND sayhello
|
94
|
+
|
95
|
+
commands List all 'hello-cli' commands
|
96
|
+
help Provide help documentation for a command
|
97
|
+
sayhello Says hello!
|
98
|
+
|
99
|
+
For help on a particular command, use 'hello-cli help COMMAND'.
|
100
|
+
|
101
|
+
<em><tt>$ ./hello-cli help</tt></em>
|
102
|
+
|
103
|
+
Usage: hello-cli help COMMAND
|
104
|
+
|
105
|
+
Summary:
|
106
|
+
Provide help documentation for a command
|
107
|
+
|
108
|
+
<em><tt>$ ./hello-cli help sayhello</tt></em>
|
109
|
+
|
110
|
+
Usage: hello-cli sayhello [SAY]
|
111
|
+
|
112
|
+
Arguments:
|
113
|
+
SAY: Something to say (default 'Hello World!')
|
114
|
+
|
115
|
+
Summary:
|
116
|
+
Says hello!
|
117
|
+
|
118
|
+
<em><tt>$ ./hello-cli sayhello</tt></em>
|
119
|
+
|
120
|
+
Hello World!
|
121
|
+
|
122
|
+
<em><tt>$ ./hello-cli sayhello Hi There</tt></em>
|
123
|
+
|
124
|
+
Hi There
|
125
|
+
|
126
|
+
<em><tt>$ ./hello-cli Hi There</tt></em> `# this works because sayhello is configured as the default command`
|
127
|
+
|
128
|
+
Hi There
|
129
|
+
|
130
|
+
TODO
|
131
|
+
----
|
132
|
+
|
133
|
+
* implement <tt>command_missing</tt> method
|
data/Rakefile
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'spec/rake/spectask'
|
5
|
+
|
6
|
+
begin
|
7
|
+
require 'jeweler'
|
8
|
+
Jeweler::Tasks.new do |s|
|
9
|
+
s.name = "simplecli"
|
10
|
+
s.summary = "For making simple RubyGem-like command-line interfaces"
|
11
|
+
s.email = "remi@remitaylor.com"
|
12
|
+
s.homepage = "http://github.com/remi/simplecli"
|
13
|
+
s.description = "SimpleCLI gives you a stupidly simple way to implement command-line interfaces like that of RubyGems"
|
14
|
+
s.authors = %w( remi )
|
15
|
+
s.files = FileList["[A-Z]*", "{lib,spec,examples}/**/*"]
|
16
|
+
# s.add_dependency 'person-project'
|
17
|
+
# s.executables = "neato"
|
18
|
+
end
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
21
|
+
end
|
22
|
+
|
23
|
+
Spec::Rake::SpecTask.new do |t|
|
24
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
desc "Run all examples with RCov"
|
28
|
+
Spec::Rake::SpecTask.new('rcov') do |t|
|
29
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
30
|
+
t.rcov = true
|
31
|
+
end
|
32
|
+
|
33
|
+
Rake::RDocTask.new do |rdoc|
|
34
|
+
rdoc.rdoc_dir = 'rdoc'
|
35
|
+
rdoc.title = 'simplecli'
|
36
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
37
|
+
rdoc.rdoc_files.include('README.rdoc')
|
38
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
39
|
+
end
|
40
|
+
|
41
|
+
desc 'Confirm that gemspec is $SAFE'
|
42
|
+
task :safe do
|
43
|
+
require 'yaml'
|
44
|
+
require 'rubygems/specification'
|
45
|
+
data = File.read('simplecli.gemspec')
|
46
|
+
spec = nil
|
47
|
+
if data !~ %r{!ruby/object:Gem::Specification}
|
48
|
+
Thread.new { spec = eval("$SAFE = 3\n#{data}") }.join
|
49
|
+
else
|
50
|
+
spec = YAML.load(data)
|
51
|
+
end
|
52
|
+
spec.validate
|
53
|
+
puts spec
|
54
|
+
puts "OK"
|
55
|
+
end
|
56
|
+
|
57
|
+
task :default => :spec
|
data/VERSION.yml
ADDED
data/lib/simplecli.rb
CHANGED
@@ -59,20 +59,61 @@ module SimpleCLI
|
|
59
59
|
|
60
60
|
@default_command = @options[:default].to_s if @options.keys.include? :default
|
61
61
|
@commands = all_commands
|
62
|
-
|
63
|
-
if
|
62
|
+
|
63
|
+
# if a command is discovered, eg. $ myscript foo 1 2 3 # where foo is a command
|
64
|
+
if not args.empty? and @commands.map {|c| c.downcase }.include? args.first.downcase
|
64
65
|
@command = args.shift.downcase
|
66
|
+
|
67
|
+
# if a command is not discovered, and no arguments were sent at all, eg. $ myscript
|
68
|
+
elsif args.empty?
|
69
|
+
@command = @default_command || 'usage'
|
70
|
+
|
71
|
+
# there were args passed, but we don't know what to call ... try command_missing first
|
72
|
+
elsif command_from_command_missing = command_missing(args)
|
73
|
+
@command = command_from_command_missing # if it returns something that's not nil, set it to command
|
74
|
+
|
75
|
+
# there were some arguments, pass if to the default command if there is one, else 'command not found'
|
76
|
+
elsif @default_command
|
77
|
+
@command = @default_command
|
78
|
+
|
79
|
+
# nothing worked out ... show command not found & usage
|
65
80
|
else
|
66
|
-
|
81
|
+
puts "command not found: #{ args.first.downcase }"
|
82
|
+
@command = 'usage'
|
83
|
+
|
67
84
|
end
|
68
85
|
|
69
|
-
@command_args
|
86
|
+
@command_args = args
|
87
|
+
end
|
88
|
+
|
89
|
+
# before dropping to default command ( if defined via :default option ),
|
90
|
+
# the arguments get passed along to command_missing (you get the original args array)
|
91
|
+
#
|
92
|
+
# your command_missing can return a command string (name of the command) or just a proc to call!
|
93
|
+
#
|
94
|
+
# actually, if what you respond with responds to #call, we use that, else we call #to_s to
|
95
|
+
# get the name of the command to run
|
96
|
+
#
|
97
|
+
# ***NOTE*** if command_missing makes changes to the args passed to it, these are persisted
|
98
|
+
# and passed to the command. args.dup if you need to mess with args!
|
99
|
+
#
|
100
|
+
# if you return something #call-able, the command arguments will be passed to your block
|
101
|
+
#
|
102
|
+
# you can call super to drop back to the default command_missing in SimpleCLI
|
103
|
+
# or just return nil to say "nope, can't find a command for this"
|
104
|
+
#
|
105
|
+
def command_missing args
|
106
|
+
nil
|
70
107
|
end
|
71
108
|
|
72
109
|
# run command determined by parse
|
73
110
|
def run
|
74
111
|
begin
|
75
|
-
|
112
|
+
if @command.respond_to? :call
|
113
|
+
@command.call @command_args
|
114
|
+
else
|
115
|
+
self.send @command.to_s, *@command_args
|
116
|
+
end
|
76
117
|
rescue ArgumentError => ex
|
77
118
|
puts "'#{@command}' called with wrong number of arguments\n\n"
|
78
119
|
puts help_for( @command )
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: remi-simplecli
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
- remi
|
7
|
+
- remi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2009-03-14 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -19,25 +19,21 @@ executables: []
|
|
19
19
|
|
20
20
|
extensions: []
|
21
21
|
|
22
|
-
extra_rdoc_files:
|
23
|
-
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
24
|
files:
|
25
|
-
-
|
25
|
+
- Rakefile
|
26
|
+
- VERSION.yml
|
27
|
+
- README.rdoc
|
28
|
+
- LICENSE
|
26
29
|
- lib/simplecli.rb
|
27
30
|
- examples/hello-cli
|
28
31
|
has_rdoc: true
|
29
32
|
homepage: http://github.com/remi/simplecli
|
30
33
|
post_install_message:
|
31
34
|
rdoc_options:
|
32
|
-
- --quiet
|
33
|
-
- --title
|
34
|
-
- SimpleCLI - Simple RubyGems-like Command-line Interface
|
35
|
-
- --opname
|
36
|
-
- index.html
|
37
|
-
- --line-numbers
|
38
|
-
- --main
|
39
|
-
- README
|
40
35
|
- --inline-source
|
36
|
+
- --charset=UTF-8
|
41
37
|
require_paths:
|
42
38
|
- lib
|
43
39
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -55,9 +51,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
51
|
requirements: []
|
56
52
|
|
57
53
|
rubyforge_project:
|
58
|
-
rubygems_version: 1.0
|
54
|
+
rubygems_version: 1.2.0
|
59
55
|
signing_key:
|
60
56
|
specification_version: 2
|
61
|
-
summary:
|
57
|
+
summary: For making simple RubyGem-like command-line interfaces
|
62
58
|
test_files: []
|
63
59
|
|
data/README
DELETED
@@ -1,118 +0,0 @@
|
|
1
|
-
== SimpleCLI
|
2
|
-
|
3
|
-
Super Simple RubyGems-like CLI
|
4
|
-
|
5
|
-
SimpleCLI gives you a stupidly simple way to implement command-line
|
6
|
-
interfaces like that of RubyGems with a basic interface like:
|
7
|
-
|
8
|
-
gem command [options]
|
9
|
-
|
10
|
-
SimpleCLI gives you a way of defining your commands (or actions) so
|
11
|
-
they'll automatically show up when you run <tt>`yourapp commands`</tt>
|
12
|
-
|
13
|
-
SimpleCLI also makes it really easy to add documentation to each of
|
14
|
-
your commands (or actions)
|
15
|
-
|
16
|
-
=== Example
|
17
|
-
|
18
|
-
Here's a super simple SimpleCLI example:
|
19
|
-
|
20
|
-
#! /usr/bin/env ruby
|
21
|
-
|
22
|
-
require File.dirname(__FILE__) + '/../lib/simplecli'
|
23
|
-
|
24
|
-
class Hello
|
25
|
-
include SimpleCLI
|
26
|
-
|
27
|
-
def usage
|
28
|
-
puts <<doco
|
29
|
-
|
30
|
-
Hello CLI
|
31
|
-
|
32
|
-
Usage:
|
33
|
-
#{ script_name } command [options]
|
34
|
-
|
35
|
-
Futher help:
|
36
|
-
#{ script_name } commands # list all available commands
|
37
|
-
#{ script_name } help <COMMAND> # show help for COMMAND
|
38
|
-
#{ script_name } help # show this help message
|
39
|
-
|
40
|
-
doco
|
41
|
-
end
|
42
|
-
|
43
|
-
def sayhello_help
|
44
|
-
<<doco
|
45
|
-
Usage: #{ script_name } sayhello [SAY]
|
46
|
-
|
47
|
-
Arguments:
|
48
|
-
SAY: Something to say (default 'Hello World!')
|
49
|
-
|
50
|
-
Summary:
|
51
|
-
Says hello!
|
52
|
-
doco
|
53
|
-
end
|
54
|
-
def sayhello *args
|
55
|
-
puts args.empty? ? "Hello World!" : args.join(' ')
|
56
|
-
end
|
57
|
-
|
58
|
-
end
|
59
|
-
|
60
|
-
# POSTAMBLE
|
61
|
-
if __FILE__ == $0
|
62
|
-
Hello.new( ARGV, :default => 'sayhello' ).run
|
63
|
-
end
|
64
|
-
|
65
|
-
Example usage:
|
66
|
-
|
67
|
-
<b><tt>$ ./hello-cli</b></tt>
|
68
|
-
|
69
|
-
Hello CLI
|
70
|
-
|
71
|
-
Usage:
|
72
|
-
hello-cli command [options]
|
73
|
-
|
74
|
-
Futher help:
|
75
|
-
hello-cli commands # list all available commands
|
76
|
-
hello-cli help <COMMAND> # show help for COMMAND
|
77
|
-
hello-cli help # show this help message
|
78
|
-
|
79
|
-
<b><tt>$ ./hello-cli commands</b></tt>
|
80
|
-
|
81
|
-
hello-cli commands are:
|
82
|
-
|
83
|
-
DEFAULT COMMAND sayhello
|
84
|
-
|
85
|
-
commands List all 'hello-cli' commands
|
86
|
-
help Provide help documentation for a command
|
87
|
-
sayhello Says hello!
|
88
|
-
|
89
|
-
For help on a particular command, use 'hello-cli help COMMAND'.
|
90
|
-
|
91
|
-
<b><tt>$ ./hello-cli help</b></tt>
|
92
|
-
|
93
|
-
Usage: hello-cli help COMMAND
|
94
|
-
|
95
|
-
Summary:
|
96
|
-
Provide help documentation for a command
|
97
|
-
|
98
|
-
<b><tt>$ ./hello-cli help sayhello</b></tt>
|
99
|
-
|
100
|
-
Usage: hello-cli sayhello [SAY]
|
101
|
-
|
102
|
-
Arguments:
|
103
|
-
SAY: Something to say (default 'Hello World!')
|
104
|
-
|
105
|
-
Summary:
|
106
|
-
Says hello!
|
107
|
-
|
108
|
-
<b><tt>$ ./hello-cli sayhello</b></tt>
|
109
|
-
|
110
|
-
Hello World!
|
111
|
-
|
112
|
-
<b><tt>$ ./hello-cli sayhello Hi There</b></tt>
|
113
|
-
|
114
|
-
Hi There
|
115
|
-
|
116
|
-
<b><tt>$ ./hello-cli Hi There</b></tt> <tt># this works because sayhello is configured as the default command</tt>
|
117
|
-
|
118
|
-
Hi There
|