fuelcell 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.
Files changed (50) hide show
  1. checksums.yaml +15 -0
  2. data/.codeclimate.yml +12 -0
  3. data/.gitignore +13 -0
  4. data/.rspec +5 -0
  5. data/.travis.yml +15 -0
  6. data/CODE_OF_CONDUCT.md +13 -0
  7. data/Gemfile +3 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +51 -0
  10. data/Rakefile +5 -0
  11. data/bin/console +9 -0
  12. data/bin/example.rb +9 -0
  13. data/bin/setup +7 -0
  14. data/bin/test +20 -0
  15. data/bin/world.rb +6 -0
  16. data/fuelcell.gemspec +26 -0
  17. data/lib/fuelcell/action/arg_definition.rb +36 -0
  18. data/lib/fuelcell/action/arg_results.rb +57 -0
  19. data/lib/fuelcell/action/args_manager.rb +66 -0
  20. data/lib/fuelcell/action/callable.rb +54 -0
  21. data/lib/fuelcell/action/command.rb +72 -0
  22. data/lib/fuelcell/action/not_found.rb +55 -0
  23. data/lib/fuelcell/action/opt_definition.rb +79 -0
  24. data/lib/fuelcell/action/opt_results.rb +68 -0
  25. data/lib/fuelcell/action/opts_manager.rb +80 -0
  26. data/lib/fuelcell/action/root.rb +76 -0
  27. data/lib/fuelcell/action/subcommands.rb +81 -0
  28. data/lib/fuelcell/action.rb +11 -0
  29. data/lib/fuelcell/cli.rb +89 -0
  30. data/lib/fuelcell/help/base_formatter.rb +24 -0
  31. data/lib/fuelcell/help/builder.rb +71 -0
  32. data/lib/fuelcell/help/cmds_formatter.rb +57 -0
  33. data/lib/fuelcell/help/desc_formatter.rb +21 -0
  34. data/lib/fuelcell/help/opts_formatter.rb +62 -0
  35. data/lib/fuelcell/help/usage_formatter.rb +88 -0
  36. data/lib/fuelcell/help.rb +27 -0
  37. data/lib/fuelcell/parser/arg_handler.rb +31 -0
  38. data/lib/fuelcell/parser/base_handler.rb +133 -0
  39. data/lib/fuelcell/parser/cmd_args_strategy.rb +28 -0
  40. data/lib/fuelcell/parser/ignore_handler.rb +25 -0
  41. data/lib/fuelcell/parser/opt_handler.rb +58 -0
  42. data/lib/fuelcell/parser/opt_name_handler.rb +89 -0
  43. data/lib/fuelcell/parser/opt_value_equal_handler.rb +26 -0
  44. data/lib/fuelcell/parser/parsing_strategy.rb +80 -0
  45. data/lib/fuelcell/parser/short_opt_no_space_handler.rb +54 -0
  46. data/lib/fuelcell/parser.rb +4 -0
  47. data/lib/fuelcell/shell.rb +102 -0
  48. data/lib/fuelcell/version.rb +3 -0
  49. data/lib/fuelcell.rb +114 -0
  50. metadata +148 -0
@@ -0,0 +1,102 @@
1
+ module Fuelcell
2
+ class Shell
3
+ extend Forwardable
4
+
5
+ SUCCESS_EXIT_CODE = 0
6
+ ERROR_EXIT_CODE = 255
7
+ DEFAULT_TERMINAL_WIDTH = 80
8
+
9
+ attr_reader :output_stream, :error_stream, :input_stream, :backtrace
10
+ alias_method :backtrace?, :backtrace
11
+ delegate puts: :output_stream
12
+
13
+ def initialize(config = {})
14
+ @output_stream = config[:output_stream] || $stdout
15
+ @input_stream = config[:input_stream] || $stdin
16
+ @error_stream = config[:error_stream] || $stderr
17
+ @backtrace = config[:backtrace] || false
18
+ @terminal_cols = config[:terminal_cols]
19
+ @backtrace = @backtrace == true ? true : false
20
+ end
21
+
22
+ def enable_backtrace
23
+ @backtrace = true
24
+ end
25
+
26
+ def disable_backtrace
27
+ @backtrace = false
28
+ end
29
+
30
+ def error(text)
31
+ error_stream.puts text
32
+ end
33
+
34
+ def exception(e)
35
+ message = e.message
36
+ message << "\n" << e.backtrace.join("\n") if backtrace?
37
+ error(message)
38
+ failure_exit
39
+ end
40
+
41
+ def terminal_width
42
+ if ENV['FUELCELL_COLUMNS']
43
+ result = ENV['FUELCELL_COLUMNS'].to_i
44
+ elsif @terminal_cols
45
+ result = @terminal_cols.to_i
46
+ else
47
+ result = unix? ? dynamic_width : DEFAULT_TERMINAL_WIDTH
48
+ end
49
+
50
+ result < 10 ? DEFAULT_TERMINAL_WIDTH : result
51
+ rescue
52
+ DEFAULT_TERMINAL_WIDTH
53
+ end
54
+
55
+ # Determine the width using stty or tput
56
+ #
57
+ # credit goes to the rake project, I took this from the application.rb
58
+ # @return Integer
59
+ def dynamic_width
60
+ @dynamic_width ||= (dynamic_width_stty.nonzero? || dynamic_width_tput)
61
+ end
62
+
63
+ # Determines if we are on a unix like system for determining the width
64
+ # of the terminal
65
+ #
66
+ # credit goes to the rake project, I took this from the application.rb
67
+ #
68
+ # @return Boolean
69
+ def unix?
70
+ !(
71
+ RUBY_PLATFORM =~
72
+ /(aix|darwin|linux|(net|free|open)bsd|cygwin|solaris|irix|hpux)/i
73
+ ).nil?
74
+ end
75
+
76
+ # credit goes to the rake project, I took this from the application.rb
77
+ #
78
+ # @return Integer
79
+ def dynamic_width_stty
80
+ `stty size 2>/dev/null`.split[1].to_i
81
+ end
82
+
83
+ # credit goes to the rake project, I took this from the application.rb
84
+ #
85
+ # @return Integer
86
+ def dynamic_width_tput
87
+ `tput cols 2>/dev/null`.to_i
88
+ end
89
+
90
+ def successful_exit
91
+ exit(SUCCESS_EXIT_CODE)
92
+ end
93
+
94
+ def failure_exit
95
+ exit(ERROR_EXIT_CODE)
96
+ end
97
+
98
+ def exit(code)
99
+ Kernel.exit(code)
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,3 @@
1
+ module Fuelcell
2
+ VERSION = "0.1.0"
3
+ end
data/lib/fuelcell.rb ADDED
@@ -0,0 +1,114 @@
1
+ require 'forwardable'
2
+ require 'fuelcell/version'
3
+ require 'fuelcell/cli'
4
+
5
+ # Exposes Fuelcell's main interface. You can describe a complete command
6
+ # heirarchy, and start the processing/execution of a command all at the
7
+ # same time using Fuelcell.start. Fuelcell.define allows you to describe
8
+ # a command heirarchy using separate files. The cli interfaces are needed
9
+ # when :start & :define are used togather, allowing :define to access the
10
+ # root command
11
+ module Fuelcell
12
+ class << self
13
+ # Assigning the cli represents the current execution context for which
14
+ # commandline processing takes place. This is triggered by the :start
15
+ # method and is needed to allow the :define method to work in a separate
16
+ # file the was required by the script using :start
17
+ #
18
+ # The alias :make_cli_available_for_definitions is ment to describe the
19
+ # intent of the :start method
20
+ attr_accessor :cli
21
+ alias_method :make_cli_available_for_definitions, :cli=
22
+
23
+ def cli?
24
+ ! @cli.nil?
25
+ end
26
+
27
+ # The current execution context
28
+ #
29
+ # @return [Fuelcell::Cli]
30
+
31
+ # Remove the cli object & replace it with nil. The general case for this
32
+ # is in the :start method when no more command definition will be
33
+ # processed. Since we don't know for sure how long we will be in memory
34
+ # we clean up whats not used
35
+ #
36
+ # The alias :remove_cli_availability is an attempt to improve intent of
37
+ # the :start method
38
+ #
39
+ # @return [Nil]
40
+ def remove_cli
41
+ @cli = nil
42
+ end
43
+ alias_method :remove_cli_availability, :remove_cli
44
+
45
+ # Allows definitions to be created in a separate file and have them included
46
+ # by load/require in the start block.
47
+ #
48
+ # @param name [String] name of the command to be defined
49
+ # @yield instance_eval into the root command
50
+ def define(name, &block)
51
+ name = name.to_s
52
+ if name.empty?
53
+ fail ArgumentError, 'command name can not be empty'
54
+ end
55
+
56
+ unless cli?
57
+ fail RuntimeError, 'cli interface does not exist, please use ' \
58
+ 'Fuelcell.start before Fuelcell.define'
59
+ end
60
+
61
+ root = cli.root
62
+ cmd_args = name.split(' ')
63
+ root.ensure_command_hierarchy(cmd_args)
64
+ cmd = root[name]
65
+ cmd.instance_eval(&block)
66
+ end
67
+
68
+ # Allows for the configuration of the Cli object which controls
69
+ # command line processing.
70
+ #
71
+ # @param settings [Hash]
72
+ # root_name: used to override the default script name
73
+ # root_cmd: override the RootCommand object with your own
74
+ # shell: override the Shell object with your own
75
+ # backtrace: causes backtraces to be printed to error stream
76
+ # output_stream: override $stdout as the output stream
77
+ # input_stream: override $stdin as the input stream
78
+ # error_stream: override $stderr as the error stream
79
+ # exit: cause fuelcell not to explicitly exit, use false
80
+ # parser: override opt & args parsing
81
+ # cmd_args_extractor: override command extraction strategy
82
+ #
83
+ # @return [Fuelcell::Cli]
84
+ def create_cli(settings = {})
85
+ root_name = settings[:root_name]
86
+ root_cmd = settings.fetch(:root_cmd) { Action::Root.new(root_name) }
87
+ shell = settings.fetch(:shell) { Shell.new(settings) }
88
+ Cli.new(root: root_cmd, shell: shell)
89
+ end
90
+
91
+ # Entry point for executing a command which was defined through the
92
+ # the fuelcell dsl. The executable command should return an exit code,
93
+ # used to exit the program.
94
+ #
95
+ # @param args [Array] command line args to be processed
96
+ # @param settings [Hash] allows for overriding parts of fuelcell
97
+ # @yield optional block, will instance_eval on the root command
98
+ # @return [Int]
99
+ def start(args = [], settings = {}, &block)
100
+ cli = settings[:cli] || create_cli(settings)
101
+ root = cli.root
102
+
103
+ make_cli_available_for_definitions(cli)
104
+
105
+ # expose the command dsl
106
+ root.instance_eval(&block) if block_given?
107
+ context = cli.parse(args)
108
+
109
+ remove_cli_availability
110
+ code = cli.execute(context)
111
+ cli.handle_exit code
112
+ end
113
+ end
114
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fuelcell
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Robert Scott-Buccleuch
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-24 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '0.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '0.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 3.3.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 3.3.0
69
+ description: framework which simplifies working with the command-line.
70
+ email:
71
+ - rsb.code@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .codeclimate.yml
77
+ - .gitignore
78
+ - .rspec
79
+ - .travis.yml
80
+ - CODE_OF_CONDUCT.md
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/example.rb
87
+ - bin/setup
88
+ - bin/test
89
+ - bin/world.rb
90
+ - fuelcell.gemspec
91
+ - lib/fuelcell.rb
92
+ - lib/fuelcell/action.rb
93
+ - lib/fuelcell/action/arg_definition.rb
94
+ - lib/fuelcell/action/arg_results.rb
95
+ - lib/fuelcell/action/args_manager.rb
96
+ - lib/fuelcell/action/callable.rb
97
+ - lib/fuelcell/action/command.rb
98
+ - lib/fuelcell/action/not_found.rb
99
+ - lib/fuelcell/action/opt_definition.rb
100
+ - lib/fuelcell/action/opt_results.rb
101
+ - lib/fuelcell/action/opts_manager.rb
102
+ - lib/fuelcell/action/root.rb
103
+ - lib/fuelcell/action/subcommands.rb
104
+ - lib/fuelcell/cli.rb
105
+ - lib/fuelcell/help.rb
106
+ - lib/fuelcell/help/base_formatter.rb
107
+ - lib/fuelcell/help/builder.rb
108
+ - lib/fuelcell/help/cmds_formatter.rb
109
+ - lib/fuelcell/help/desc_formatter.rb
110
+ - lib/fuelcell/help/opts_formatter.rb
111
+ - lib/fuelcell/help/usage_formatter.rb
112
+ - lib/fuelcell/parser.rb
113
+ - lib/fuelcell/parser/arg_handler.rb
114
+ - lib/fuelcell/parser/base_handler.rb
115
+ - lib/fuelcell/parser/cmd_args_strategy.rb
116
+ - lib/fuelcell/parser/ignore_handler.rb
117
+ - lib/fuelcell/parser/opt_handler.rb
118
+ - lib/fuelcell/parser/opt_name_handler.rb
119
+ - lib/fuelcell/parser/opt_value_equal_handler.rb
120
+ - lib/fuelcell/parser/parsing_strategy.rb
121
+ - lib/fuelcell/parser/short_opt_no_space_handler.rb
122
+ - lib/fuelcell/shell.rb
123
+ - lib/fuelcell/version.rb
124
+ homepage: https://github.com/rsb/fuelcell
125
+ licenses:
126
+ - MIT
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ! '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 2.4.5
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: framework which simplifies working with the command-line.
148
+ test_files: []