Class: KBSecret::CLI
- Inherits:
-
Object
- Object
- KBSecret::CLI
- Defined in:
- lib/kbsecret/cli.rb
Overview
An encapsulation of useful methods for kbsecret's CLI.
Instance Attribute Summary collapse
-
#args ⇒ Dreck::Result
readonly
The result of trailing argument parsing.
-
#opts ⇒ Slop::Result
readonly
The result of option parsing.
Class Method Summary collapse
-
.die(msg) ⇒ void
Print an error message and terminate.
-
.ensure_session(sess_label) ⇒ void
deprecated
Deprecated.
Use #ensure_session! instead.
-
.ifs ⇒ String
Finds a reasonable default field separator by checking the environment first and then falling back to ":".
-
.slop(cmds: [], errors: false) ⇒ Slop::Result
deprecated
Deprecated.
Use #initialize instead.
Instance Method Summary collapse
-
#dreck(errors: true, &block) ⇒ Object
Parse trailing arguments for a kbsecret utility, using the elements remaining after options have been removed and interpreted via #slop.
-
#ensure_session!(where = :option) ⇒ void
Ensure that a session passed in as an option or argument already exists (i.e., is already configured).
-
#initialize(&block) ⇒ CLI
constructor
Encapsulate both the options and trailing arguments passed to a
kbsecret
command. -
#slop(cmds: [], errors: false) ⇒ Slop::Result
Parse options for a kbsecret utility, adding some default options for introspection and help output.
Constructor Details
#initialize(&block) ⇒ CLI
Encapsulate both the options and trailing arguments passed to a kbsecret
command.
33 34 35 36 37 38 39 40 |
# File 'lib/kbsecret/cli.rb', line 33 def initialize(&block) @trailing = ARGV @opts = nil @args = nil instance_eval(&block) rescue => e self.class.die e.to_s.capitalize end |
Instance Attribute Details
#args ⇒ Dreck::Result (readonly)
Returns the result of trailing argument parsing.
14 15 16 |
# File 'lib/kbsecret/cli.rb', line 14 def args @args end |
#opts ⇒ Slop::Result (readonly)
Returns the result of option parsing.
11 12 13 |
# File 'lib/kbsecret/cli.rb', line 11 def opts @opts end |
Class Method Details
.die(msg) ⇒ void
This method does not return!
This method returns an undefined value.
Print an error message and terminate.
97 98 99 100 |
# File 'lib/kbsecret/cli.rb', line 97 def die(msg) pretty = "#{"Fatal".red}: #{msg}" abort pretty end |
.ensure_session(sess_label) ⇒ void
Use #ensure_session! instead.
This method does not return if the given session is not configured!
This method returns an undefined value.
Instantiate a session if it exists, and terminate otherwise.
107 108 109 110 111 |
# File 'lib/kbsecret/cli.rb', line 107 def ensure_session(sess_label) die "Unknown session: '#{sess_label}'." unless Config.session? sess_label Session.new label: sess_label end |
.ifs ⇒ String
Finds a reasonable default field separator by checking the environment first and then falling back to ":".
116 117 118 |
# File 'lib/kbsecret/cli.rb', line 116 def ifs ENV["IFS"] || ":" end |
.slop(cmds: [], errors: false) ⇒ Slop::Result
Use #initialize instead.
Parse arguments for a kbsecret utility, adding some default options for introspection and help output.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/kbsecret/cli.rb', line 126 def slop(cmds: [], errors: false) Slop.parse suppress_errors: !errors do |o| yield o o.on "-h", "--help" do puts o exit end o.on "--introspect-flags" do comp = o..flat_map(&:flags) + cmds puts comp.join "\n" exit end end end |
Instance Method Details
#dreck(errors: true, &block) ⇒ Object
71 72 73 74 75 |
# File 'lib/kbsecret/cli.rb', line 71 def dreck(errors: true, &block) @args = Dreck.parse @trailing, strict: errors do instance_eval(&block) end end |
#ensure_session!(where = :option) ⇒ void
87 88 89 90 |
# File 'lib/kbsecret/cli.rb', line 87 def ensure_session!(where = :option) label = where == :option ? @opts[:session] : @args[:session] raise "Unknown session: '#{label}'." unless Config.session? label end |
#slop(cmds: [], errors: false) ⇒ Slop::Result
This should be called within the block passed to #initialize.
Parse options for a kbsecret utility, adding some default options for introspection and help output.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/kbsecret/cli.rb', line 48 def slop(cmds: [], errors: false) @opts = Slop.parse suppress_errors: !errors do |o| yield o o.on "-h", "--help" do puts o exit end o.on "--introspect-flags" do comp = o..flat_map(&:flags) + cmds puts comp.join "\n" exit end end @trailing = @opts.args end |