Class: RunKit::Options::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/run_kit/options/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



13
14
15
16
17
18
# File 'lib/run_kit/options/config.rb', line 13

def initialize
  @app_name = File.basename($PROGRAM_NAME)
  @naked = true
  @lookup = {}
  @flags, @positionals, @separators = [], [], []
end

Instance Attribute Details

#app_nameObject

Returns the value of attribute app_name.



9
10
11
# File 'lib/run_kit/options/config.rb', line 9

def app_name
  @app_name
end

Returns the value of attribute banner.



9
10
11
# File 'lib/run_kit/options/config.rb', line 9

def banner
  @banner
end

#colorObject

Returns the value of attribute color.



9
10
11
# File 'lib/run_kit/options/config.rb', line 9

def color
  @color
end

#exitObject

Returns the value of attribute exit.



9
10
11
# File 'lib/run_kit/options/config.rb', line 9

def exit
  @exit
end

#flagsObject (readonly)

Returns the value of attribute flags.



10
11
12
# File 'lib/run_kit/options/config.rb', line 10

def flags
  @flags
end

#helpObject

Returns the value of attribute help.



9
10
11
# File 'lib/run_kit/options/config.rb', line 9

def help
  @help
end

#help_flagObject (readonly)

Returns the value of attribute help_flag.



10
11
12
# File 'lib/run_kit/options/config.rb', line 10

def help_flag
  @help_flag
end

#lookupObject (readonly)

Returns the value of attribute lookup.



10
11
12
# File 'lib/run_kit/options/config.rb', line 10

def lookup
  @lookup
end

#nakedObject Also known as: naked?

Returns the value of attribute naked.



9
10
11
# File 'lib/run_kit/options/config.rb', line 9

def naked
  @naked
end

#positionalsObject (readonly)

Returns the value of attribute positionals.



10
11
12
# File 'lib/run_kit/options/config.rb', line 10

def positionals
  @positionals
end

#separatorsObject (readonly)

Returns the value of attribute separators.



10
11
12
# File 'lib/run_kit/options/config.rb', line 10

def separators
  @separators
end

#versionObject

Returns the value of attribute version.



9
10
11
# File 'lib/run_kit/options/config.rb', line 9

def version
  @version
end

#version_flagObject (readonly)

Returns the value of attribute version_flag.



10
11
12
# File 'lib/run_kit/options/config.rb', line 10

def version_flag
  @version_flag
end

Instance Method Details

#bool(*opts, default: nil, required: false) ⇒ Object Also known as: boolean

flags



40
41
42
# File 'lib/run_kit/options/config.rb', line 40

def bool(*opts, default: nil, required: false)
  add_flag(Flag.new(:bool, opts, default:, required:))
end

#defaultsObject

handy helpers



74
75
76
77
78
79
# File 'lib/run_kit/options/config.rb', line 74

def defaults
  flags.filter_map do
    next if _1.required || _1 == help_flag || _1 == version_flag
    [_1.key, _1.default]
  end.to_h
end

#flag(switch) ⇒ Object

one-liners



82
# File 'lib/run_kit/options/config.rb', line 82

def flag(switch) = lookup[switch]

#flag?(switch) ⇒ Boolean

Returns:

  • (Boolean)


83
# File 'lib/run_kit/options/config.rb', line 83

def flag?(switch) = lookup.key?(switch)

#float(*opts, default: nil, required: false, choices: nil) ⇒ Object



44
45
46
# File 'lib/run_kit/options/config.rb', line 44

def float(*opts, default: nil, required: false, choices: nil)
  add_flag(Flag.new(:float, opts, default:, required:, choices:))
end

#int(*opts, default: nil, required: false, choices: nil) ⇒ Object Also known as: integer



48
49
50
# File 'lib/run_kit/options/config.rb', line 48

def int(*opts, default: nil, required: false, choices: nil)
  add_flag(Flag.new(:int, opts, default:, required:, choices:))
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


84
# File 'lib/run_kit/options/config.rb', line 84

def key?(key) = lookup.key?(key)

#path(*opts, default: nil, required: false, choices: nil) ⇒ Object Also known as: pathname



52
53
54
# File 'lib/run_kit/options/config.rb', line 52

def path(*opts, default: nil, required: false, choices: nil)
  add_flag(Flag.new(:path, opts, default:, required:, choices:))
end

#pos(meta, help = "") ⇒ Object Also known as: positional

Add a positional param declared as <url>.



21
22
23
24
25
26
27
# File 'lib/run_kit/options/config.rb', line 21

def pos(meta, help = "")
  Positional.new(meta:, help:).tap do
    raise ArgumentError, "duplicate positional #{_1.key}" if key?(_1.key)
    positionals << _1
    lookup[_1.key] = _1
  end
end

#prepare!Object

Complete one-time setup after the caller has declared overrides.



88
89
90
91
92
93
94
# File 'lib/run_kit/options/config.rb', line 88

def prepare!
  return if @prepared
  @prepared = true
  @exit ||= lambda { |status| Kernel.exit(status) }
  @help_flag = add_builtin(["-h", "--help"], "Show this message")
  @version_flag = add_builtin(["-v", "--version"], "Show version") if version
end

#requiredObject



85
# File 'lib/run_kit/options/config.rb', line 85

def required = flags.select(&:required?)

#sep(text = "") ⇒ Object Also known as: separator

Add separator text at the current point in generated help.



30
31
32
33
34
# File 'lib/run_kit/options/config.rb', line 30

def sep(text = "")
  [flags.length, text].tap do
    separators << _1
  end
end

#str(*opts, default: nil, required: false, choices: nil) ⇒ Object Also known as: string



56
57
58
# File 'lib/run_kit/options/config.rb', line 56

def str(*opts, default: nil, required: false, choices: nil)
  add_flag(Flag.new(:str, opts, default:, required:, choices:))
end

#sym(*opts, default: nil, required: false, choices: nil) ⇒ Object Also known as: symbol



60
61
62
# File 'lib/run_kit/options/config.rb', line 60

def sym(*opts, default: nil, required: false, choices: nil)
  add_flag(Flag.new(:sym, opts, default:, required:, choices:))
end