swamp-cli 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f3ad3156b27833899de0a8bea26a3f4b29caed89442eac42a7a0a0067c7c1886
4
+ data.tar.gz: c696acaea6ce58239bce863d418c251d96510f1dd6ab9141b66891f094811093
5
+ SHA512:
6
+ metadata.gz: f412e6249e8730dbc36d7e527627cfb9340e968d3bce4a7c2face82f6ea43a77796154d8703c2538328cf93c85937cbb2e6a6c2a94545fdb777045d1a6eeb205
7
+ data.tar.gz: f35dcb638c842a0000bb5442f7f88acb0c1f087d83d62b0524ab57c43ab2f7c23a6bd03cf92d6065c14b8a9bebc08b5d1928e47a50b2ac11ff5fc8645454f758
checksums.yaml.gz.sig ADDED
@@ -0,0 +1,2 @@
1
+ ���?q��5����V�#����Ъ+�3��9���f�<g8`4y��S�8�}�Q�;�΀o�f���!�e|
2
+
data.tar.gz.sig ADDED
Binary file
data/lib/swamp/cli.rb ADDED
@@ -0,0 +1,53 @@
1
+ require 'swamp/version'
2
+ require 'swamp/command'
3
+ require 'swamp/command_dsl'
4
+ require 'swamp/cli_dsl'
5
+ require 'swamp/context'
6
+ require 'swamp/error'
7
+
8
+ module Swamp
9
+ class CLI
10
+
11
+ attr_reader :commands
12
+ attr_reader :app_name
13
+ attr_accessor :version
14
+
15
+ def initialize(app_name, options = {})
16
+ @app_name = app_name
17
+ @version = options[:version]
18
+ @commands = {}
19
+ end
20
+
21
+ def add(command)
22
+ @commands[command.name.to_sym] = command
23
+ end
24
+
25
+ def command(name)
26
+ @commands[name.to_sym]
27
+ end
28
+
29
+ def dispatch(argv, *additional_args)
30
+ command_name = argv.shift
31
+ if command_name && command = command(command_name)
32
+ context = Context.new(self)
33
+ context.options = command.parse_options(argv)
34
+ context.args = argv
35
+
36
+ command.action.call(context, *additional_args)
37
+ else
38
+ raise Error, "Command not found"
39
+ end
40
+ end
41
+
42
+ def load_from_directory(path)
43
+ unless File.directory?(path)
44
+ raise Error, "No commands directory at #{path}"
45
+ end
46
+
47
+ Dir[File.join(path, '**', '*.rb')].each do |path|
48
+ CLIDSL.new(self).instance_eval(File.read(path))
49
+ end
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,18 @@
1
+ require 'swamp/command'
2
+ require 'swamp/command_dsl'
3
+
4
+ module Swamp
5
+ class CLIDSL
6
+
7
+ def initialize(cli)
8
+ @cli = cli
9
+ end
10
+
11
+ def command(name, &block)
12
+ command = Command.new(@cli, name)
13
+ CommandDSL.new(command).instance_eval(&block)
14
+ @cli.add(command)
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,42 @@
1
+ require 'optparse'
2
+
3
+ module Swamp
4
+ class Command
5
+
6
+ attr_reader :name
7
+ attr_reader :options
8
+ attr_reader :action
9
+ attr_accessor :description
10
+
11
+ def initialize(cli, name)
12
+ @cli = cli
13
+ @name = name
14
+ @options = {}
15
+ @args = []
16
+ end
17
+
18
+ def set_action(&block)
19
+ @action = block
20
+ end
21
+
22
+ def add_option(command, *args, &block)
23
+ @options[command] = {spec: [command] + args, block: block}
24
+ end
25
+
26
+ def parse_options(argv)
27
+ options = {}
28
+ OptionParser.new do |opts|
29
+ opts.version = @cli.version
30
+ opts.banner = "Usage: #{@cli.app_name} #{@name} [options]"
31
+ @options.values.each do |option|
32
+ opts_block = proc { |value| option[:block].call(value, options) }
33
+ opts.on(*option[:spec], &opts_block)
34
+ end
35
+ end.parse!(argv)
36
+ options
37
+ rescue OptionParser::InvalidOption, OptionParser::MissingArgument => e
38
+ raise Swamp::Error, e.message
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,21 @@
1
+ module Swamp
2
+ class CommandDSL
3
+
4
+ def initialize(command)
5
+ @command = command
6
+ end
7
+
8
+ def action(&block)
9
+ @command.set_action(&block)
10
+ end
11
+
12
+ def option(*args, &block)
13
+ @command.add_option(*args, &block)
14
+ end
15
+
16
+ def desc(text)
17
+ @command.description = text
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ module Swamp
2
+ class Context
3
+
4
+ attr_reader :cli
5
+ attr_accessor :options
6
+ attr_accessor :args
7
+
8
+ def initialize(cli)
9
+ @cli = cli
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ module Swamp
2
+ class Error < StandardError
3
+
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module Swamp
2
+ VERSION = '1.0.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: swamp-cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Cooke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIDZDCCAkygAwIBAgIBATANBgkqhkiG9w0BAQUFADA8MQswCQYDVQQDDAJtZTEZ
14
+ MBcGCgmSJomT8ixkARkWCWFkYW1jb29rZTESMBAGCgmSJomT8ixkARkWAmlvMB4X
15
+ DTE5MDUxNDEzNTIxM1oXDTIwMDUxMzEzNTIxM1owPDELMAkGA1UEAwwCbWUxGTAX
16
+ BgoJkiaJk/IsZAEZFglhZGFtY29va2UxEjAQBgoJkiaJk/IsZAEZFgJpbzCCASIw
17
+ DQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMUohRlPw3iIOhWZq+qf5N1ATm1H
18
+ 7gBO4TpsUrw/FL/+urFExzt1+4MPfiKjILge48vKpjoTfuZusRsOQebaFidOfmhk
19
+ sEqa941CvN3OeUYARA53ORlmoLDLmdcrxq430+woFp4uuSYwim/2YQgIMdgiOTqs
20
+ cHaM9yh/xUGMnH4lB9bBDNfggMmkSFb6P8Ax4rvdX3EVv5P58RHwHszd+UI4fyy9
21
+ 0W143m6ntNmqena4ZOc7HtWtRyDHHXXzlGgmghKEZgOA+/LO53VHp+cM0JqB7lq5
22
+ ZxN43fQrIT5yY9Dy7dRBeiDo53WNJPspa5soEivCBVYstMqfd+LGk/BnsyMCAwEA
23
+ AaNxMG8wCQYDVR0TBAIwADALBgNVHQ8EBAMCBLAwHQYDVR0OBBYEFGlRGerNfr6J
24
+ Dprgl6DQ3kLvgVvPMBoGA1UdEQQTMBGBD21lQGFkYW1jb29rZS5pbzAaBgNVHRIE
25
+ EzARgQ9tZUBhZGFtY29va2UuaW8wDQYJKoZIhvcNAQEFBQADggEBAK2TQPMeW9qh
26
+ NDNoVbbplfSc8/uscSP2DfssCbhXQqeDfF2z+kQpxv8iAc++KTlotqOaX5A6RvLb
27
+ NvuwMHPJRQJ2e8rbuN8Sh3tUjbkAEv3SFw4hqbKmtp0j2oKBU0dxHWNfp+5ulh2l
28
+ UXnQAt4zg3v1hTD1VrwLqG/hyk9xAaWB38lEDBuPhLrDIdDJklg9bD1E2TUvoMrg
29
+ L6TIbdP1TRrxINO1D9GzboR+OuWos7qMLBEEbjat/fQchYrW1KLcHIUCyrGkZTLm
30
+ 3wUJNGnT5XYq+qvTqmjkTSTfdGvZCM63C6bGdN5CAyMokGOOatGqyCMAONolWnfC
31
+ gm3t2GWWrxY=
32
+ -----END CERTIFICATE-----
33
+ date: 2019-10-13 00:00:00.000000000 Z
34
+ dependencies: []
35
+ description: A framework for writing simple CLI applications in Ruby.
36
+ email:
37
+ - me@adamcooke.io
38
+ executables: []
39
+ extensions: []
40
+ extra_rdoc_files: []
41
+ files:
42
+ - lib/swamp/cli.rb
43
+ - lib/swamp/cli_dsl.rb
44
+ - lib/swamp/command.rb
45
+ - lib/swamp/command_dsl.rb
46
+ - lib/swamp/context.rb
47
+ - lib/swamp/error.rb
48
+ - lib/swamp/version.rb
49
+ homepage: https://github.com/adamcooke/swamp
50
+ licenses:
51
+ - MIT
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubygems_version: 3.0.6
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Used by Procodile and Bask, this is a simple framework for developing simple
72
+ CLI applications in Ruby.
73
+ test_files: []
metadata.gz.sig ADDED
Binary file