Context where the options process

Introduced properties

private var _context_errors: Array[String]

opts :: OptionContext :: _context_errors

Errors found in the context after parsing
private var _options: Array[Option]

opts :: OptionContext :: _options

Options present in the context
private var _options_before_rest: Bool

opts :: OptionContext :: _options_before_rest

Must all option be given before the first argument?
private var _rest: Array[String]

opts :: OptionContext :: _rest

Rest of the options after parse is called
fun add_option(opts: Option...)

opts :: OptionContext :: add_option

Add one or more options to the context
private fun build

opts :: OptionContext :: build

fun context_errors: Array[String]

opts :: OptionContext :: context_errors

Errors found in the context after parsing
protected fun context_errors=(context_errors: Array[String])

opts :: OptionContext :: context_errors=

Errors found in the context after parsing
fun errors: Array[String]

opts :: OptionContext :: errors

Options parsing errors.
fun options: Array[Option]

opts :: OptionContext :: options

Options present in the context
protected fun options=(options: Array[Option])

opts :: OptionContext :: options=

Options present in the context
fun options_before_rest: Bool

opts :: OptionContext :: options_before_rest

Must all option be given before the first argument?
fun options_before_rest=(options_before_rest: Bool)

opts :: OptionContext :: options_before_rest=

Must all option be given before the first argument?
private fun optmap=(optmap: HashMap[String, Option])

opts :: OptionContext :: optmap=

fun parse(argv: nullable Collection[String])

opts :: OptionContext :: parse

Parse and assign options in argv or args
protected fun parse_intern(it: Iterator[String])

opts :: OptionContext :: parse_intern

Parse the command line
fun rest: Array[String]

opts :: OptionContext :: rest

Rest of the options after parse is called
protected fun rest=(rest: Array[String])

opts :: OptionContext :: rest=

Rest of the options after parse is called
fun usage

opts :: OptionContext :: usage

Display all the options available

Redefined properties

redef type SELF: OptionContext

opts $ OptionContext :: SELF

Type of this instance, automatically specialized in every class

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
private var _context_errors: Array[String]

opts :: OptionContext :: _context_errors

Errors found in the context after parsing
private var _options: Array[Option]

opts :: OptionContext :: _options

Options present in the context
private var _options_before_rest: Bool

opts :: OptionContext :: _options_before_rest

Must all option be given before the first argument?
private var _rest: Array[String]

opts :: OptionContext :: _rest

Rest of the options after parse is called
fun add_option(opts: Option...)

opts :: OptionContext :: add_option

Add one or more options to the context
private fun build

opts :: OptionContext :: build

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.
fun context_errors: Array[String]

opts :: OptionContext :: context_errors

Errors found in the context after parsing
protected fun context_errors=(context_errors: Array[String])

opts :: OptionContext :: context_errors=

Errors found in the context after parsing
fun errors: Array[String]

opts :: OptionContext :: errors

Options parsing errors.
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.
init init

core :: Object :: init

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.
private intern fun native_class_name: CString

core :: Object :: native_class_name

The class name of the object in CString format.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun options: Array[Option]

opts :: OptionContext :: options

Options present in the context
protected fun options=(options: Array[Option])

opts :: OptionContext :: options=

Options present in the context
fun options_before_rest: Bool

opts :: OptionContext :: options_before_rest

Must all option be given before the first argument?
fun options_before_rest=(options_before_rest: Bool)

opts :: OptionContext :: options_before_rest=

Must all option be given before the first argument?
private fun optmap=(optmap: HashMap[String, Option])

opts :: OptionContext :: optmap=

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 parse(argv: nullable Collection[String])

opts :: OptionContext :: parse

Parse and assign options in argv or args
protected fun parse_intern(it: Iterator[String])

opts :: OptionContext :: parse_intern

Parse the command line
fun rest: Array[String]

opts :: OptionContext :: rest

Rest of the options after parse is called
protected fun rest=(rest: Array[String])

opts :: OptionContext :: rest=

Rest of the options after parse is called
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 usage

opts :: OptionContext :: usage

Display all the options available
package_diagram opts::OptionContext OptionContext core::Object Object opts::OptionContext->core::Object

Parents

interface Object

core :: Object

The root of the class hierarchy.

Class definitions

opts $ OptionContext
# Context where the options process
class OptionContext
	# Options present in the context
	var options = new Array[Option]

	# Rest of the options after `parse` is called
	var rest = new Array[String]

	# Errors found in the context after parsing
	var context_errors = new Array[String]

	private var optmap = new HashMap[String, Option]

	# Add one or more options to the context
	fun add_option(opts: Option...) do options.add_all(opts)

	# Display all the options available
	fun usage
	do
		var lmax = 1
		for i in options do
			var l = 3
			for n in i.names do
				l += n.length + 2
			end
			if lmax < l then lmax = l
		end

		for i in options do
			if not i.hidden then
				print(i.pretty(lmax))
			end
		end
	end

	# Parse and assign options in `argv` or `args`
	fun parse(argv: nullable Collection[String])
	do
		if argv == null then argv = args
		var it = argv.iterator
		parse_intern(it)
	end

	# Must all option be given before the first argument?
	#
	# When set to `false` (the default), options of the command line are
	# all parsed until the end of the list of arguments or until "--" is met (in this case "--" is discarded).
	#
	# When set to `true` options are parsed until the first non-option is met.
	var options_before_rest = false is writable

	# Parse the command line
	protected fun parse_intern(it: Iterator[String])
	do
		var parseargs = true
		build
		var rest = rest

		while parseargs and it.is_ok do
			var str = it.item
			if str == "--" then
				it.next
				rest.add_all(it.to_a)
				parseargs = false
			else
				# We're looking for packed short options
				if str.chars.last_index_of('-') == 0 and str.length > 2 then
					var next_called = false
					for i in [1..str.length[ do
						var short_opt = "-" + str.chars[i].to_s
						if optmap.has_key(short_opt) then
							var option = optmap[short_opt]
							if option isa OptionParameter then
								it.next
								next_called = true
							end
							option.read_param(self, it)
						end
					end
					if not next_called then it.next
				else
					if optmap.has_key(str) then
						var opt = optmap[str]
						it.next
						opt.read_param(self, it)
					else
						rest.add(it.item)
						it.next
						if options_before_rest then
							rest.add_all(it.to_a)
							parseargs = false
						end
					end
				end
			end
		end

		for opt in options do
			if opt.mandatory and not opt.read then
				context_errors.add("Mandatory option {opt.names.join(", ")} not found.")
			end
		end
	end

	private fun build
	do
		for o in options do
			for n in o.names do
				optmap[n] = o
			end
		end
	end

	# Options parsing errors.
	fun errors: Array[String]
	do
		var errors = new Array[String]
		errors.add_all context_errors
		for o in options do
			for e in o.errors do
				errors.add(e)
			end
		end
		return errors
	end
end
lib/opts/opts.nit:281,1--406,3