Configuration options for nit tools and apps

This module provides basic services for options handling in your Nit programs.

Basic configuration holder

The Config class can be used as a simple option holder and processor:

import config

# Create a new config option
var opt_my = new OptionString("My option", "--my")

# Create the config and add the option
var config = new Config
config.add_option(opt_my)

# Parse the program arguments, usually `args`
config.parse_options(["--my", "myOption", "arg1", "arg2"])

# Access the options and args
assert opt_my.value == "myOption"
assert config.args == ["arg1", "arg2"]

Custom configuration class

Instead of using basic Config instances, it is better to define new sublcasses to store options and define custom services.

import config

class MyConfig
    super Config

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

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

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

var config = new MyConfig
config.parse_options(["--my", "myOption", "arg1", "arg2"])

assert config.my == "myOption"
assert config.args == ["arg1", "arg2"]

We define the my method to provide an elegant shortcut to opt_my.value and define the default value if the option was not set by the user.

The tool_description attribute is used to set the usage header printed when the user request the help message.

config.parse_options(["-h"])
if config.help then
    config.usage
    exit 0
end

This will display the tool usage like this:

Usage: MyExample [OPTION]... [ARGS]...
 -h, --help   Show this help message
 --my         My option

Configuration with ini file

The IniConfig class extends Config to add an easy way to link your configuration to an 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

This time, we define the my method to return the option value or the ini if no option was passed. Finally, if no ini value can be found, we return the default value.

By default, IniConfig looks at a config.ini file in the execution directory. This can be overrided in multiple ways.

First by the app user by setting the --config option:

var config = new MyIniConfig
config.parse_options(["--config", "my_config.ini"])

assert config.config_file == "my_config.ini"

Default config file can also be changed by the library client through the default_config_file attribute:

config = new MyIniConfig
config.default_config_file = "my_config.ini"
config.parse_options(["arg"])

assert config.config_file == "my_config.ini"

Or by the library developper in the custom config class:

class MyCustomIniConfig
    super IniConfig

    redef var default_config_file = "my_config.ini"
end

var config = new MyCustomIniConfig
config.parse_options(["arg"])

assert config.config_file == "my_config.ini"

All subgroups and modules

module config

config :: config

Configuration options for nit tools and apps
package_diagram config\> config ini ini config\>->ini opts opts config\>->opts core core ini->core opts->core ...core ... ...core->core markdown markdown markdown->config\> markdown2 markdown2 markdown2->config\> nlp\>examples\> examples nlp\>examples\>->config\> popcorn popcorn popcorn->config\> vsm\>examples\> examples vsm\>examples\>->config\> nitc nitc nitc->markdown nitc->markdown2 nitc->popcorn nitc... ... nitc...->nitc github github github->popcorn github... ... github...->github

Ancestors

group codecs

core > codecs

Group module for all codec-related manipulations
group collection

core > collection

This module define several collection classes.
group core

core

Nit common library of core classes and methods
group text

core > text

All the classes and methods related to the manipulation of text entities

Parents

group ini

ini

ini - Read and write INI configuration files
group opts

opts

Management of options on the command line

Children

group examples

nlp > examples

group examples

vsm > examples

group markdown

markdown

A markdown parser for Nit.
group popcorn

popcorn

Popcorn

Descendants

group api

nitc > doc > api

Components required to build a web server about the nit model.
group catalog

nitc > catalog

Basic catalog generator for Nit packages
group commands

nitc > doc > commands

group compiler

nitc > compiler

Compilation to C
group compiler_ffi

nitc > compiler > compiler_ffi

Full FFI support for the compiler
group doc

nitc > doc

group dynamic_loading_ffi

nitc > interpreter > dynamic_loading_ffi

This group implement a partial support for the Nit FFI in the interpreter.
group ffi

nitc > ffi

Full FFI support, independent of the compiler
group frontend

nitc > frontend

Collect and orchestration of main frontend phases
group github

github

Nit wrapper for Github API
group interpreter

nitc > interpreter

Interpretation of Nit programs
group metrics

nitc > metrics

Various statistics about Nit models and programs
group model

nitc > model

The meta model of Nit programs
group modelize

nitc > modelize

Create a model from nit source files
group nitc

nitc

Nit compiler and tools
group nitni

nitc > nitni

Native interface related services (used underneath the FFI)
group parser

nitc > parser

Parser and AST for the Nit language
group platform

nitc > platform

Platform system, used to customize the behavior of the compiler.
group saf

nitc > saf

Nit Static Analysis Framework.
group semantize

nitc > semantize

Process bodies of methods in regard with the model.
group static

nitc > doc > static

Nitdoc generation framework
group term

nitc > doc > term

group testing

nitc > testing

Test unit generation and execution for Nit.
group uml

nitc > uml

Group head module for UML generation services
group vm

nitc > vm

Entry point of all vm components