Chapter 13. DefaultSources and Configuration Files
Prev |  | Next

## Chapter 13. DefaultSources and Configuration Files

Default program behavior can be configured in one or more files that the command line merely overrides. `JSAP` provides a `registerDefaultSource(DefaultSource)` method to support this. At the time of this writing, the only concrete implementation of `DefaultSource` is `PropertyDefaultSource`, which reads files in `java.util.Properties` format.

Multiple `DefaultSource`s may be registered with a `JSAP`. When a default value is sought, the `DefaultSource`s are checked in the order in which they were registered. The first default value encountered by `JSAP` is used.

## PropertyDefaultSource

`PropertyDefaultSource` reads default values from text files consisting of zero or more lines of "key=value"-type assignments. Property names may be parameter IDs, long flags, or short flags for the parameters they address. `JSAP` interprets them in that order (ID, then long flag, then short flag).

When you create a `PropertyDefaultSource`, you can tell it not to throw `Exceptions`. This allows you to specify a number of possible locations for files that don't have to exist.

A typical order in which to register `PropertyDefaultSources` is:

1. `.yourprogramname` in the user's home directory (obtained via `System` properties)

2. `/etc/yourprogramname.conf` on *nix systems

3. `$YOURPROGRAM_HOME/yourprogramname.conf` if your program makes use of an environment variable to point to its home directory

### Note

If you have any suggestions for a search order example for Windows or Mac, please let me know.

### Warning

`PropertyDefaultSource` has not yet been tested with `QualifiedSwitches`; odds are it won't work as you expect.

Prev |  | Next
Chapter 12. Handling Errors | Home | Chapter 14. Loading JSAP Configurations at Runtime with XML