An option to choose from an enumeration

Declare an enumeration option with all its possible values as an array. Once the arguments are processed, value is set as the index of the selected value, if any.

Introduced properties

init init(values: Array[String], help: String, default: Int, names: String...)

opts :: OptionEnum :: init

Init a new OptionEnum from values with a help message and names.
fun value_name: String

opts :: OptionEnum :: value_name

Get the value name from values.
fun values: Array[String]

opts :: OptionEnum :: values

Values in the enumeration.
protected fun values=(values: Array[String])

opts :: OptionEnum :: values=

Values in the enumeration.

Redefined properties

redef type SELF: OptionEnum

opts $ OptionEnum :: SELF

Type of this instance, automatically specialized in every class
redef type VALUE: Int

opts $ OptionEnum :: VALUE

Type of the value of the option
redef fun convert(str: String): VALUE

opts $ OptionEnum :: convert

Convert str to a value of type VALUE.
redef fun pretty_default: String

opts $ OptionEnum :: pretty_default

Pretty print the default value.

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
type VALUE: nullable Object

opts :: Option :: VALUE

Type of the value of the option
fun add_aliases(names: String...)

opts :: Option :: add_aliases

Add new aliases for this option
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
protected abstract fun convert(str: String): VALUE

opts :: OptionParameter :: convert

Convert str to a value of type VALUE.
fun default_value: VALUE

opts :: Option :: default_value

Default value of this option
fun default_value=(default_value: VALUE)

opts :: Option :: default_value=

Default value of this option
fun errors: Array[String]

opts :: Option :: errors

Gathering errors during parsing
protected fun errors=(errors: Array[String])

opts :: Option :: errors=

Gathering errors during parsing
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
fun helptext: String

opts :: Option :: helptext

Human readable description of the option
protected fun helptext=(helptext: String)

opts :: Option :: helptext=

Human readable description of the option
fun hidden: Bool

opts :: Option :: hidden

Is this option hidden from usage?
fun hidden=(hidden: Bool)

opts :: Option :: hidden=

Is this option hidden from usage?
init init(values: Array[String], help: String, default: Int, names: String...)

opts :: OptionEnum :: init

Init a new OptionEnum from values with a help message and names.
init init

core :: Object :: init

init init(help: String, default: VALUE, names: nullable Array[String])

opts :: Option :: init

Create a new option
fun init_opt(help: String, default: VALUE, names: nullable Array[String])

opts :: Option :: init_opt

Init option helptext, default_value and names.
fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
fun mandatory: Bool

opts :: Option :: mandatory

Is this option mandatory?
fun mandatory=(mandatory: Bool)

opts :: Option :: mandatory=

Is this option mandatory?
fun names: Array[String]

opts :: Option :: names

Names for the option (including long and short ones)
protected fun names=(names: Array[String])

opts :: Option :: names=

Names for the option (including long and short ones)
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun parameter_mandatory: Bool

opts :: OptionParameter :: parameter_mandatory

Is the parameter mandatory?
fun parameter_mandatory=(parameter_mandatory: Bool)

opts :: OptionParameter :: parameter_mandatory=

Is the parameter mandatory?
fun pretty(off: Int): String

opts :: Option :: pretty

A pretty print for this help
fun pretty_default: String

opts :: Option :: pretty_default

Pretty print the default value.
fun read: Bool

opts :: Option :: read

Has this option been read?
fun read=(read: Bool)

opts :: Option :: read=

Has this option been read?
protected fun read_param(opts: OptionContext, it: Iterator[String])

opts :: Option :: read_param

Consume parameters for this option
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
fun value: VALUE

opts :: Option :: value

Current value of this option
fun value=(value: VALUE)

opts :: Option :: value=

Current value of this option
fun value_name: String

opts :: OptionEnum :: value_name

Get the value name from values.
fun values: Array[String]

opts :: OptionEnum :: values

Values in the enumeration.
protected fun values=(values: Array[String])

opts :: OptionEnum :: values=

Values in the enumeration.
package_diagram opts::OptionEnum OptionEnum opts::OptionParameter OptionParameter opts::OptionEnum->opts::OptionParameter opts::Option Option opts::OptionParameter->opts::Option ...opts::Option ... ...opts::Option->opts::Option

Ancestors

interface Object

core :: Object

The root of the class hierarchy.
abstract class Option

opts :: Option

Super class of all option's class

Parents

abstract class OptionParameter

opts :: OptionParameter

Option with one parameter (mandatory by default)

Class definitions

opts $ OptionEnum
# An option to choose from an enumeration
#
# Declare an enumeration option with all its possible values as an array.
# Once the arguments are processed, `value` is set as the index of the selected value, if any.
class OptionEnum
	super OptionParameter
	redef type VALUE: Int

	# Values in the enumeration.
	var values: Array[String]

	# Init a new OptionEnum from `values` with a `help` message and `names`.
	#
	# `default` is the index of the default value in `values`.
	init(values: Array[String], help: String, default: Int, names: String...) is old_style_init do
		assert values.length > 0
		self.values = values.to_a
		super("{help} <{values.join(", ")}>", default, names)
	end

	redef fun convert(str)
	do
		var id = values.index_of(str)
		if id == -1 then
			var e = "Unrecognized value for option {names.join(", ")}.\n"
			e += "Expected values are: {values.join(", ")}."
			errors.add(e)
		end
		return id
	end

	# Get the value name from `values`.
	fun value_name: String do return values[value]

	redef fun pretty_default
	do
		return " ({values[default_value]})"
	end
end
lib/opts/opts.nit:189,1--227,3