cmdline_arg_parser 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9359a93f712f7d228c5d1dbab3c551a1b86f471a
4
+ data.tar.gz: 900cf435687f0a00d7f43c72ccfc54e66ecccacf
5
+ SHA512:
6
+ metadata.gz: 0de95ecb7c9e83d4f1deaa4a58d5692bf4adf88008fcbacd1f162051afa9bfa5c09b6633330a307c06e791d1ec0e4c2aa93ecf65d0eef023ee6c09a98d6b122c
7
+ data.tar.gz: c270e607d5da96b94ba6e148be2e3534093ebc8386d2317bc9e6c3aff21d09c29263465cb73c5cad73fb22760f5cd4180a0a05c6b7ef0931583c33d366610486
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/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in cmdline_arg_parser.gemspec
6
+ gemspec
data/README.markdown ADDED
@@ -0,0 +1,58 @@
1
+ # Command-line argument parser
2
+
3
+ A small library for parsing command-line arguments in Ruby.
4
+
5
+ Designed to be used with command-line tools like `git` which have a set of subcommands that each have different options and switches.
6
+
7
+ ## Sample
8
+
9
+ ```ruby
10
+ require "cmdline_arg_parser"
11
+
12
+ # Define a parser using the DSL
13
+ class ParserFromDsl
14
+ extend CmdlineArgParser::Dsl
15
+
16
+ subcommand "merge" do
17
+ option "branch", short_key: "b"
18
+ option("from-step") { |value| value.to_i }
19
+ switch "dry-run"
20
+ end
21
+
22
+ subcommand "version"
23
+ end
24
+
25
+ # Have some ARGV
26
+ ARGV = ["merge", "--dry-run", "--from-step", "10", "-b", "release-branch"]
27
+
28
+ args = ParserFromDsl.parse(ARGV)
29
+
30
+ # Accessing the parsed data
31
+ args.subcommand # => "merge"
32
+ args.options # => { "branch" => "release-branch", "from-step" => 10 }
33
+ args.switches # => Set.new(["dry-run"])
34
+ ```
35
+
36
+ If you don't want to use the DSL, the above parser can also be written as:
37
+
38
+ ```ruby
39
+ parser = CmdlineArgParser::Parser.new(
40
+ subcommands: [
41
+ CmdlineArgParser::Parser::Subcommand.new(
42
+ "merge",
43
+ options: [
44
+ CmdlineArgParser::Parser::Option.new("branch", short_key: "b"),
45
+ CmdlineArgParser::Parser::Option.new("from-step") { |value| value.to_i },
46
+ ],
47
+ switches: [
48
+ CmdlineArgParser::Parser::Switch.new("dry-run"),
49
+ ],
50
+ ),
51
+ CmdlineArgParser::Parser::Subcommand.new("version"),
52
+ ]
53
+ )
54
+ ```
55
+
56
+ ## Built by
57
+
58
+ [@davidpdrsn](https://twitter.com/davidpdrsn)
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # CmdlineArgParser
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cmdline_arg_parser`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'cmdline_arg_parser'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install cmdline_arg_parser
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/davidpdrsn/cmdline_arg_parser.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cmdline_arg_parser"
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(__FILE__)
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,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "cmdline_arg_parser/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cmdline_arg_parser"
8
+ spec.version = CmdlineArgParser::VERSION
9
+ spec.authors = ["David Pedersen"]
10
+ spec.email = ["david@tonsser.com"]
11
+
12
+ spec.summary = %q{Simple command line argument parsing}
13
+ spec.homepage = "https://github.com/tonsser/cmdline_arg_parser"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.15"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest", "~> 5.0"
25
+ end
@@ -0,0 +1,44 @@
1
+ module CmdlineArgParser
2
+ module Dsl
3
+ def subcommand(name, &block)
4
+ @subcommands ||= []
5
+
6
+ subcommand_dsl = SubcommandDsl.new
7
+ if block
8
+ subcommand_dsl.instance_eval(&block)
9
+ end
10
+
11
+ @subcommands << Parser::Subcommand.new(
12
+ name,
13
+ options: subcommand_dsl.options,
14
+ switches: subcommand_dsl.switches,
15
+ )
16
+ end
17
+
18
+ def parse(argv)
19
+ parser.parse(argv)
20
+ end
21
+
22
+ def parser
23
+ @subcommands ||= []
24
+ @parser ||= Parser.new(subcommands: @subcommands)
25
+ end
26
+
27
+ class SubcommandDsl
28
+ def initialize
29
+ @options = []
30
+ @switches = []
31
+ end
32
+
33
+ attr_reader :options, :switches
34
+
35
+ def option(name, short_key: nil, &block)
36
+ @options << Parser::Option.new(name, short_key: short_key, &block)
37
+ end
38
+
39
+ def switch(name, short_key: nil)
40
+ @switches << Parser::Switch.new(name, short_key: short_key)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,81 @@
1
+ module CmdlineArgParser
2
+ class ReadmeBuilder
3
+ def initialize(parser:, specifics:)
4
+ @parser = parser
5
+ @specifics = specifics
6
+ end
7
+
8
+ def build
9
+ output = <<-EOS
10
+ # #{@specifics.title}
11
+
12
+ #{@specifics.description}
13
+
14
+ ## Subcommands
15
+
16
+ EOS
17
+
18
+ subcommand_descriptions = @parser.subcommands.map do |subcommand|
19
+ subcommand_description = <<-EOS
20
+ $ #{@specifics.script_name} #{subcommand.command}
21
+ #{@specifics.description_for_subcommand(subcommand.command)}
22
+ EOS
23
+
24
+ if subcommand.options != [] || subcommand.switches != []
25
+ subcommand_description += indent("\n## Options\n", 2)
26
+
27
+ option_descriptions = subcommand.options.map do |option|
28
+ option_label = @specifics.option_label_for(
29
+ subcommand: subcommand.command,
30
+ option_key: option.long_key,
31
+ )
32
+
33
+ lines = []
34
+ lines << "--#{option.long_key} #{option_label}"
35
+
36
+ if option.short_key
37
+ lines << "-#{option.short_key} #{option_label}"
38
+ end
39
+
40
+ option_description = @specifics.option_description(
41
+ subcommand: subcommand.command,
42
+ option_key: option.long_key,
43
+ )
44
+ lines << indent("#{option_description}\n", 2)
45
+
46
+ lines.join("\n")
47
+ end
48
+
49
+ switch_descriptions = subcommand.switches.map do |switch|
50
+ lines = []
51
+ lines << "--#{switch.long_key}"
52
+ if switch.short_key
53
+ lines << "-#{short_key.long_key}"
54
+ end
55
+ lines << @specifics.switch_label(
56
+ subcommand: subcommand.command,
57
+ switch_key: switch.long_key,
58
+ )
59
+ lines.join("\n")
60
+ end
61
+
62
+ subcommand_description += indent(option_descriptions.join("\n"), 4)
63
+ subcommand_description += indent(switch_descriptions.join("\n"), 4)
64
+ end
65
+
66
+ indent(subcommand_description, 2)
67
+ end
68
+ output += subcommand_descriptions.join("\n")
69
+
70
+ output.chomp
71
+ end
72
+
73
+ private
74
+
75
+ def indent(str, amount)
76
+ str.lines.map do |line|
77
+ "#{' ' * amount}#{line}"
78
+ end.join
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,3 @@
1
+ module CmdlineArgParser
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,128 @@
1
+ require "cmdline_arg_parser/version"
2
+ require "cmdline_arg_parser/readme_builder"
3
+ require "cmdline_arg_parser/dsl"
4
+
5
+ module CmdlineArgParser
6
+ class Parser
7
+ class ParseError < RuntimeError; end
8
+
9
+ def initialize(subcommands:)
10
+ @subcommands = subcommands
11
+ end
12
+
13
+ attr_reader :subcommands
14
+
15
+ def parse(argv)
16
+ parsed_args = ParsedArgs.new
17
+
18
+ @subcommands.each do |subcommand|
19
+ subcommand.try_parse(argv, parsed_args)
20
+ end
21
+
22
+ unless argv == []
23
+ raise ParseError, "Expected end of input, but got \"#{argv.join(' ')}\""
24
+ end
25
+
26
+ parsed_args
27
+ end
28
+
29
+ def build_readme(builder)
30
+ ReadmeBuilder.new(parser: self, specifics: builder).build
31
+ end
32
+
33
+ class Subcommand
34
+ def initialize(command, options: [], switches: [])
35
+ @command = command
36
+ @options = options
37
+ @switches = switches
38
+ end
39
+
40
+ attr_reader :command, :options, :switches
41
+
42
+ def try_parse(argv, out)
43
+ return unless argv[0] == @command
44
+
45
+ out.subcommand = argv.shift
46
+ @options.each { |option| option.parse(argv, out) }
47
+ @switches.each { |switch| switch.parse(argv, out) }
48
+ end
49
+ end
50
+
51
+ class Option
52
+ def initialize(long_key, short_key: nil, &block)
53
+ @long_key = long_key
54
+ @short_key = short_key
55
+ @block = block
56
+ end
57
+
58
+ attr_reader :short_key, :long_key
59
+
60
+ def parse(argv, out)
61
+ index_of_key = argv.find_index do |word|
62
+ word == "--#{@long_key}" || word == "-#{@short_key}"
63
+ end
64
+ index_of_value = index_of_key + 1
65
+
66
+ value = argv[index_of_value]
67
+ if @block
68
+ value = @block.call(value)
69
+ end
70
+ out.set_option(@long_key, value)
71
+
72
+ argv.delete_at(index_of_key)
73
+ argv.delete_at(index_of_value - 1)
74
+ end
75
+ end
76
+
77
+ class Switch
78
+ def initialize(long_key, short_key: nil)
79
+ @long_key = long_key
80
+ @short_key = short_key
81
+ end
82
+
83
+ attr_reader :long_key, :short_key
84
+
85
+ def parse(argv, out)
86
+ index_of_key = argv.find_index do |word|
87
+ word == "--#{@long_key}" || word == "-#{@short_key}"
88
+ end
89
+
90
+ if index_of_key
91
+ out.set_switch(@long_key)
92
+ end
93
+
94
+ argv.delete_at(index_of_key)
95
+ end
96
+ end
97
+ end
98
+
99
+ class ParsedArgs
100
+ attr_reader(
101
+ :subcommand,
102
+ )
103
+
104
+ attr_writer(
105
+ :subcommand,
106
+ :options,
107
+ :switches,
108
+ )
109
+
110
+ def options
111
+ @options ||= {}
112
+ end
113
+
114
+ def switches
115
+ @switches ||= Set.new
116
+ end
117
+
118
+ def set_option(key, value)
119
+ @options ||= {}
120
+ @options[key] = value
121
+ end
122
+
123
+ def set_switch(key)
124
+ @switches ||= Set.new
125
+ @switches << key
126
+ end
127
+ end
128
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cmdline_arg_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - David Pedersen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-11-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description:
56
+ email:
57
+ - david@tonsser.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - README.markdown
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - cmdline_arg_parser.gemspec
71
+ - lib/cmdline_arg_parser.rb
72
+ - lib/cmdline_arg_parser/dsl.rb
73
+ - lib/cmdline_arg_parser/readme_builder.rb
74
+ - lib/cmdline_arg_parser/version.rb
75
+ homepage: https://github.com/tonsser/cmdline_arg_parser
76
+ licenses: []
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.5.1
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Simple command line argument parsing
98
+ test_files: []