Class: YleTf::CLI
- Inherits:
-
Object
- Object
- YleTf::CLI
- Defined in:
- lib/yle_tf/cli.rb
Constant Summary collapse
- TF_OPTIONS =
YleTf option arguments
%w[--debug --no-color --no-hooks --only-hooks].freeze
- HELP_ARGS =
%w[-h --help help].freeze
- VERSION_ARGS =
%w[-v --version version].freeze
Instance Attribute Summary collapse
-
#tf_command ⇒ Object
readonly
Returns the value of attribute tf_command.
-
#tf_command_args ⇒ Object
readonly
Returns the value of attribute tf_command_args.
-
#tf_env ⇒ Object
readonly
Returns the value of attribute tf_env.
-
#tf_options ⇒ Object
readonly
Returns the value of attribute tf_options.
Instance Method Summary collapse
- #debug=(value) ⇒ Object
- #debug? ⇒ Boolean
- #execute ⇒ Object
-
#initialize(argv) ⇒ CLI
constructor
A new instance of CLI.
-
#key(arg) ⇒ Object
Returns ‘Symbol` for the arg, e.g.
-
#split_args(argv) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/BlockLength, Metrics/MethodLength rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
Constructor Details
#initialize(argv) ⇒ CLI
Returns a new instance of CLI.
13 14 15 16 17 |
# File 'lib/yle_tf/cli.rb', line 13 def initialize(argv) @tf_options = {} @tf_command_args = [] split_args(argv) end |
Instance Attribute Details
#tf_command ⇒ Object (readonly)
Returns the value of attribute tf_command.
5 6 7 |
# File 'lib/yle_tf/cli.rb', line 5 def tf_command @tf_command end |
#tf_command_args ⇒ Object (readonly)
Returns the value of attribute tf_command_args.
5 6 7 |
# File 'lib/yle_tf/cli.rb', line 5 def tf_command_args @tf_command_args end |
#tf_env ⇒ Object (readonly)
Returns the value of attribute tf_env.
5 6 7 |
# File 'lib/yle_tf/cli.rb', line 5 def tf_env @tf_env end |
#tf_options ⇒ Object (readonly)
Returns the value of attribute tf_options.
5 6 7 |
# File 'lib/yle_tf/cli.rb', line 5 def @tf_options end |
Instance Method Details
#debug=(value) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/yle_tf/cli.rb', line 79 def debug=(value) if value ENV['TF_DEBUG'] = '1' else ENV.delete('TF_DEBUG') end end |
#debug? ⇒ Boolean
87 88 89 |
# File 'lib/yle_tf/cli.rb', line 87 def debug? ENV.key?('TF_DEBUG') end |
#execute ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/yle_tf/cli.rb', line 19 def execute tf = YleTf.new(, tf_env, tf_command, tf_command_args) tf.run rescue YleTf::Error => e raise e if debug? Logger.fatal e exit 1 end |
#key(arg) ⇒ Object
Returns ‘Symbol` for the arg, e.g. `“–foo-bar”` -> `:foo_bar`
75 76 77 |
# File 'lib/yle_tf/cli.rb', line 75 def key(arg) arg.sub(/\A--?/, '').tr('-', '_').to_sym end |
#split_args(argv) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/BlockLength, Metrics/MethodLength rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/yle_tf/cli.rb', line 31 def split_args(argv) argv.each do |arg| if @tf_env && @tf_command if TF_OPTIONS.include?(arg) @tf_options[key(arg)] = true else @tf_command_args << arg end elsif HELP_ARGS.include?(arg) @tf_command = 'help' @tf_env = '_' break elsif VERSION_ARGS.include?(arg) @tf_command = 'version' @tf_env = '_' break elsif arg.start_with?('-') if TF_OPTIONS.include?(arg) @tf_options[key(arg)] = true else STDERR.puts "Unknown option '#{arg}'" @tf_command = 'help' @tf_env = 'error' break end elsif !@tf_env @tf_env = arg else @tf_command = arg end end if !@tf_command || !@tf_env @tf_command = 'help' @tf_env = 'error' end self.debug = true if @tf_options[:debug] YleTf::Logger.color = false if @tf_options[:no_color] end |