cromo The Cromo Guide
Download PDF IT

std.config

Names with their values, name=value: in memory, and in a file of lines that a person reads and writes, as the settings of a program.

cromo
import std.config

config.Config settings = config.load("app.conf")   // port=8080, a line for each name
i64 port = settings.getInt("port", 80)             // 80 when the file does not say it
string host = settings.getOr("host", "localhost")
settings.setBool("verbose", true)
settings.save("app.conf")                          // the comments of the file are still there

The guide: Files, processes and time.

Made from the documentation in the sources of the library.

#Types

#Config

cromo
struct Config

Names with their values, in the order in which they were written. One declared without a value is empty. It is a value: a copy has the names that the first had.

#Config.set

cromo
void set(string name, string value)

Gives name the text value, in place of the one it had, or as a new line at the end. The spaces around the two are left out. Throws an Error for a name that is empty, has a = or an end of line, or starts with # or ;, and for a value with an end of line: a file could not hold them.

#Config.setInt

cromo
void setInt(string name, i64 value)

Gives name a whole number.

#Config.setFloat

cromo
void setFloat(string name, f64 value)

Gives name a number with a fraction.

#Config.setBool

cromo
void setBool(string name, bool value)

Gives name "true" or "false".

#Config.setLine

cromo
void setLine(string line)

Reads a line name=value, as those of a file, or of the arguments of a program: the name is what comes before the first =. Throws an Error for a line without a =.

#Config.has

cromo
bool has(string name) const

Whether name has a value, also an empty one.

#Config.get

cromo
string get(string name) const

The value of name, or "" when it has none.

#Config.getOr

cromo
string getOr(string name, string otherwise) const

The value of name, or otherwise when it has none.

#Config.getInt

cromo
i64 getInt(string name, i64 otherwise) const

The value of name as a whole number, or otherwise when it has none or it is not a number.

#Config.getFloat

cromo
f64 getFloat(string name, f64 otherwise) const

The value of name as a number with a fraction, or otherwise when it has none or it is not a number.

#Config.getBool

cromo
bool getBool(string name, bool otherwise) const

The value of name as a yes or a no: true for "true", "yes", "on" and "1", false for "false", "no", "off" and "0", in upper or lower case; otherwise when it has none or it is another text.

#Config.remove

cromo
bool remove(string name)

Takes name and its value away. Returns whether it was there.

#Config.names

cromo
list<string> names() const

The names, in the order in which they were written.

#Config.len

cromo
i64 len() const

How many names there are.

#Config.clear

cromo
void clear()

Takes everything away, the comments too.

#Config.text

cromo
string text() const

The lines of a file: name=value for each name, in their order, with the comments and the blank lines that parse or load read, each line ended by "\n".

#Config.save

cromo
void save(string path) const

Writes text() to the file at path, in place of what it had. Throws as fs.writeText when the file cannot be written.

#Functions

#parse

cromo
Config parse(string text)

The names and the values written in text, a line name=value for each; the blank lines and those that start with # or ; are comments, kept for text() and save. A name written twice has its last value. Throws an Error with the number of the line for one that is not a comment and has no =.

#load

cromo
Config load(string path)

The names and the values of the file at path, as parse reads them. Throws as fs.readText when the file cannot be read.