Class: OsCtl::Lib::Cli::Command
- Inherits:
-
Object
- Object
- OsCtl::Lib::Cli::Command
- Defined in:
- lib/libosctl/cli/command.rb
Overview
Utilities for command handlers in CLI
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#gopts ⇒ Object
readonly
Returns the value of attribute gopts.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
-
#initialize(global_opts, opts, args) ⇒ Command
constructor
A new instance of Command.
- #require_args!(*required, optional: [], strict: true) ⇒ Object protected
Constructor Details
#initialize(global_opts, opts, args) ⇒ Command
Returns a new instance of Command.
6 7 8 9 10 |
# File 'lib/libosctl/cli/command.rb', line 6 def initialize(global_opts, opts, args) @gopts = global_opts @opts = opts @args = args end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
4 5 6 |
# File 'lib/libosctl/cli/command.rb', line 4 def args @args end |
#gopts ⇒ Object (readonly)
Returns the value of attribute gopts.
4 5 6 |
# File 'lib/libosctl/cli/command.rb', line 4 def gopts @gopts end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
4 5 6 |
# File 'lib/libosctl/cli/command.rb', line 4 def opts @opts end |
Instance Method Details
#require_args!(*required, optional: [], strict: true) ⇒ Object (protected)
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/libosctl/cli/command.rb', line 17 def require_args!(*required, optional: [], strict: true) if args.count < required.count arg = required[args.count] raise GLI::BadCommandLine, "missing argument <#{arg}>" elsif strict && args.count > (required.count + optional.count) unknown = args[(required.count + optional.count)..] msg = '' msg << if unknown.count > 1 'unknown arguments: ' else 'unknown argument: ' end msg << unknown.join(' ') if unknown.detect { |v| v.start_with?('-') } msg << ' (note that options must come before arguments)' end raise GLI::BadCommandLine, msg end end |