File: README.md

package info (click to toggle)
headius-options 1.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 164 kB
  • sloc: java: 408; xml: 85; makefile: 2
file content (154 lines) | stat: -rw-r--r-- 4,676 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
options: a library for JVM property-driven configuration
========================================================

This library provides a simple mechanism for defining JVM property-based
configuration for an application or library.

Options are defined via a small DSL-like setup, supporting String, Integer,
Boolean, and Enum-based configurations. Non-Boolean options support a set of
supported values, and all options allow specifying a default value. In addition,
options are created with an Enum-based category (provided by the user) and a
documentation string, which allows grouping properties and printing out a full
set of options as a valid, modifiable .properties file.

Usage
-----

A real-world usage example from the [JRuby project's Options class](https://github.com/jruby/jruby/blob/master/core/src/main/java/org/jruby/util/cli/Options.java).

Example usage of the four types of options:

```java
import com.headius.options.Option;
import static com.headius.options.Option.*;
// ...
enum MyCategory { STANDARD, EXTENDED, SPECIAL, OTHER }
enum AccountType { ADMIN, GUEST, NORMAL }

// ...

// Without defaults or options...
Option<String> databaseName = string("config.databaseName", MyCategory.STANDARD, "name of the database");
Option<Integer> connCount = integer("config.connCount", MyCategory.EXTENDED, "connection count");
Option<Boolean> authenticate = bool("config.authenticate", MyCategory.SPECIAL, "do authentication");
Option<AccountType> acctType = enumeration("config.acctType", MyCategory.OTHER, AccountType.class, "account type");

// With options...
Option<String> username = string("config.username", MyCategory.STANDARD, new String[]{"root", "guest"}, "account name");

// With default...
Option<Boolean> timeout = bool("config.timeout", MyCategory.EXTENDED, true, "timeout connections");

// With both...
Option<Integer> timeoutSecs = integer("config.timeoutSecs", MyCategory.EXTENDED, new Integer[]{15, 30, 60}, 30, "timeout in seconds");

// Load options; value is retrieved once and cached.
String dbname = databaseName.load();
AccountType accountType = acctType.load();

// Or reloading the property can be forced...
System.setProperty("config.databaseName", "customers");
dbname = databaseName.reload();

// options can be queried to see if they were provided
if (databaseName.isSpecified()) {
    // logic for setting database name
}

// If your system has a standard property prefix, it can be specified in the
// option. The prefix will be omitted from formatted output.
Option<String> firstName = string("config", "first.name", MyCategory.STANDARD, "...");

// formatted output of all options
System.out.println(
        Option.formatOptions(
                databaseName, connCount, authenticate, acctType,
                username, timeout, timeoutSecs, firstName));

// short output of current settings
System.out.println(
        Option.formatValues(
                databaseName, connCount, authenticate, acctType,
                username, timeout, timeoutSecs, firstName));
```

Formatted output of options is an editable properties file. Note the "firstName"
option does not include the "config." prefix.

```
################################################################################
# STANDARD
################################################################################

# name of the database

#config.databaseName=

# account name
# Options: [root, guest].

#config.username=

# ...

#first.name=


################################################################################
# EXTENDED
################################################################################

# connection count

#config.connCount=

# timeout connections
# Options: [true, false], Default: true.

#config.timeout=true

# timeout in seconds
# Options: [15, 30, 60], Default: 30.

#config.timeoutSecs=30


################################################################################
# SPECIAL
################################################################################

# do authentication
# Options: [true, false].

#config.authenticate=


################################################################################
# OTHER
################################################################################

# account type
# Options: [ADMIN, GUEST, NORMAL].

#config.acctType=
```

You can also format the values of all options, loaded on the current JVM.

```
STANDARD
config.databaseName=customers
config.username=<unspecified>
first.name=<unspecified>

EXTENDED
config.connCount=<unspecified>
config.timeout=true
config.timeoutSecs=30

SPECIAL
config.authenticate=<unspecified>

OTHER
config.acctType=<unspecified>
```