command_line_helper 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. data/Gemfile +22 -0
  2. data/LICENSE +22 -0
  3. data/README.md +70 -0
  4. data/Rakefile +23 -0
  5. data/command_line_helper.gemspec +20 -0
  6. data/lib/command_line_helper.rb +8 -0
  7. data/lib/command_line_helper.rb~ +8 -0
  8. data/lib/command_line_helper/base.rb~ +70 -0
  9. data/lib/command_line_helper/help.rb~ +67 -0
  10. data/lib/command_line_helper/help_text.rb +67 -0
  11. data/lib/command_line_helper/helper.rb~ +59 -0
  12. data/lib/command_line_helper/program_name.rb +9 -0
  13. data/lib/command_line_helper/program_name.rb~ +12 -0
  14. data/lib/command_line_helper/version.rb +3 -0
  15. data/spec/lib/command_line_helper/base_spec.rb~ +73 -0
  16. data/spec/lib/command_line_helper/help_text_spec.rb +65 -0
  17. data/spec/lib/command_line_helper/help_text_spec.rb~ +79 -0
  18. data/spec/lib/command_line_helper/helper.rb~ +23 -0
  19. data/spec/lib/command_line_helper/helper_spec.rb~ +22 -0
  20. data/spec/lib/command_line_helper/program_name_spec.rb +20 -0
  21. data/spec/lib/command_line_helper/program_name_spec.rb~ +20 -0
  22. data/spec/reports/SPEC-CommandLineHelper-Base-help-info.0.xml +11 -0
  23. data/spec/reports/SPEC-CommandLineHelper-Base-help-info.1.xml +11 -0
  24. data/spec/reports/SPEC-CommandLineHelper-Base-help-info.2.xml +11 -0
  25. data/spec/reports/SPEC-CommandLineHelper-Base-help-info.3.xml +11 -0
  26. data/spec/reports/SPEC-CommandLineHelper-Base-help-info.xml +11 -0
  27. data/spec/reports/SPEC-CommandLineHelper-Base-option-details.0.xml +9 -0
  28. data/spec/reports/SPEC-CommandLineHelper-Base-option-details.1.xml +9 -0
  29. data/spec/reports/SPEC-CommandLineHelper-Base-option-details.2.xml +9 -0
  30. data/spec/reports/SPEC-CommandLineHelper-Base-option-details.3.xml +9 -0
  31. data/spec/reports/SPEC-CommandLineHelper-Base-option-details.xml +9 -0
  32. data/spec/reports/SPEC-CommandLineHelper-Base-program-name.0.xml +13 -0
  33. data/spec/reports/SPEC-CommandLineHelper-Base-program-name.1.xml +13 -0
  34. data/spec/reports/SPEC-CommandLineHelper-Base-program-name.2.xml +13 -0
  35. data/spec/reports/SPEC-CommandLineHelper-Base-program-name.3.xml +13 -0
  36. data/spec/reports/SPEC-CommandLineHelper-Base-program-name.xml +13 -0
  37. data/spec/reports/SPEC-CommandLineHelper-Base-short-hand-option.0.xml +11 -0
  38. data/spec/reports/SPEC-CommandLineHelper-Base-short-hand-option.1.xml +11 -0
  39. data/spec/reports/SPEC-CommandLineHelper-Base-short-hand-option.2.xml +11 -0
  40. data/spec/reports/SPEC-CommandLineHelper-Base-short-hand-option.3.xml +11 -0
  41. data/spec/reports/SPEC-CommandLineHelper-Base-short-hand-option.xml +11 -0
  42. data/spec/reports/SPEC-CommandLineHelper-Base.0.xml +7 -0
  43. data/spec/reports/SPEC-CommandLineHelper-Base.1.xml +9 -0
  44. data/spec/reports/SPEC-CommandLineHelper-Base.2.xml +7 -0
  45. data/spec/reports/SPEC-CommandLineHelper-Base.3.xml +9 -0
  46. data/spec/reports/SPEC-CommandLineHelper-Base.4.xml +7 -0
  47. data/spec/reports/SPEC-CommandLineHelper-Base.5.xml +9 -0
  48. data/spec/reports/SPEC-CommandLineHelper-Base.6.xml +7 -0
  49. data/spec/reports/SPEC-CommandLineHelper-Base.7.xml +9 -0
  50. data/spec/reports/SPEC-CommandLineHelper-Base.8.xml +7 -0
  51. data/spec/reports/SPEC-CommandLineHelper-Base.xml +9 -0
  52. data/spec/reports/SPEC-CommandLineHelper-HelpText-help-info.xml +11 -0
  53. data/spec/reports/SPEC-CommandLineHelper-HelpText-option-details.xml +9 -0
  54. data/spec/reports/SPEC-CommandLineHelper-HelpText-short-hand-option.xml +11 -0
  55. data/spec/reports/SPEC-CommandLineHelper-HelpText.0.xml +7 -0
  56. data/spec/reports/SPEC-CommandLineHelper-HelpText.xml +9 -0
  57. data/spec/reports/SPEC-CommandLineHelper-ProgramName-program-name.xml +11 -0
  58. data/spec/reports/SPEC-CommandLineHelper-ProgramName.xml +7 -0
  59. data/spec/spec_helper.rb +23 -0
  60. data/spec/spec_helper.rb~ +20 -0
  61. metadata +169 -0
data/Gemfile ADDED
@@ -0,0 +1,22 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in command_line_helper.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem "guard"
8
+ gem "guard-bundler"
9
+ gem "guard-rspec"
10
+ gem "rb-fchange", :require => false
11
+ gem "rb-fsevent", :require => false
12
+ gem "rb-inotify", :require => false
13
+ gem "terminal-notifier-guard"
14
+ end
15
+
16
+ group :test do
17
+ gem "ci_reporter"
18
+ gem "rake"
19
+ gem "rspec"
20
+ gem "simplecov", :require => false
21
+ gem "simplecov-rcov"
22
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Juan C. Müller
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # CommandLineHelper
2
+
3
+ This helps other gems with their common CLI chores.
4
+
5
+ At the moment, it generates a nice help output based on `options_possible` defined in the
6
+ class that includes `CommandLineHelper::HelpText`.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'command_line_helper'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install command_line_helper
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ require 'command_line_helper'
26
+
27
+ class CLI
28
+ include CommandLineHelper::HelpText
29
+
30
+ def do_something
31
+ puts help_info
32
+ end
33
+
34
+ private
35
+
36
+ def options_possible
37
+ [
38
+ ['--foo', '-f', GetoptLong::REQUIRED_ARGUMENT, "This option foo takes an argument"],
39
+ ['--bar', '-f', GetoptLong::NO_ARGUMENT, "This option bar does not take an argument"]
40
+ ]
41
+ end
42
+
43
+ def version_info
44
+ "This is version BLAH of this amazing package. (c) Someone Somewhere Today."
45
+ end
46
+
47
+ end
48
+
49
+ CLI.new.do_something
50
+ ```
51
+
52
+ ```bash
53
+ $ ruby ./cli.rb
54
+ Usage: cli.rb [options]
55
+ [--foo|-f argument], [--bar|-f]
56
+
57
+ Options:
58
+ --foo, -f This option foo takes an argument
59
+ --bar, -f This option bar does not take an argument
60
+
61
+ This is version BLAH of this amazing package. (c) Someone Somewhere Today.
62
+ ```
63
+
64
+ ## Contributing
65
+
66
+ 1. Fork it
67
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
68
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
69
+ 4. Push to the branch (`git push origin my-new-feature`)
70
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env rake
2
+
3
+ $:.push File.expand_path("../lib", __FILE__)
4
+
5
+ require "rspec/core/rake_task"
6
+ require "command_line_helper/version"
7
+ require "bundler/gem_tasks"
8
+
9
+ RSpec::Core::RakeTask.new(:spec) do |spec|
10
+ spec.pattern = "spec/**/*_spec.rb"
11
+ spec.rspec_opts = ["--backtrace --format CI::Reporter::RSpec"]
12
+ end
13
+
14
+ desc "Clean backup and swap files, and artifacts"
15
+ task :clean do
16
+ require "fileutils"
17
+ Dir["{pkg/*,**/*~,**/.*.sw?,coverage/**,spec/reports/**}"].each do |file|
18
+ rm_rf file
19
+ end
20
+ end
21
+
22
+ desc "Run rspec by default"
23
+ task :default => :spec
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/command_line_helper/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Juan C. Müller"]
6
+ gem.email = ["jcmuller@gmail.com"]
7
+ gem.description = %q{Helper for CLI apps.}
8
+ gem.summary = %q{This library provides a module that helps with some common CLI tasks (mainly showing help texts and options)}
9
+ gem.homepage = ""
10
+
11
+ gem.files = Dir["lib/**/*"] +
12
+ %w(command_line_helper.gemspec Gemfile LICENSE Rakefile README.md)
13
+ gem.executables = []
14
+ gem.test_files = Dir["spec/**/*"]
15
+ gem.name = "command_line_helper"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = CommandLineHelper::VERSION
18
+
19
+ gem.add_dependency("activesupport")
20
+ end
@@ -0,0 +1,8 @@
1
+ require "rubygems"
2
+ require "getoptlong"
3
+
4
+ module CommandLineHelper
5
+ autoload :HelpText, 'command_line_helper/help_text'
6
+ autoload :ProgramName, 'command_line_helper/program_name'
7
+ autoload :VERSION, 'command_line_helper/version'
8
+ end
@@ -0,0 +1,8 @@
1
+ require "rubygems"
2
+ require "getoptlong"
3
+
4
+ module CommandLineHelper
5
+ autoload :Help, 'command_line_helper/help'
6
+ autoload :ProgramName, 'command_line_helper/program_name'
7
+ autoload :VERSION, 'command_line_helper/version'
8
+ end
@@ -0,0 +1,70 @@
1
+ # TODO Check for definition of options_possible
2
+ require 'active_support/concern'
3
+
4
+ module CommandLineHelper
5
+ # Depends on:
6
+ # * version_info (simply defined to return __PACKAGE__::VERSION)
7
+ # * options_possible
8
+ # [ ['--option', '-o', GetoptLong::OPTION_TYPE, 'Helpful message'], ... ]
9
+ extend ActiveSupport::Concern
10
+
11
+ module ClassMethods
12
+ def program_name
13
+ @program_name = File.basename($0)
14
+ end
15
+ end
16
+
17
+ # Help information that is constructed from the options_possible defined
18
+ #
19
+ # Returns the help text
20
+ def help_info
21
+ <<-EOH
22
+ Usage: #{program_name} [options]
23
+ #{short_hand_options}
24
+
25
+ Options:
26
+ #{option_details}
27
+ #{version_info}
28
+ EOH
29
+ end
30
+
31
+ def version_info
32
+ VERSION
33
+ end
34
+
35
+ def program_name
36
+ self.class.program_name
37
+ end
38
+
39
+ private
40
+
41
+ def options_possible
42
+ raise %{Must override this with something like [ ['--option', '-o', GetoptLong::OPTION_TYPE, 'Helpful message'], ... ]}
43
+ end
44
+
45
+ def option_details
46
+ <<-EOO
47
+ #{options_possible.map{ |o| expand_option(o) }.join("\n")}
48
+ EOO
49
+ end
50
+
51
+ def expand_option(option)
52
+ sprintf(" %-#{longest_width + 6}s %s", option.first(2).join(', '), option[3])
53
+ end
54
+
55
+ def longest_width
56
+ @max_width ||= options_possible.map{ |o| o[0] }.max{ |a, b| a.length <=> b.length }.length
57
+ end
58
+
59
+ def short_hand_options
60
+ "[#{options_possible.map{ |o| short_hand_option(o)}.join('], [')}]"
61
+ end
62
+
63
+ def short_hand_option(option)
64
+ if option[2] == GetoptLong::REQUIRED_ARGUMENT
65
+ [option[0], option[1]].join('|') << " argument"
66
+ else
67
+ [option[0], option[1]].join('|')
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,67 @@
1
+ # TODO Check for definition of options_possible
2
+ require 'active_support/concern'
3
+
4
+ module CommandLineHelper
5
+ # Depends on:
6
+ # * version_info (simply defined to return __PACKAGE__::VERSION)
7
+ # * options_possible
8
+ # [ ['--option', '-o', GetoptLong::OPTION_TYPE, 'Helpful message'], ... ]
9
+ module Help
10
+ extend ActiveSupport::Concern
11
+
12
+ included do
13
+ include CommandLineHelper::ProgramName
14
+ end
15
+
16
+ # Help information that is constructed from the options_possible defined
17
+ #
18
+ # Returns the help text
19
+ def help_info
20
+ <<-EOH
21
+ Usage: #{program_name} [options]
22
+ #{short_hand_options}
23
+
24
+ Options:
25
+ #{option_details}
26
+ #{version_info}
27
+ EOH
28
+ end
29
+
30
+ def version_info
31
+ VERSION
32
+ end
33
+
34
+ private
35
+
36
+ def options_possible
37
+ raise %{Must override this with something like [ ['--option', '-o', GetoptLong::OPTION_TYPE, 'Helpful message'], ... ]}
38
+ end
39
+
40
+ def option_details
41
+ <<-EOO
42
+ #{options_possible.map{ |o| expand_option(o) }.join("\n")}
43
+ EOO
44
+ end
45
+
46
+ def expand_option(option)
47
+ sprintf(" %-#{longest_width + 6}s %s", option.first(2).join(', '), option[3])
48
+ end
49
+
50
+ def longest_width
51
+ @max_width ||= options_possible.map{ |o| o[0] }.max{ |a, b| a.length <=> b.length }.length
52
+ end
53
+
54
+ def short_hand_options
55
+ "[#{options_possible.map{ |o| short_hand_option(o)}.join('], [')}]"
56
+ end
57
+
58
+ def short_hand_option(option)
59
+ if option[2] == GetoptLong::REQUIRED_ARGUMENT
60
+ [option[0], option[1]].join('|') << " argument"
61
+ else
62
+ [option[0], option[1]].join('|')
63
+ end
64
+ end
65
+
66
+ end
67
+ end
@@ -0,0 +1,67 @@
1
+ # TODO Check for definition of options_possible
2
+ require 'active_support/concern'
3
+
4
+ module CommandLineHelper
5
+ # Depends on:
6
+ # * version_info (simply defined to return __PACKAGE__::VERSION)
7
+ # * options_possible
8
+ # [ ['--option', '-o', GetoptLong::OPTION_TYPE, 'Helpful message'], ... ]
9
+ module HelpText
10
+ extend ActiveSupport::Concern
11
+
12
+ included do
13
+ include CommandLineHelper::ProgramName
14
+ end
15
+
16
+ # Help information that is constructed from the options_possible defined
17
+ #
18
+ # Returns the help text
19
+ def help_info
20
+ <<-EOH
21
+ Usage: #{program_name} [options]
22
+ #{short_hand_options}
23
+
24
+ Options:
25
+ #{option_details}
26
+ #{version_info}
27
+ EOH
28
+ end
29
+
30
+ def version_info
31
+ VERSION
32
+ end
33
+
34
+ private
35
+
36
+ def options_possible
37
+ raise %{Must override this with something like [ ['--option', '-o', GetoptLong::OPTION_TYPE, 'Helpful message'], ... ]}
38
+ end
39
+
40
+ def option_details
41
+ <<-EOO
42
+ #{options_possible.map{ |o| expand_option(o) }.join("\n")}
43
+ EOO
44
+ end
45
+
46
+ def expand_option(option)
47
+ sprintf(" %-#{longest_width + 6}s %s", option.first(2).join(', '), option[3])
48
+ end
49
+
50
+ def longest_width
51
+ @max_width ||= options_possible.map{ |o| o[0] }.max{ |a, b| a.length <=> b.length }.length
52
+ end
53
+
54
+ def short_hand_options
55
+ "[#{options_possible.map{ |o| short_hand_option(o)}.join('], [')}]"
56
+ end
57
+
58
+ def short_hand_option(option)
59
+ if option[2] == GetoptLong::REQUIRED_ARGUMENT
60
+ [option[0], option[1]].join('|') << " argument"
61
+ else
62
+ [option[0], option[1]].join('|')
63
+ end
64
+ end
65
+
66
+ end
67
+ end
@@ -0,0 +1,59 @@
1
+ # TODO Check for definition of options_possible
2
+ #require 'active_support/concern'
3
+
4
+ module CommandLineHelper
5
+ module Helper
6
+ # Provides:
7
+ # * Requires on options_possible to be defined and return an array of the form:
8
+ # [ ['--option', '-o', GetoptLong::OPTION_TYPE, 'Helpful message'], ... ]
9
+ # - help_info
10
+ # - program_name
11
+ # - short_hand_options (used inside help_info)
12
+ # - option_details (used inside help_info)
13
+ # - longest_width
14
+ # - expand_option
15
+ #
16
+ #
17
+ def help_info
18
+ <<-EOH
19
+ Usage: #{program_name} [options]
20
+ #{short_hand_options}
21
+
22
+ Options:
23
+ #{option_details}
24
+ #{version_info}
25
+ EOH
26
+ end
27
+
28
+ def program_name
29
+ @@program_name = File.basename($0)
30
+ end
31
+
32
+ def short_hand_options
33
+ "[#{options_possible.map{ |o| short_hand_option(o)}.join('], [')}]"
34
+ end
35
+
36
+ def short_hand_option(option)
37
+ if option[2] == GetoptLong::REQUIRED_ARGUMENT
38
+ [option[0], option[1]].join('|') << " argument"
39
+ else
40
+ [option[0], option[1]].join('|')
41
+ end
42
+ end
43
+
44
+ def option_details
45
+ <<-EOO
46
+ #{options_possible.map{ |o| expand_option(o) }.join("\n")}
47
+ EOO
48
+ end
49
+
50
+ def longest_width
51
+ @max_width ||= options_possible.map{ |o| o[0] }.max{ |a, b| a.length <=> b.length }.length
52
+ end
53
+
54
+ def expand_option(option)
55
+ sprintf(" %-#{longest_width + 6}s %s", option.first(2).join(', '), option[3])
56
+ end
57
+
58
+ end
59
+ end