Class: CLI::Kit::Args::Definition
- Inherits:
-
Object
- Object
- CLI::Kit::Args::Definition
- Extended by:
- T::Sig
- Defined in:
- lib/cli/kit/args/definition.rb
Defined Under Namespace
Modules: OptBase, OptValue Classes: Flag, Option, Position
Constant Summary collapse
- Error =
Class.new(Args::Error)
- ConflictingFlag =
Class.new(Error)
- InvalidFlag =
Class.new(Error)
- InvalidLookup =
Class.new(Error)
- InvalidPosition =
Class.new(Error)
Instance Attribute Summary collapse
-
#flags ⇒ Object
readonly
Returns the value of attribute flags.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#positions ⇒ Object
readonly
Returns the value of attribute positions.
Instance Method Summary collapse
- #add_flag(name, short: nil, long: nil, desc: nil) ⇒ Object
- #add_option(name, short: nil, long: nil, desc: nil, default: nil, required: false, multi: false) ⇒ Object
- #add_position(name, required:, multi:, desc: nil, default: nil, skip: nil) ⇒ Object
-
#initialize ⇒ Definition
constructor
A new instance of Definition.
- #lookup_flag(name) ⇒ Object
- #lookup_long(name) ⇒ Object
- #lookup_option(name) ⇒ Object
- #lookup_position(name) ⇒ Object
- #lookup_short(name) ⇒ Object
Methods included from T::Sig
Constructor Details
#initialize ⇒ Definition
Returns a new instance of Definition.
81 82 83 84 85 86 87 88 |
# File 'lib/cli/kit/args/definition.rb', line 81 def initialize @flags = [] @options = [] @by_short = {} @by_long = {} @by_name = {} @positions = [] end |
Instance Attribute Details
#flags ⇒ Object (readonly)
Returns the value of attribute flags.
18 19 20 |
# File 'lib/cli/kit/args/definition.rb', line 18 def flags @flags end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
21 22 23 |
# File 'lib/cli/kit/args/definition.rb', line 21 def @options end |
#positions ⇒ Object (readonly)
Returns the value of attribute positions.
24 25 26 |
# File 'lib/cli/kit/args/definition.rb', line 24 def positions @positions end |
Instance Method Details
#add_flag(name, short: nil, long: nil, desc: nil) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/cli/kit/args/definition.rb', line 27 def add_flag(name, short: nil, long: nil, desc: nil) short, long = strip_prefixes_and_validate(short, long) flag = Flag.new(name: name, short: short, long: long, desc: desc) add_resolution(flag) @flags << flag end |
#add_option(name, short: nil, long: nil, desc: nil, default: nil, required: false, multi: false) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/cli/kit/args/definition.rb', line 46 def add_option(name, short: nil, long: nil, desc: nil, default: nil, required: false, multi: false) short, long = strip_prefixes_and_validate(short, long) option = Option.new( name: name, short: short, long: long, desc: desc, default: default, required: required, multi: multi ) add_resolution(option) @options << option end |
#add_position(name, required:, multi:, desc: nil, default: nil, skip: nil) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/cli/kit/args/definition.rb', line 70 def add_position(name, required:, multi:, desc: nil, default: nil, skip: nil) position = Position.new( name: name, desc: desc, required: required, multi: multi, default: default, skip: skip ) validate_order(position) add_name_resolution(position) @positions << position end |
#lookup_flag(name) ⇒ Object
234 235 236 237 238 239 |
# File 'lib/cli/kit/args/definition.rb', line 234 def lookup_flag(name) flagopt = @by_name[name] if flagopt.class == Flag flagopt end end |
#lookup_long(name) ⇒ Object
257 258 259 260 261 |
# File 'lib/cli/kit/args/definition.rb', line 257 def lookup_long(name) raise(InvalidLookup, "invalid '-' prefix") if name.start_with?('-') @by_long[name] end |
#lookup_option(name) ⇒ Object
242 243 244 245 246 247 |
# File 'lib/cli/kit/args/definition.rb', line 242 def lookup_option(name) flagopt = @by_name[name] if flagopt.class == Option flagopt end end |
#lookup_position(name) ⇒ Object
264 265 266 267 268 269 |
# File 'lib/cli/kit/args/definition.rb', line 264 def lookup_position(name) position = @by_name[name] if position.class == Position position end end |
#lookup_short(name) ⇒ Object
250 251 252 253 254 |
# File 'lib/cli/kit/args/definition.rb', line 250 def lookup_short(name) raise(InvalidLookup, "invalid '-' prefix") if name.start_with?('-') @by_short[name] end |