byggvir 0.0.2
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.
- checksums.yaml +7 -0
- data/README.md +55 -0
- data/lib/byggvir.rb +39 -0
- data/lib/byggvir/simple.rb +10 -0
- data/lib/byggvir/static.rb +44 -0
- data/lib/byggvir/version.rb +3 -0
- data/test/doctest.rb +13 -0
- data/test/doctest.rb~ +13 -0
- data/test/easy.rb +7 -0
- data/test/easy.rb~ +5 -0
- data/test/failing.rb +15 -0
- data/test/failing.rb~ +16 -0
- data/test/multiple.rb +16 -0
- data/test/multiple.rb~ +15 -0
- data/test/test.rb +10 -0
- data/test/testfrompe.rb~ +14 -0
- data/test/update_local.rb +13 -0
- data/test/update_local.rb~ +16 -0
- metadata +73 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 33cb21d9027ea23af201468dd6dd94765e6167cf
|
4
|
+
data.tar.gz: 132a76de672b5a45ec7835f9816632d858c3922e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0cdd028fb88671b4e72951be28829445e3c57f71c1f6ce327d84023fc5bec38ddbdd2535e23774fbae5378deb9d1d0ca57261525d3c8934a797dfe14a0824658
|
7
|
+
data.tar.gz: 1388b8ea08e29506906224c4841a6b90d7841b00f2f1cff67a01344960aa738aa21a257b27958ec89524b38d8097da8206cf32bd2af78c8d396e7d77aa52bff8
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
byggvir
|
2
|
+
=======
|
3
|
+
|
4
|
+
Byggvir is designed to be the simplest possible command line processing for ruby 2.1
|
5
|
+
|
6
|
+
Just require byggvir/simple and off you go!
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
require 'byggvir/simple'
|
10
|
+
|
11
|
+
def cat_zoo(cats:, dollars: 0.0):
|
12
|
+
puts "#{cats}, #{cost}"
|
13
|
+
end
|
14
|
+
```
|
15
|
+
Byggvir/simple is the most convenient option- it will match the function with the same name as your file. Integers, floats and arrays are automatically made into the most logical type, and you can use the short, one letter argument if there is no duplication.
|
16
|
+
|
17
|
+
```bash
|
18
|
+
cat_zoo -c "Mr Buttons,spot" -d 5.0
|
19
|
+
cat_zoo -c Mephistopheles,Drunky -d 5
|
20
|
+
```
|
21
|
+
|
22
|
+
Byggvir::cli will apply to the function you specify
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
require 'byggvir'
|
26
|
+
|
27
|
+
Byggvir::cli,
|
28
|
+
def cat_master(cats:)
|
29
|
+
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
Byggvir/multiple will match multiple subfunctions
|
34
|
+
```bash
|
35
|
+
pet dog -n 5
|
36
|
+
pet cat -n 3
|
37
|
+
```
|
38
|
+
|
39
|
+
Just like Thor
|
40
|
+
```ruby
|
41
|
+
require 'byggvir'
|
42
|
+
class Pet < Byggvir::Multiple
|
43
|
+
doc "The number of dogs",
|
44
|
+
def dog(number:)
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
doc "The number of cats",
|
49
|
+
def cat(number:)
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
Note that it is currently quite easy to expose bug https://bugs.ruby-lang.org/issues/9308 when using the multiple/doc functionality
|
data/lib/byggvir.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'byggvir/static'
|
2
|
+
class Byggvir
|
3
|
+
def self.cli(fun)
|
4
|
+
::Byggvir.wrap(self,cli)
|
5
|
+
end
|
6
|
+
class TestObject
|
7
|
+
end
|
8
|
+
class Multiple
|
9
|
+
class << self
|
10
|
+
def doc(doc,name=nil)
|
11
|
+
::Byggvir.error!("doc has to end with a comma, #{caller[1..1]}") unless name.class == Symbol
|
12
|
+
@docs||={}
|
13
|
+
@docs[name]=doc
|
14
|
+
end
|
15
|
+
def commands(inst)
|
16
|
+
unless @docs
|
17
|
+
@docs = (inst.methods.to_set - TestObject.new.methods.to_set).map{|meth|
|
18
|
+
[meth,"Options: "+::Byggvir.parameters(inst.instance_eval{self.class.instance_method(meth)}).keys.join(?,)]
|
19
|
+
}.to_h
|
20
|
+
end
|
21
|
+
longest = @docs.keys.map(&:length).max
|
22
|
+
@docs.map{ |k,v| Kernel.sprintf("% #{longest+1}s : %s",k,v)}.join("\n")
|
23
|
+
end
|
24
|
+
def new(*args)
|
25
|
+
inst=super(*args)
|
26
|
+
::Byggvir.error!("Please specify a method, One of: \n#{commands(inst)}") unless ARGV.length>0
|
27
|
+
name = ARGV[0].to_sym
|
28
|
+
ARGV.drop(1)
|
29
|
+
begin
|
30
|
+
meth = instance_method(name)
|
31
|
+
rescue NameError => ex
|
32
|
+
::Byggvir.error!("No method #{name.to_s} defined in #{$0}, try one of \n#{commands(inst)} (#{ex})")
|
33
|
+
else
|
34
|
+
::Byggvir.wrap(inst,meth)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'set'
|
2
|
+
require 'getoptlong'
|
3
|
+
require 'yaml'
|
4
|
+
class Byggvir
|
5
|
+
def self.error!(err)
|
6
|
+
$stderr.puts("#{$0}:#{err}");$stderr.flush;Process.exit!(-1);
|
7
|
+
end
|
8
|
+
def self.parameters(method)
|
9
|
+
method #map argument types
|
10
|
+
.parameters
|
11
|
+
.map{|ty, na| [na.to_s, ty]}
|
12
|
+
.to_h
|
13
|
+
end
|
14
|
+
def self.wrap(instance,method)
|
15
|
+
name = method.name
|
16
|
+
arguments = ::Byggvir.parameters(method)
|
17
|
+
if (badarg = (arguments.values.to_set - [:keyreq, :key].to_set)).empty?
|
18
|
+
required_arguments = arguments.select{|k,v| v == :keyreq}.keys.to_set
|
19
|
+
short = arguments
|
20
|
+
.keys
|
21
|
+
.map{|k| {(?- + k[0]) => k}} #prepend dash
|
22
|
+
.reduce{|old, new| old.merge(new){|k, o, n| nil}} #duplicates are uncertain
|
23
|
+
.select{|k, v| v != nil}
|
24
|
+
.invert
|
25
|
+
opts = GetoptLong.new(*arguments.map{|k, v| ["--#{k}", short[k], GetoptLong::REQUIRED_ARGUMENT].compact})
|
26
|
+
opts.quiet = true
|
27
|
+
call = {}
|
28
|
+
unwrap = proc {|w| (w.class == Array && w.length == 1)?w[0]:w }
|
29
|
+
begin
|
30
|
+
opts.each{|opt, arg| call[opt[2..-1].to_sym] = unwrap.call(YAML.load("---\n[#{arg}]"))}
|
31
|
+
rescue GetoptLong::InvalidOption => ex
|
32
|
+
::Byggvir.error!(ex.to_s)
|
33
|
+
else
|
34
|
+
if (missing_arguments = (required_arguments - call.keys.map(&:to_s).to_set)).empty?
|
35
|
+
instance.send(name,**call)
|
36
|
+
else
|
37
|
+
::Byggvir.error!("Missing arguments to #{name}: #{missing_arguments.to_a.join(?,)}")
|
38
|
+
end
|
39
|
+
end
|
40
|
+
else
|
41
|
+
::Byggvir.error!("Doesn't support non-hash like function calls. Need something like def(required: ,optional: thing), bad arguments #{badarg}")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/test/doctest.rb
ADDED
data/test/doctest.rb~
ADDED
data/test/easy.rb
ADDED
data/test/easy.rb~
ADDED
data/test/failing.rb
ADDED
data/test/failing.rb~
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env rvm 2.1.0 do ruby
|
2
|
+
$:.push('../lib')
|
3
|
+
require 'byggvir'
|
4
|
+
|
5
|
+
class Test < Byggvir::Multiple
|
6
|
+
doc "Short floo documentation",
|
7
|
+
def floo(frac: 3, froc:, duc:["soy","boy"])
|
8
|
+
p [frac,froc,duc]
|
9
|
+
end
|
10
|
+
|
11
|
+
def florb(duc: )
|
12
|
+
p [duc]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
Test.new
|
data/test/multiple.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env rvm 2.1.0 do ruby
|
2
|
+
$:.push('../lib')
|
3
|
+
require 'byggvir'
|
4
|
+
|
5
|
+
class Test < Byggvir::Multiple
|
6
|
+
doc "Short floo documentation",
|
7
|
+
def floo(frac: 3, froc:, duc:["soy","boy"])
|
8
|
+
p [frac,froc,duc]
|
9
|
+
end
|
10
|
+
|
11
|
+
def florb(duc: )
|
12
|
+
p [duc]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
Test.new
|
data/test/multiple.rb~
ADDED
data/test/test.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
puts `./easy.rb -p spot,kitty --gifts=4 --money=32.25`
|
2
|
+
puts `./multiple.rb floo --frac=4 --froc=3.12,hoy`
|
3
|
+
puts `./multiple.rb florb --duc=4`
|
4
|
+
puts "Should fail:"
|
5
|
+
puts `./multiple.rb fleep`
|
6
|
+
puts `./failing.rb`
|
7
|
+
puts `./multiple.rb`
|
8
|
+
puts `./multiple.rb fleep`
|
9
|
+
puts `./easy.rb`
|
10
|
+
puts `./doctest.rb`
|
data/test/testfrompe.rb~
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env rvm 2.1.0 do ruby
|
2
|
+
$:.push('../lib')
|
3
|
+
|
4
|
+
class PusherControl << Byggvir::Multiple
|
5
|
+
doc "Get hostname of specific class (edge, rest, etc..), and then boot hostname",
|
6
|
+
def make(environment:,puppet_class:,release:DefaultRelease,output:ThreadedOperationalOutput.new)
|
7
|
+
output.run("booting a single server of type #{puppet_class} in #{environment}") do |out|
|
8
|
+
e=Environment.realize(environment)
|
9
|
+
hn=gethostname(e,puppet_class,1,out)[0]
|
10
|
+
instancetype=getinstancetype(environment:e.name,fqdn:hn,output:out)
|
11
|
+
boot(e,hn,instancetype,release,output.clone(hn))
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/env rvm 2.1.0 do ruby
|
2
|
+
$:.push('../lib')
|
3
|
+
require 'byggvir'
|
4
|
+
|
5
|
+
class Test < Byggvir::Multiple
|
6
|
+
doc "Boot list of hostnames",
|
7
|
+
def make_multiple_hostnames(environment:,hostnames:,release:DefaultRelease,output:ThreadedOperationalOutput.new)
|
8
|
+
p []
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
Test.new
|
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env rvm 2.1.0 do ruby
|
2
|
+
$:.push('../lib')
|
3
|
+
require 'byggvir'
|
4
|
+
|
5
|
+
class Test < Byggvir::Multiple
|
6
|
+
doc "Short floo documentation",
|
7
|
+
def floo(frac: 3, froc:, duc:["soy","boy"])
|
8
|
+
p [frac,froc,duc]
|
9
|
+
end
|
10
|
+
|
11
|
+
def update_local(output:3)
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
Test.new
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: byggvir
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Samuel Kleiner
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-13 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: |
|
14
|
+
Command line parser for new ruby2.1 functionality
|
15
|
+
email: sam@ulterior.org
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- README.md
|
21
|
+
- lib/byggvir.rb
|
22
|
+
- lib/byggvir/simple.rb
|
23
|
+
- lib/byggvir/static.rb
|
24
|
+
- lib/byggvir/version.rb
|
25
|
+
- test/doctest.rb
|
26
|
+
- test/doctest.rb~
|
27
|
+
- test/easy.rb
|
28
|
+
- test/easy.rb~
|
29
|
+
- test/failing.rb
|
30
|
+
- test/failing.rb~
|
31
|
+
- test/multiple.rb
|
32
|
+
- test/multiple.rb~
|
33
|
+
- test/test.rb
|
34
|
+
- test/testfrompe.rb~
|
35
|
+
- test/update_local.rb
|
36
|
+
- test/update_local.rb~
|
37
|
+
homepage: https://github.com/cultureulterior/byggvir
|
38
|
+
licenses:
|
39
|
+
- MIT
|
40
|
+
metadata: {}
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - '='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '2.1'
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 2.2.1
|
58
|
+
signing_key:
|
59
|
+
specification_version: 3
|
60
|
+
summary: Command line tool for ruby2.1
|
61
|
+
test_files:
|
62
|
+
- test/doctest.rb
|
63
|
+
- test/doctest.rb~
|
64
|
+
- test/easy.rb
|
65
|
+
- test/easy.rb~
|
66
|
+
- test/failing.rb
|
67
|
+
- test/failing.rb~
|
68
|
+
- test/multiple.rb
|
69
|
+
- test/multiple.rb~
|
70
|
+
- test/test.rb
|
71
|
+
- test/testfrompe.rb~
|
72
|
+
- test/update_local.rb
|
73
|
+
- test/update_local.rb~
|