Class: OsCtl::Lib::Cli::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/libosctl/cli/command.rb

Overview

Utilities for command handlers in CLI

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#argsObject (readonly)

Returns the value of attribute args.



4
5
6
# File 'lib/libosctl/cli/command.rb', line 4

def args
  @args
end

#goptsObject (readonly)

Returns the value of attribute gopts.



4
5
6
# File 'lib/libosctl/cli/command.rb', line 4

def gopts
  @gopts
end

#optsObject (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)

Parameters:

  • required (Array)

    list of required arguments

  • optional (Array) (defaults to: [])

    list of optional arguments

  • strict (Boolean) (defaults to: true)

    do not allow more arguments than specified



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