cmdline_arg_parser 0.1.2 → 0.1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8d3d346c457fa1fe1d4043c7a90f7ac97a96d785
4
- data.tar.gz: 957371cea616ca30cb503eaa1f133c2390f58876
3
+ metadata.gz: 974f92a6832ab5e261f1d07ce4200498b722f794
4
+ data.tar.gz: 6bf37cab4da9025a898c250f5265194881354c37
5
5
  SHA512:
6
- metadata.gz: 9adaf0a06b11c270248852c0d0539e1a73d8f90e30669df446e00744c657036af8beb27d01c2aaab75629ae32f590f56ec33f1c71a0427e629a5a0a7a81c3756
7
- data.tar.gz: 834352bbc193faa1932cfa6c73cf28ff415695603d6a4fd7f1f5c992a20cf7e0909de9d70b2a79b979eac00875a838792cf100a0fbf1269f900cab2416336c26
6
+ metadata.gz: 3e0bb11a936208f25732fa19ee0c45778553af7b03c8e134f88fe1f286d36c42ab3bc1af9e43e8b3383bf9cc8365dd7159664108e20360f1cf4a030668808a00
7
+ data.tar.gz: 329e1baeb2fa05bc28c706da4f5c0e72c52847847959c2f81a7995991f829801a28ac6e547d5dd6bc107949d867d0d912117efb9af241693731086092c61cfa2
data/README.markdown CHANGED
@@ -4,6 +4,8 @@ A small library for parsing command-line arguments in Ruby.
4
4
 
5
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
6
 
7
+ [On rubygems.org](https://rubygems.org/gems/cmdline_arg_parser)
8
+
7
9
  ## Sample
8
10
 
9
11
  ```ruby
@@ -14,7 +16,8 @@ class ParserFromDsl
14
16
  extend CmdlineArgParser::Dsl
15
17
 
16
18
  subcommand "merge" do
17
- option "branch", short_key: "b"
19
+ option "branches", multiple: true, short_key: "b"
20
+ option "into", default: "master"
18
21
  option("from-step") { |value| value.to_i }
19
22
  switch "dry-run"
20
23
  end
@@ -23,13 +26,13 @@ class ParserFromDsl
23
26
  end
24
27
 
25
28
  # Have some ARGV
26
- ARGV = ["merge", "--dry-run", "--from-step", "10", "-b", "release-branch"]
29
+ ARGV = ["merge", "--dry-run", "--from-step", "10", "-b", "release-branch", "other-branch"]
27
30
 
28
31
  args = ParserFromDsl.parse(ARGV)
29
32
 
30
33
  # Accessing the parsed data
31
34
  args.subcommand # => "merge"
32
- args.options # => { "branch" => "release-branch", "from-step" => 10 }
35
+ args.options # => { "into" => "master", "branches" => ["release-branch", "other-branch"], "from-step" => 10 }
33
36
  args.switches # => Set.new(["dry-run"])
34
37
  ```
35
38
 
@@ -41,7 +44,8 @@ parser = CmdlineArgParser::Parser.new(
41
44
  CmdlineArgParser::Parser::Subcommand.new(
42
45
  "merge",
43
46
  options: [
44
- CmdlineArgParser::Parser::Option.new("branch", short_key: "b"),
47
+ CmdlineArgParser::Parser::Option.new("branches", multiple: true, short_key: "b"),
48
+ CmdlineArgParser::Parser::Option.new("into", default: "master"),
45
49
  CmdlineArgParser::Parser::Option.new("from-step") { |value| value.to_i },
46
50
  ],
47
51
  switches: [
@@ -53,6 +57,26 @@ parser = CmdlineArgParser::Parser.new(
53
57
  )
54
58
  ```
55
59
 
60
+ ### Readme generation
61
+
62
+ TODO
63
+
64
+ ## Installation
65
+
66
+ Add this line to your application's Gemfile:
67
+
68
+ ```ruby
69
+ gem "cmdline_arg_parser"
70
+ ```
71
+
72
+ And then execute:
73
+
74
+ $ bundle
75
+
76
+ Or install it yourself as:
77
+
78
+ $ gem install cmdline_arg_parser
79
+
56
80
  ## Built by
57
81
 
58
82
  [@davidpdrsn](https://twitter.com/davidpdrsn)
@@ -36,8 +36,14 @@ module CmdlineArgParser
36
36
 
37
37
  attr_reader :options, :switches
38
38
 
39
- def option(name, short_key: nil, &block)
40
- @options << Parser::Option.new(name, short_key: short_key, &block)
39
+ def option(name, default: nil, multiple: false, short_key: nil, &block)
40
+ @options << Parser::Option.new(
41
+ name,
42
+ multiple: multiple,
43
+ short_key: short_key,
44
+ default: default,
45
+ &block
46
+ )
41
47
  end
42
48
 
43
49
  def switch(name, short_key: nil)
@@ -37,24 +37,29 @@ $ #{@specifics.script_name} #{subcommand.command}
37
37
  lines << "-#{option.short_key} #{option_label}"
38
38
  end
39
39
 
40
- option_description = @specifics.option_description(
41
- subcommand: subcommand.command,
42
- option_key: option.long_key,
40
+ lines << indent(
41
+ @specifics.option_description_for(
42
+ subcommand: subcommand.command,
43
+ option_key: option.long_key,
44
+ ) + "\n",
45
+ 2
43
46
  )
44
- lines << indent("#{option_description}\n", 2)
45
47
 
46
48
  lines.join("\n")
47
49
  end
48
50
 
49
51
  switch_descriptions = subcommand.switches.map do |switch|
50
- lines = []
52
+ lines = [""]
51
53
  lines << "--#{switch.long_key}"
52
54
  if switch.short_key
53
55
  lines << "-#{short_key.long_key}"
54
56
  end
55
- lines << @specifics.switch_label(
56
- subcommand: subcommand.command,
57
- switch_key: switch.long_key,
57
+ lines << indent(
58
+ @specifics.switch_label_for(
59
+ subcommand: subcommand.command,
60
+ switch_key: switch.long_key,
61
+ ),
62
+ 2
58
63
  )
59
64
  lines.join("\n")
60
65
  end
@@ -63,11 +68,11 @@ $ #{@specifics.script_name} #{subcommand.command}
63
68
  subcommand_description += indent(switch_descriptions.join("\n"), 4)
64
69
  end
65
70
 
66
- indent(subcommand_description, 2)
71
+ indent(subcommand_description, 2) + "\n"
67
72
  end
68
73
  output += subcommand_descriptions.join("\n")
69
74
 
70
- output.chomp
75
+ output.chomp.gsub("\n\n\n", "\n\n")
71
76
  end
72
77
 
73
78
  private
@@ -1,3 +1,3 @@
1
1
  module CmdlineArgParser
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -50,9 +50,15 @@ module CmdlineArgParser
50
50
  end
51
51
 
52
52
  class Option
53
- def initialize(long_key, short_key: nil, &block)
53
+ def initialize(long_key, default: nil, multiple: false, short_key: nil, &block)
54
+ if short_key && short_key.length != 1
55
+ raise ArgumentError, "`short_key` can only be one character, was #{short_key.inspect}"
56
+ end
57
+
54
58
  @long_key = long_key
55
59
  @short_key = short_key
60
+ @multiple = multiple
61
+ @default = default
56
62
  @block = block
57
63
  end
58
64
 
@@ -62,21 +68,49 @@ module CmdlineArgParser
62
68
  index_of_key = argv.find_index do |word|
63
69
  word == "--#{@long_key}" || word == "-#{@short_key}"
64
70
  end
65
- index_of_value = index_of_key + 1
66
71
 
67
- value = argv[index_of_value]
68
- if @block
69
- value = @block.call(value)
72
+ if index_of_key.nil?
73
+ if @default.nil?
74
+ msg = "Missing argument --#{@long_key}"
75
+ if @short_key
76
+ msg += " or -#{@short_key}"
77
+ end
78
+ raise ParseError, msg
79
+ else
80
+ out.set_option(@long_key, @default)
81
+ return
82
+ end
70
83
  end
71
- out.set_option(@long_key, value)
72
84
 
73
- argv.delete_at(index_of_key)
74
- argv.delete_at(index_of_value - 1)
85
+ index_of_value = index_of_key + 1
86
+
87
+ if @multiple
88
+ argv.delete_at(index_of_key)
89
+ values = argv.take_while { |value| !(value =~ /^-/) }
90
+ values.length.times { argv.delete_at(0) }
91
+
92
+ if @block
93
+ values = values.map { |value| @block.call(value) }
94
+ end
95
+ out.set_option(@long_key, values)
96
+ else
97
+ value = argv[index_of_value]
98
+ if @block
99
+ value = @block.call(value)
100
+ end
101
+ out.set_option(@long_key, value)
102
+ argv.delete_at(index_of_key)
103
+ argv.delete_at(index_of_value - 1)
104
+ end
75
105
  end
76
106
  end
77
107
 
78
108
  class Switch
79
109
  def initialize(long_key, short_key: nil)
110
+ if short_key && short_key.length != 1
111
+ raise ArgumentError, "`short_key` can only be one character"
112
+ end
113
+
80
114
  @long_key = long_key
81
115
  @short_key = short_key
82
116
  end
@@ -90,9 +124,8 @@ module CmdlineArgParser
90
124
 
91
125
  if index_of_key
92
126
  out.set_switch(@long_key)
127
+ argv.delete_at(index_of_key)
93
128
  end
94
-
95
- argv.delete_at(index_of_key)
96
129
  end
97
130
  end
98
131
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cmdline_arg_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Pedersen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-11-20 00:00:00.000000000 Z
11
+ date: 2017-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -63,7 +63,6 @@ files:
63
63
  - ".travis.yml"
64
64
  - Gemfile
65
65
  - README.markdown
66
- - README.md
67
66
  - Rakefile
68
67
  - bin/console
69
68
  - bin/setup
data/README.md DELETED
@@ -1,37 +0,0 @@
1
- # CmdlineArgParser
2
-
3
- [On rubygems.org](https://rubygems.org/gems/cmdline_arg_parser)
4
-
5
- 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.
6
-
7
- TODO: Delete this and the text above, and describe your gem
8
-
9
- ## Installation
10
-
11
- Add this line to your application's Gemfile:
12
-
13
- ```ruby
14
- gem 'cmdline_arg_parser'
15
- ```
16
-
17
- And then execute:
18
-
19
- $ bundle
20
-
21
- Or install it yourself as:
22
-
23
- $ gem install cmdline_arg_parser
24
-
25
- ## Usage
26
-
27
- TODO: Write usage instructions here
28
-
29
- ## Development
30
-
31
- 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.
32
-
33
- 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).
34
-
35
- ## Contributing
36
-
37
- Bug reports and pull requests are welcome on GitHub at https://github.com/davidpdrsn/cmdline_arg_parser.