Class: NanDoc::OptsNormalizer::Exclusive

Inherits:
Object
  • Object
show all
Includes:
NanDoc::OptsNormalizer
Defined in:
lib/nandoc/support-modules.rb

Instance Method Summary collapse

Methods included from NanDoc::OptsNormalizer

#exclusive_opt_flags, #normalize_opts, #unnormalize_opt_key, #unnormalize_opt_keys

Constructor Details

#initialize(&block) ⇒ Exclusive

Returns a new instance of Exclusive.



41
42
43
44
45
46
47
48
49
# File 'lib/nandoc/support-modules.rb', line 41

def initialize &block
  @exclusive_flag_keys = nil
  @default_short = nil
  @default_key = nil
  @notice_stream = $stderr
  instance_eval(&block)
  fail('definition block needs at least flags()') unless
    @exclusive_flag_keys
end

Instance Method Details

#default(*a) ⇒ Object

params: [short_name_string] name_key



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/nandoc/support-modules.rb', line 54

def default *a
  if a.first.kind_of?(String)
    @default_short = a.shift
  end
  if a.first.kind_of?(Symbol)
    @default_key = a.shift
  else
    fail("bad args: #{a.first.inspect}")
  end
  fail("extra args: #{a.inspect}") if a.any?
end

#flags(*exclusive_flag_keys) ⇒ Object



50
51
52
# File 'lib/nandoc/support-modules.rb', line 50

def flags * exclusive_flag_keys
  @exclusive_flag_keys = exclusive_flag_keys
end

#notice_stream(mixed) ⇒ Object



65
66
67
# File 'lib/nandoc/support-modules.rb', line 65

def notice_stream mixed
  @notice_stream = mixed
end

#parse(cmd, opts) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/nandoc/support-modules.rb', line 68

def parse cmd, opts
  these = @exclusive_flag_keys & opts.keys
  if these.empty? && @default_key
    if @notice_stream
      msg =
      ["using default: "+unnormalize_opt_key(@default_key),
        @default_short ? "(#{@default_short})" : nil
      ].compact.join(' ')
      @notice_stream.puts msg
    end
    these.push(@default_key)
  end
  if these.size > 1
    flags = unnormalize_opt_keys(@exclusive_flag_keys)
    cmd.task_abort <<-ABORT.gsub(/^  */,'')
      #{flags.join(' and ')} are mutually exclusive.
      usage: #{cmd.usage}
      #{cmd.invite_to_more_command_help}
    ABORT
  end
  these.first
end