autoargs 0.1.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
+ SHA1:
3
+ metadata.gz: a4904378daa3039b791b8631f164adbc182f3c5a
4
+ data.tar.gz: 2731046da2d3e254361f1a4f88337f790afc612d
5
+ SHA512:
6
+ metadata.gz: 719b82c31a0028421217aa024c2408132f32aa1f89ee25314c570d969a364630b3835b48fee0c58d01c30ce7a41f26df1341c40a8d646b5fd51d06326b45ff3a
7
+ data.tar.gz: 0524db0755ffc9473352329221343ec42235da886172a531787a052293fe6182af5338e296cca25330505c6ea02e5638fd0c106202cfa5ff898afc2055cf5414
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in autoargs.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # autoargs
2
+
3
+ autoargs automatically parses command-line arguments given a method by inspecting its signature.
4
+
5
+ ```ruby
6
+ # test.rb
7
+ require 'autoargs'
8
+
9
+ def main(foo, bar='bar', baz: 'baz')
10
+ puts(foo, bar, baz)
11
+ end
12
+
13
+ Autoargs::run(method(:main))
14
+
15
+ # $ ruby test.rb one two --baz three
16
+ # one
17
+ # two
18
+ # three
19
+ ```
20
+
21
+ ## Installation
22
+
23
+ Add this line to your application's Gemfile:
24
+
25
+ ```ruby
26
+ gem 'autoargs'
27
+ ```
28
+
29
+ And then execute:
30
+
31
+ $ bundle
32
+
33
+ Or install it yourself as:
34
+
35
+ $ gem install autoargs
36
+
37
+ ## Development
38
+
39
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
40
+
41
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/autoargs.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'autoargs/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "autoargs"
8
+ spec.version = Autoargs::VERSION
9
+ spec.authors = ["Chris Wendt"]
10
+ spec.email = ["chrismwendt@gmail.com"]
11
+
12
+ spec.summary = %q{Automatically parses command-line arguments}
13
+ spec.homepage = "https://github.com/chrismwendt/autoargs"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "parser"
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.12.a"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "autoargs"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module Autoargs
2
+ VERSION = "0.1.0"
3
+ end
data/lib/autoargs.rb ADDED
@@ -0,0 +1,58 @@
1
+ require 'autoargs/version'
2
+ require 'parser/current'
3
+
4
+ module Autoargs
5
+ def self.run(method)
6
+ caller_file = caller[0].split(":")[0]
7
+
8
+ if caller_file == $0
9
+ ast = Parser::CurrentRuby
10
+ .parse(File.read(caller_file))
11
+ .children
12
+ .select do |node| node.type == :def end
13
+ .map(&:children)
14
+ .select do |name, _| name == method.name end[0]
15
+
16
+ abort("No top level method named " + method.name.to_s + " found.") if ast.nil?
17
+
18
+ all_args = ast[1].children
19
+
20
+ args = all_args
21
+ .select do |arg| arg.type == :arg end
22
+ .map do |arg| arg.children[0] end
23
+
24
+ optargs = all_args
25
+ .select do |arg| arg.type == :optarg end
26
+ .map do |arg| { :name => arg.children[0], :default => arg.children[1].children[0] } end
27
+
28
+ kwoptargs = Hash[
29
+ all_args
30
+ .select do |arg| arg.type == :kwoptarg end
31
+ .map do |arg| [arg.children[0], arg.children[1].children[0]] end
32
+ ]
33
+
34
+ argv_args = ARGV[0 ... args.length]
35
+ argv_optargs = ARGV[args.length ... args.length + optargs.length]
36
+ argv_kwoptargs = Hash[
37
+ (ARGV[args.length + optargs.length .. -1] || [])
38
+ .each_slice(2)
39
+ .map do |name_with_prefix, value|
40
+ abort("Expected argument " + name_with_prefix + " to start with --.") unless name_with_prefix.start_with?("--")
41
+ name = name_with_prefix[2 .. -1]
42
+ abort(name + " is not an option.") unless kwoptargs.has_key?(name.to_sym)
43
+ [name.to_sym, value]
44
+ end
45
+ ]
46
+
47
+ if argv_args.length < args.length
48
+ abort([[caller_file],
49
+ args.map do |arg| "<" + arg.to_s + ">" end,
50
+ optargs.map do |optarg| "[" + optarg[:name].to_s + "=" + optarg[:default].inspect + "]" end,
51
+ kwoptargs.map do |name, default| "[--" + name.to_s + " " + default.inspect + "]" end
52
+ ].flatten(1).join(" "))
53
+ end
54
+
55
+ method.call(*ARGV.slice(0, args.length + optargs.length), **argv_kwoptargs)
56
+ end
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: autoargs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris Wendt
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: parser
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.12.a
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.12.a
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description:
56
+ email:
57
+ - chrismwendt@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - README.md
65
+ - Rakefile
66
+ - autoargs.gemspec
67
+ - bin/console
68
+ - bin/setup
69
+ - lib/autoargs.rb
70
+ - lib/autoargs/version.rb
71
+ homepage: https://github.com/chrismwendt/autoargs
72
+ licenses: []
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.2.5
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Automatically parses command-line arguments
94
+ test_files: []