byggvir 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 33cb21d9027ea23af201468dd6dd94765e6167cf
4
- data.tar.gz: 132a76de672b5a45ec7835f9816632d858c3922e
3
+ metadata.gz: 128d5803e389bf983050284063499d06f33d5b11
4
+ data.tar.gz: 14c01506592e0f90e601a07b25091f0ffab6566e
5
5
  SHA512:
6
- metadata.gz: 0cdd028fb88671b4e72951be28829445e3c57f71c1f6ce327d84023fc5bec38ddbdd2535e23774fbae5378deb9d1d0ca57261525d3c8934a797dfe14a0824658
7
- data.tar.gz: 1388b8ea08e29506906224c4841a6b90d7841b00f2f1cff67a01344960aa738aa21a257b27958ec89524b38d8097da8206cf32bd2af78c8d396e7d77aa52bff8
6
+ metadata.gz: f3bf53ab112c99a4115652063ab2d7c388da471f9ef75a9121714bf229ad600c686644618f856749bd103c60591477a4ebc2d4875b62879a08cb34cfdf998812
7
+ data.tar.gz: 89555404be353f42bbed45a4643c7c08c19ac4605f69d401d562d3edd6da02816e97fdebaed76f9f52a3760fd3109c1dee34c8339592668c96aaf4d8203d7fdc
data/README.md CHANGED
@@ -3,7 +3,7 @@ byggvir
3
3
 
4
4
  Byggvir is designed to be the simplest possible command line processing for ruby 2.1
5
5
 
6
- Just require byggvir/simple and off you go!
6
+ Just require byggvir/simple and off you go!
7
7
 
8
8
  ```ruby
9
9
  require 'byggvir/simple'
@@ -19,17 +19,6 @@ cat_zoo -c "Mr Buttons,spot" -d 5.0
19
19
  cat_zoo -c Mephistopheles,Drunky -d 5
20
20
  ```
21
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
22
  Byggvir/multiple will match multiple subfunctions
34
23
  ```bash
35
24
  pet dog -n 5
@@ -42,9 +31,9 @@ require 'byggvir'
42
31
  class Pet < Byggvir::Multiple
43
32
  doc "The number of dogs",
44
33
  def dog(number:)
45
-
34
+
46
35
  end
47
-
36
+
48
37
  doc "The number of cats",
49
38
  def cat(number:)
50
39
 
@@ -1,17 +1,21 @@
1
1
  require 'byggvir/static'
2
2
  class Byggvir
3
- def self.cli(fun)
4
- ::Byggvir.wrap(self,cli)
5
- end
6
3
  class TestObject
7
4
  end
8
5
  class Multiple
9
6
  class << self
7
+ def create_binaries()
8
+ directory = File.mkdir(File.basedir($0)+"/bin")
9
+ list_existing_methods.each{""}
10
+ end
11
+
10
12
  def doc(doc,name=nil)
11
13
  ::Byggvir.error!("doc has to end with a comma, #{caller[1..1]}") unless name.class == Symbol
12
14
  @docs||={}
13
15
  @docs[name]=doc
14
16
  end
17
+
18
+ # List existing commands
15
19
  def commands(inst)
16
20
  unless @docs
17
21
  @docs = (inst.methods.to_set - TestObject.new.methods.to_set).map{|meth|
@@ -21,6 +25,8 @@ class Byggvir
21
25
  longest = @docs.keys.map(&:length).max
22
26
  @docs.map{ |k,v| Kernel.sprintf("% #{longest+1}s : %s",k,v)}.join("\n")
23
27
  end
28
+
29
+ # Instantiate a multiple function object
24
30
  def new(*args)
25
31
  inst=super(*args)
26
32
  ::Byggvir.error!("Please specify a method, One of: \n#{commands(inst)}") unless ARGV.length>0
@@ -1,6 +1,7 @@
1
1
  require 'byggvir/static'
2
2
  class ::Object
3
3
  class << self
4
+ # call function if named same as file
4
5
  def method_added(name)
5
6
  if name.to_s == File.basename($0,".rb")
6
7
  ::Byggvir.wrap(self,self.instance_method(name))
@@ -5,40 +5,58 @@ class Byggvir
5
5
  def self.error!(err)
6
6
  $stderr.puts("#{$0}:#{err}");$stderr.flush;Process.exit!(-1);
7
7
  end
8
+
9
+ # get method parameters as hash
8
10
  def self.parameters(method)
9
11
  method #map argument types
10
12
  .parameters
11
13
  .map{|ty, na| [na.to_s, ty]}
12
14
  .to_h
13
15
  end
16
+
17
+ # parse arguments
18
+ def self.interpret(arg)
19
+ data = YAML.load("---\n[#{arg}]")
20
+ data.class == Array && data.length == 1 ? data[0] : data
21
+ end
22
+
23
+ # wrap a function
14
24
  def self.wrap(instance,method)
15
25
  name = method.name
16
26
  arguments = ::Byggvir.parameters(method)
27
+ # Check that function has no non-keyword arguments
17
28
  if (badarg = (arguments.values.to_set - [:keyreq, :key].to_set)).empty?
18
- required_arguments = arguments.select{|k,v| v == :keyreq}.keys.to_set
29
+ required_arguments = arguments
30
+ .select{|k,v| v == :keyreq}
31
+ .keys
32
+ .map(&:to_sym)
33
+ .to_set
34
+ # Single-letter arguments, first letter, except for duplicates
19
35
  short = arguments
20
36
  .keys
21
37
  .map{|k| {(?- + k[0]) => k}} #prepend dash
22
- .reduce{|old, new| old.merge(new){|k, o, n| nil}} #duplicates are uncertain
38
+ .reduce({}){|old, new| old.merge(new){|k, o, n| nil}} #Drop duplicates
23
39
  .select{|k, v| v != nil}
24
40
  .invert
25
41
  opts = GetoptLong.new(*arguments.map{|k, v| ["--#{k}", short[k], GetoptLong::REQUIRED_ARGUMENT].compact})
26
42
  opts.quiet = true
43
+
44
+ # Use getoptlong to parse arguments,
27
45
  call = {}
28
- unwrap = proc {|w| (w.class == Array && w.length == 1)?w[0]:w }
29
46
  begin
30
- opts.each{|opt, arg| call[opt[2..-1].to_sym] = unwrap.call(YAML.load("---\n[#{arg}]"))}
47
+ opts.each{|opt, arg| call[opt[2..-1].to_sym] = ::Byggvir.interpret(arg)}
31
48
  rescue GetoptLong::InvalidOption => ex
32
49
  ::Byggvir.error!(ex.to_s)
33
50
  else
34
- if (missing_arguments = (required_arguments - call.keys.map(&:to_s).to_set)).empty?
51
+ # Call function if all arguments are present
52
+ if (missing_arguments = (required_arguments - call.keys.to_set)).empty?
35
53
  instance.send(name,**call)
36
54
  else
37
55
  ::Byggvir.error!("Missing arguments to #{name}: #{missing_arguments.to_a.join(?,)}")
38
56
  end
39
57
  end
40
58
  else
41
- ::Byggvir.error!("Doesn't support non-hash like function calls. Need something like def(required: ,optional: thing), bad arguments #{badarg}")
59
+ ::Byggvir.error!("#{name} is not defined correctly, bad arguments #{badarg}\nByggvir can only use optional or mandatory keyword arguments like def(something: other:val)")
42
60
  end
43
61
  end
44
62
  end
@@ -1,3 +1,3 @@
1
1
  module Byggvir
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env rvm 2.1.0 do ruby
2
+ $:.push('../lib')
3
+ require 'byggvir'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: byggvir
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Kleiner
@@ -17,22 +17,23 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
- - README.md
21
- - lib/byggvir.rb
22
20
  - lib/byggvir/simple.rb
23
21
  - lib/byggvir/static.rb
24
22
  - lib/byggvir/version.rb
23
+ - lib/byggvir.rb
24
+ - README.md
25
25
  - test/doctest.rb
26
- - test/doctest.rb~
27
26
  - test/easy.rb
28
- - test/easy.rb~
29
27
  - test/failing.rb
30
- - test/failing.rb~
31
28
  - test/multiple.rb
32
- - test/multiple.rb~
33
29
  - test/test.rb
34
- - test/testfrompe.rb~
35
30
  - test/update_local.rb
31
+ - test/cli.rb~
32
+ - test/doctest.rb~
33
+ - test/easy.rb~
34
+ - test/failing.rb~
35
+ - test/multiple.rb~
36
+ - test/testfrompe.rb~
36
37
  - test/update_local.rb~
37
38
  homepage: https://github.com/cultureulterior/byggvir
38
39
  licenses:
@@ -44,21 +45,22 @@ require_paths:
44
45
  - lib
45
46
  required_ruby_version: !ruby/object:Gem::Requirement
46
47
  requirements:
47
- - - '='
48
+ - - '>='
48
49
  - !ruby/object:Gem::Version
49
50
  version: '2.1'
50
51
  required_rubygems_version: !ruby/object:Gem::Requirement
51
52
  requirements:
52
- - - ">="
53
+ - - '>='
53
54
  - !ruby/object:Gem::Version
54
55
  version: '0'
55
56
  requirements: []
56
57
  rubyforge_project:
57
- rubygems_version: 2.2.1
58
+ rubygems_version: 2.0.14
58
59
  signing_key:
59
60
  specification_version: 3
60
61
  summary: Command line tool for ruby2.1
61
62
  test_files:
63
+ - test/cli.rb~
62
64
  - test/doctest.rb
63
65
  - test/doctest.rb~
64
66
  - test/easy.rb