Class: Sym::App::CLI

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
CLISlop
Defined in:
lib/sym/app/cli.rb

Overview

This is the main interface class for the CLI application. It is responsible for parsing user’s input, providing help, examples, coordination of various sub-systems (such as PrivateKey detection), etc.

Besides holding the majority of the application state, it contains two primary public methods: #new and #run.

The constructor is responsible for parsing the flags and determining the the application is about to do. It sets up input/output, but doesn’t really execute any encryption or decryption. This happens in the #run method called immediately after #new.

{Shh{Shh::App{Shh::App::CLI} module effectively performs the translation of the opts object (of type Slop::Result) and interpretation of users intentions. It holds on to opts for the duration of the program.

Responsibility Delegated

The responsibility of determining the private key from various options provided is performed by the PrivateKey::Handler instance. See there for more details.

Subsequently, #run method handles the finding of the appropriate Sym::App::Commands::BaseCommand subclass to respond to user’s request. Command registry, sorting, command dependencies, and finding them is done by the Coommands module.

User input is handled by the Input::Handler instance, while the output is provided by the procs in the Output classes.

Finally, the Mac OS-X -specific usage of the KeyChain, is encapsulated in a cross-platform way inside the Keychain module.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CLISlop

#parse

Constructor Details

#initialize(argv_original) ⇒ CLI

Returns a new instance of CLI.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sym/app/cli.rb', line 63

def initialize(argv_original)
  env_args = ENV[ENV_ARGS_VARIABLE_NAME]
  begin
    argv = argv_original.dup
    argv << env_args.split(' ') if env_args && !(argv.include?('-M') or argv.include?('--no-environment'))
    argv.flatten!
    dict      = argv.delete('--dictionary')
    self.opts = parse(argv)
    command_dictionary if dict
  rescue StandardError => e
    error exception: e
    return
  end

  command_no_color(argv_original) if opts[:no_color]

  self.application = ::Sym::Application.new(opts)

  select_output_stream
end

Instance Attribute Details

#applicationObject

Returns the value of attribute application.



61
62
63
# File 'lib/sym/app/cli.rb', line 61

def application
  @application
end

#optsObject

Returns the value of attribute opts.



61
62
63
# File 'lib/sym/app/cli.rb', line 61

def opts
  @opts
end

#output_procObject

Returns the value of attribute output_proc.



61
62
63
# File 'lib/sym/app/cli.rb', line 61

def output_proc
  @output_proc
end

#outputsObject

Returns the value of attribute outputs.



61
62
63
# File 'lib/sym/app/cli.rb', line 61

def outputs
  @outputs
end

Instance Method Details

#executeObject



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/sym/app/cli.rb', line 85

def execute
  return Sym::App.exit_code if Sym::App.exit_code != 0

  result = application.execute
  if result.is_a?(Hash)
    self.output_proc = ::Sym::App::Args.new({}).output_class
    error(result)
  else
    self.output_proc.call(result)
  end
end