Configuration class based on a INI file.

class MyIniConfig
    super IniConfig

    var opt_my = new OptionString("My option", "--my")

    init do
        super
        tool_description = "Usage: MyExample [OPTION]... [ARGS]..."
        opts.add_option(opt_my)
    end

    fun my: String do return opt_my.value or else ini["my"] or else "Default"
end

var config = new MyIniConfig
config.default_config_file = "my_config.ini"
config.parse_options(args)

if config.help then
    config.usage
    exit 0
end

assert config.my == "Default"

Introduced properties

fun config_file: String

config :: IniConfig :: config_file

Return the config file path from options or the default
fun default_config_file: String

config :: IniConfig :: default_config_file

Default config file path
fun default_config_file=(default_config_file: String)

config :: IniConfig :: default_config_file=

Default config file path
fun ini: IniFile

config :: IniConfig :: ini

Config tree used to store config options
protected fun ini=(ini: IniFile)

config :: IniConfig :: ini=

Config tree used to store config options
fun opt_config: OptionString

config :: IniConfig :: opt_config

Path to app config file
protected fun opt_config=(opt_config: OptionString)

config :: IniConfig :: opt_config=

Path to app config file

Redefined properties

redef type SELF: IniConfig

config $ IniConfig :: SELF

Type of this instance, automatically specialized in every class
redef init init

config $ IniConfig :: init

redef fun parse_options(args: Collection[String])

config $ IniConfig :: parse_options

Initialize self options from args

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
fun add_option(opt: Option...)

config :: Config :: add_option

Add an option to self
fun args: Array[String]

config :: Config :: args

Return the remaining args once options are parsed by from_args
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 config_file: String

config :: IniConfig :: config_file

Return the config file path from options or the default
fun default_config_file: String

config :: IniConfig :: default_config_file

Default config file path
fun default_config_file=(default_config_file: String)

config :: IniConfig :: default_config_file=

Default config file path
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 help: Bool

config :: Config :: help

Was the --help option requested?
fun ini: IniFile

config :: IniConfig :: ini

Config tree used to store config options
protected fun ini=(ini: IniFile)

config :: IniConfig :: ini=

Config tree used to store config options
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.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun opt_black_exts: OptionArray

config :: Config :: opt_black_exts

--blacklist-exts
fun opt_black_exts: OptionArray

config :: Config :: opt_black_exts

--blacklist-exts
protected fun opt_black_exts=(opt_black_exts: OptionArray)

config :: Config :: opt_black_exts=

--blacklist-exts
protected fun opt_black_exts=(opt_black_exts: OptionArray)

config :: Config :: opt_black_exts=

--blacklist-exts
fun opt_config: OptionString

config :: IniConfig :: opt_config

Path to app config file
protected fun opt_config=(opt_config: OptionString)

config :: IniConfig :: opt_config=

Path to app config file
fun opt_help: OptionBool

config :: Config :: opt_help

Help option
protected fun opt_help=(opt_help: OptionBool)

config :: Config :: opt_help=

Help option
protected fun opt_java_cp=(opt_java_cp: OptionString)

config :: Config :: opt_java_cp=

protected fun opt_lang=(opt_lang: OptionString)

config :: Config :: opt_lang=

--lang
protected fun opt_port=(opt_port: OptionInt)

config :: Config :: opt_port=

protected fun opt_server=(opt_server: OptionString)

config :: Config :: opt_server=

--server
fun opt_stub_man: OptionBool

config :: Config :: opt_stub_man

Option --stub-man
protected fun opt_stub_man=(opt_stub_man: OptionBool)

config :: Config :: opt_stub_man=

Option --stub-man
fun opt_white_exts: OptionArray

config :: Config :: opt_white_exts

--whitelist-exts
fun opt_white_exts: OptionArray

config :: Config :: opt_white_exts

--whitelist-exts
protected fun opt_white_exts=(opt_white_exts: OptionArray)

config :: Config :: opt_white_exts=

--whitelist-exts
protected fun opt_white_exts=(opt_white_exts: OptionArray)

config :: Config :: opt_white_exts=

--whitelist-exts
fun opts: OptionContext

config :: Config :: opts

Context used to store and parse options
protected fun opts=(opts: OptionContext)

config :: Config :: opts=

Context used to store and parse options
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_options(args: Collection[String])

config :: Config :: parse_options

Initialize self options from args
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun stub_man_options

config :: Config :: stub_man_options

Generate a manpage stub from self
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 tool_description: String

config :: Config :: tool_description

Name, usage and synopsis of the tool.
fun tool_description=(tool_description: String)

config :: Config :: tool_description=

Name, usage and synopsis of the tool.
fun usage

config :: Config :: usage

Display tool_description and options usage in console
package_diagram config::IniConfig IniConfig config::Config Config config::IniConfig->config::Config core::Object Object config::Config->core::Object ...core::Object ... ...core::Object->core::Object github::LoaderConfig LoaderConfig github::LoaderConfig->config::IniConfig popcorn::AppConfig AppConfig popcorn::AppConfig->config::IniConfig

Ancestors

interface Object

core :: Object

The root of the class hierarchy.

Parents

class Config

config :: Config

Basic configuration class

Children

class AppConfig

popcorn :: AppConfig

Configuration file for Popcorn apps
class LoaderConfig

github :: LoaderConfig

Loader configuration file

Class definitions

config $ IniConfig
# Configuration class based on a INI file.
#
# ~~~
# class MyIniConfig
#	super IniConfig
#
#	var opt_my = new OptionString("My option", "--my")
#
#	init do
#		super
#		tool_description = "Usage: MyExample [OPTION]... [ARGS]..."
#		opts.add_option(opt_my)
#	end
#
#	fun my: String do return opt_my.value or else ini["my"] or else "Default"
# end
#
# var config = new MyIniConfig
# config.default_config_file = "my_config.ini"
# config.parse_options(args)
#
# if config.help then
#	config.usage
#	exit 0
# end
#
# assert config.my == "Default"
# ~~~
class IniConfig
	super Config

	# Config tree used to store config options
	var ini: IniFile is noinit

	# Path to app config file
	var opt_config = new OptionString("Path to config file", "--config")

	init do
		super
		opts.add_option(opt_config)
	end

	redef fun parse_options(args) do
		super
		ini = new IniFile.from_file(config_file)
	end

	# Default config file path
	var default_config_file = "config.ini" is writable

	# Return the config file path from options or the default
	fun config_file: String do return opt_config.value or else default_config_file
end
lib/config/config.nit:270,1--322,3