avalanche-cli 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/README.md +60 -0
- data/Rakefile +1 -0
- data/cli.gemspec +20 -0
- data/cli/application.rb +73 -0
- data/cli/command.rb +50 -0
- data/cli/version.rb +3 -0
- data/example/app +19 -0
- data/example/hello +17 -0
- data/lib/avalanche/cli.rb +4 -0
- data/lib/avalanche/cli/application.rb +75 -0
- data/lib/avalanche/cli/command.rb +70 -0
- data/lib/avalanche/cli/version.rb +5 -0
- metadata +91 -0
data/.gitignore
ADDED
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# CLI
|
2
|
+
|
3
|
+
A small command line interface library for quickly building cli apps.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
Add this to your Gemfile:
|
8
|
+
|
9
|
+
gem 'cli', :git => 'git://github.com/avalanche123/cli.git'
|
10
|
+
|
11
|
+
Build a single command script:
|
12
|
+
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
#!/usr/bin/env ruby
|
16
|
+
|
17
|
+
require 'bundler/setup'
|
18
|
+
require 'cli'
|
19
|
+
|
20
|
+
cmd = CLI::Command.new('hello [options] NAME', {
|
21
|
+
:title => ['--title TITLE', String, 'Desired title']
|
22
|
+
}, Proc.new do |opts, args|
|
23
|
+
raise "NAME is required" if args.empty?
|
24
|
+
|
25
|
+
puts "Hello, #{opts[:title]} #{args.first}!".gsub(/\s+/, " ")
|
26
|
+
end)
|
27
|
+
|
28
|
+
cmd.run(ARGV)
|
29
|
+
```
|
30
|
+
|
31
|
+
Build a cli application with many subcommands:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
#!/usr/bin/env ruby
|
35
|
+
|
36
|
+
require 'bundler/setup'
|
37
|
+
require 'cli'
|
38
|
+
|
39
|
+
app = CLI::Application.new('app', '0.1.0')
|
40
|
+
|
41
|
+
app.command('hello [options] NAME', "Says Hello", {
|
42
|
+
:title => ['--title TITLE', String, 'Desired title']
|
43
|
+
}, Proc.new do |opts, args|
|
44
|
+
raise "NAME is required" if args.empty?
|
45
|
+
|
46
|
+
puts "Hello, #{opts[:title]} #{args.first}!".gsub(/\s+/, " ")
|
47
|
+
end)
|
48
|
+
|
49
|
+
app.run(ARGV)
|
50
|
+
```
|
51
|
+
|
52
|
+
Check it:
|
53
|
+
|
54
|
+
```shell
|
55
|
+
> /path/to/command --help
|
56
|
+
```
|
57
|
+
|
58
|
+
## License
|
59
|
+
|
60
|
+
MIT
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/cli.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
require 'avalanche/cli/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "avalanche-cli"
|
8
|
+
gem.version = Avalanche::CLI::VERSION
|
9
|
+
gem.authors = ["Bulat Shakirzyanov"]
|
10
|
+
gem.email = ["mallluhuct@gmail.com"]
|
11
|
+
gem.description = %q{A small command line interface library for quickly building cli apps.}
|
12
|
+
gem.summary = %q{A small command line interface library for quickly building cli apps.}
|
13
|
+
gem.homepage = "https://github.com/avalanche123/cli"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.require_paths = ["lib"]
|
17
|
+
|
18
|
+
gem.add_development_dependency "bundler", "~> 1.3"
|
19
|
+
gem.add_development_dependency "rake"
|
20
|
+
end
|
data/cli/application.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
module CLI
|
2
|
+
class Application
|
3
|
+
def initialize(name, version)
|
4
|
+
@name = name
|
5
|
+
@version = version
|
6
|
+
@commands = {}
|
7
|
+
@opts = OptionParser.new
|
8
|
+
|
9
|
+
@opts.program_name = @name
|
10
|
+
|
11
|
+
@opts.banner = "Usage: #{@name} [--version] [--help] command [options] [args]"
|
12
|
+
|
13
|
+
@opts.top.append("", nil, nil)
|
14
|
+
@opts.top.append("Available #{@name} commands are:", nil, nil)
|
15
|
+
|
16
|
+
@opts.base.append("", nil, nil)
|
17
|
+
@opts.base.append("Common options:", nil, nil)
|
18
|
+
|
19
|
+
@opts.on_tail("-h", "--help", "Show this message") { print_help }
|
20
|
+
@opts.on_tail('-v', '--version', "Show version") { print_version }
|
21
|
+
end
|
22
|
+
|
23
|
+
def command(usage, description, options, handler)
|
24
|
+
parts = usage.split(" ")
|
25
|
+
command = parts.shift
|
26
|
+
|
27
|
+
@opts.separator(sprintf(" %-10.10s %s", command, description))
|
28
|
+
|
29
|
+
parts.unshift("#{@name}-#{command}")
|
30
|
+
|
31
|
+
@commands[command] = Command.new(parts.join(" "), options, handler)
|
32
|
+
end
|
33
|
+
|
34
|
+
def run(argv)
|
35
|
+
if argv.empty?
|
36
|
+
print_help
|
37
|
+
end
|
38
|
+
|
39
|
+
command = @commands[argv.first]
|
40
|
+
|
41
|
+
if command.nil?
|
42
|
+
@opts.permute!(argv)
|
43
|
+
print_help
|
44
|
+
else
|
45
|
+
argv.shift
|
46
|
+
command.run(argv)
|
47
|
+
end
|
48
|
+
rescue OptionParser::InvalidOption => e
|
49
|
+
@opts.warn(e.message)
|
50
|
+
print_help
|
51
|
+
rescue Interrupt
|
52
|
+
exit 0
|
53
|
+
rescue => e
|
54
|
+
@opts.abort(e.message)
|
55
|
+
end
|
56
|
+
|
57
|
+
def to_proc
|
58
|
+
proc { |opts, args| run(args) }
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def print_help
|
64
|
+
puts @opts
|
65
|
+
exit 1
|
66
|
+
end
|
67
|
+
|
68
|
+
def print_version
|
69
|
+
puts "#{@name} #{@version}"
|
70
|
+
exit 0
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
data/cli/command.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
module CLI
|
2
|
+
class Command
|
3
|
+
def initialize(usage, options, handler)
|
4
|
+
parts = usage.split(/\s+/)
|
5
|
+
@name = parts.shift
|
6
|
+
usage = parts.join(" ")
|
7
|
+
@handler = handler.to_proc
|
8
|
+
@options = {}
|
9
|
+
@opts = OptionParser.new
|
10
|
+
|
11
|
+
@opts.program_name = @name
|
12
|
+
|
13
|
+
@opts.banner = "Usage: #{@name} #{usage}"
|
14
|
+
|
15
|
+
options.each do |name, opts|
|
16
|
+
@opts.on(*opts) do |v|
|
17
|
+
@options[name] = v
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
@opts.base.append("", nil, nil)
|
22
|
+
@opts.base.append("Additional options:", nil, nil)
|
23
|
+
|
24
|
+
@opts.on_tail("-h", "--help", "Show this message") { print_help }
|
25
|
+
end
|
26
|
+
|
27
|
+
def run(argv)
|
28
|
+
$0 = "#{@name} #{argv.join(" ")}"
|
29
|
+
@handler.call(@options, @opts.permute!(argv))
|
30
|
+
rescue OptionParser::InvalidOption => e
|
31
|
+
@opts.warn(e.message)
|
32
|
+
print_help
|
33
|
+
rescue Interrupt
|
34
|
+
exit 0
|
35
|
+
rescue => e
|
36
|
+
@opts.abort(e.message)
|
37
|
+
end
|
38
|
+
|
39
|
+
def to_proc
|
40
|
+
proc { |opts, args| run(args) }
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def print_help
|
46
|
+
puts @opts
|
47
|
+
exit 1
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/cli/version.rb
ADDED
data/example/app
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
lib_dir = File.expand_path(File.dirname(__FILE__) + "/../lib")
|
4
|
+
|
5
|
+
$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
|
6
|
+
|
7
|
+
require 'avalanche/cli'
|
8
|
+
|
9
|
+
app = Avalanche::CLI::Application.new('app', '0.1.0')
|
10
|
+
|
11
|
+
app.command('hello [options] NAME', "Says Hello", {
|
12
|
+
:title => ['--title TITLE', String, 'Desired title']
|
13
|
+
}, Proc.new do |opts, args|
|
14
|
+
raise "NAME is required" if args.empty?
|
15
|
+
|
16
|
+
puts "Hello, #{opts[:title]} #{args.first}!".gsub(/\s+/, " ")
|
17
|
+
end)
|
18
|
+
|
19
|
+
app.run(ARGV)
|
data/example/hello
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
lib_dir = File.expand_path(File.dirname(__FILE__) + "/../lib")
|
4
|
+
|
5
|
+
$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
|
6
|
+
|
7
|
+
require 'avalanche/cli'
|
8
|
+
|
9
|
+
cmd = Avalanche::CLI::Command.new('hello [options] NAME', {
|
10
|
+
:title => ['--title TITLE', String, 'Desired title']
|
11
|
+
}, Proc.new do |opts, args|
|
12
|
+
raise "NAME is required" if args.empty?
|
13
|
+
|
14
|
+
puts "Hello, #{opts[:title]} #{args.first}!".gsub(/\s+/, " ")
|
15
|
+
end)
|
16
|
+
|
17
|
+
cmd.run(ARGV)
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module Avalanche
|
2
|
+
module CLI
|
3
|
+
class Application
|
4
|
+
def initialize(name, version)
|
5
|
+
@name = name
|
6
|
+
@version = version
|
7
|
+
@commands = {}
|
8
|
+
@opts = OptionParser.new
|
9
|
+
|
10
|
+
@opts.program_name = @name
|
11
|
+
|
12
|
+
@opts.banner = "Usage: #{@name} [--version] [--help] command [options] [args]"
|
13
|
+
|
14
|
+
@opts.top.append("", nil, nil)
|
15
|
+
@opts.top.append("Available #{@name} commands are:", nil, nil)
|
16
|
+
|
17
|
+
@opts.base.append("", nil, nil)
|
18
|
+
@opts.base.append("Common options:", nil, nil)
|
19
|
+
|
20
|
+
@opts.on_tail("-h", "--help", "Show this message") { print_help }
|
21
|
+
@opts.on_tail('-v', '--version', "Show version") { print_version }
|
22
|
+
end
|
23
|
+
|
24
|
+
def command(usage, description, options, handler)
|
25
|
+
parts = usage.split(" ")
|
26
|
+
command = parts.shift
|
27
|
+
|
28
|
+
@opts.separator(sprintf(" %-10.10s %s", command, description))
|
29
|
+
|
30
|
+
parts.unshift("#{@name}-#{command}")
|
31
|
+
|
32
|
+
@commands[command] = Command.new(parts.join(" "), options, handler)
|
33
|
+
end
|
34
|
+
|
35
|
+
def run(argv)
|
36
|
+
if argv.empty?
|
37
|
+
print_help
|
38
|
+
end
|
39
|
+
|
40
|
+
command = @commands[argv.first]
|
41
|
+
|
42
|
+
if command.nil?
|
43
|
+
@opts.permute!(argv)
|
44
|
+
print_help
|
45
|
+
else
|
46
|
+
argv.shift
|
47
|
+
command.run(argv)
|
48
|
+
end
|
49
|
+
rescue OptionParser::InvalidOption => e
|
50
|
+
@opts.warn(e.message)
|
51
|
+
print_help
|
52
|
+
rescue Interrupt
|
53
|
+
exit 0
|
54
|
+
rescue => e
|
55
|
+
@opts.abort(e.message)
|
56
|
+
end
|
57
|
+
|
58
|
+
def to_proc
|
59
|
+
proc { |opts, args| run(args) }
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def print_help
|
65
|
+
puts @opts
|
66
|
+
exit 1
|
67
|
+
end
|
68
|
+
|
69
|
+
def print_version
|
70
|
+
puts "#{@name} #{@version}"
|
71
|
+
exit 0
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module Avalanche
|
2
|
+
module CLI
|
3
|
+
class Command
|
4
|
+
def initialize(usage, options, handler)
|
5
|
+
parts = usage.split(/\s+/)
|
6
|
+
@name = parts.shift
|
7
|
+
usage = parts.join(" ")
|
8
|
+
@handler = handler.to_proc
|
9
|
+
@options = {}
|
10
|
+
@opts = OptionParser.new
|
11
|
+
|
12
|
+
@opts.program_name = @name
|
13
|
+
|
14
|
+
@opts.banner = "Usage: #{@name} #{usage}"
|
15
|
+
|
16
|
+
options.each do |name, opts|
|
17
|
+
@opts.on(*opts) do |v|
|
18
|
+
@options[name] = v
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
@opts.base.append("", nil, nil)
|
23
|
+
@opts.base.append("Additional options:", nil, nil)
|
24
|
+
|
25
|
+
@opts.on_tail("-h", "--help", "Show this message") { print_help }
|
26
|
+
@opts.on_tail("-d", "--debug", "Enable debug output") { enable_debug }
|
27
|
+
end
|
28
|
+
|
29
|
+
def run(argv)
|
30
|
+
$0 = "#{@name} #{argv.join(" ")}"
|
31
|
+
@handler.call(@options, @opts.permute!(argv))
|
32
|
+
rescue ArgumentError, OptionParser::InvalidOption => e
|
33
|
+
@opts.warn(e.message)
|
34
|
+
|
35
|
+
if @debug
|
36
|
+
puts ""
|
37
|
+
puts e.backtrace
|
38
|
+
puts ""
|
39
|
+
end
|
40
|
+
|
41
|
+
print_help
|
42
|
+
rescue Interrupt
|
43
|
+
exit 0
|
44
|
+
rescue => e
|
45
|
+
if @debug
|
46
|
+
puts ""
|
47
|
+
puts e.backtrace
|
48
|
+
puts ""
|
49
|
+
end
|
50
|
+
|
51
|
+
@opts.abort(e.message)
|
52
|
+
end
|
53
|
+
|
54
|
+
def to_proc
|
55
|
+
proc { |opts, args| run(args) }
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def print_help
|
61
|
+
puts @opts
|
62
|
+
exit 1
|
63
|
+
end
|
64
|
+
|
65
|
+
def enable_debug
|
66
|
+
@debug = true
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: avalanche-cli
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Bulat Shakirzyanov
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-07-18 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: A small command line interface library for quickly building cli apps.
|
47
|
+
email:
|
48
|
+
- mallluhuct@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- cli.gemspec
|
57
|
+
- cli/application.rb
|
58
|
+
- cli/command.rb
|
59
|
+
- cli/version.rb
|
60
|
+
- example/app
|
61
|
+
- example/hello
|
62
|
+
- lib/avalanche/cli.rb
|
63
|
+
- lib/avalanche/cli/application.rb
|
64
|
+
- lib/avalanche/cli/command.rb
|
65
|
+
- lib/avalanche/cli/version.rb
|
66
|
+
homepage: https://github.com/avalanche123/cli
|
67
|
+
licenses: []
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ! '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.8.23
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: A small command line interface library for quickly building cli apps.
|
90
|
+
test_files: []
|
91
|
+
has_rdoc:
|