Module config
source code
Config file parsing
All of the scripts in this project use the optparse package to handle
command line options.
This module provides a very simple mechanism for taking config files
as input instead of specifying options on the command line. This is
convenient, for example, for running experiments repeatedly where a whole
load of options need to be given to set parameters.
The options in the file are stored in the following format:
optname=value
- The files may contain comments beginning with a '#'.
- To specify arguments, just put the argument on a line of its own.
- To specify flags, put the flag name on a line of its own preceded by a +.
You can only use long option names currently. This is best practice
anyway, as it makes the file more readable.
The config options are simply transformed into a string of
command-line-like options and added to the actual command-line
options.
Don't forget to put a comment in the file so you know what script it's
for!
Additionally, lines beginning with '%%' are treated as directives.
-
%% INCLUDE filename: includes another config file.
-
%% ARG i value: treats value as the ith
argument. If you specify any arguments in this way, you should
specify them all like this. Allows the arguments not to be given in
order.
-
%% DEF name value: defines or defines the value of the
variable name. This value may subsequently be used with
a %{name} substitution.
-
%% ABSTRACT: declares the whole file to be abstract,
i.e. it cannot be used directly, but only as an include in another
file. You should put this in any file that relies on including files
to supply required options/arguments.
-
%% REQUIRE option: requires the user to specify the
named option on the command line when using this config file.
You may use certain substitutions in the options. %{X} will be
replaced by a value if one can be found. The following sources are
consulted (in this order):
-
a variable X defined with a DEF directive;
-
a constant X from the settings file.
One purpose of this is to allow you to specify paths relative to the
project root, etc, rather than where the script is run.
A linebreak preceded by a \ will be ignored. Whitespace at the start
of the subsequent line will be ignored (but not whitespace before the
\).
Author:
Mark Granroth-Wilding <mark.granroth-wilding@ed.ac.uk>
|
|
parse_args_with_config(parser,
option_name='config')
An alternative to calling parser.parse_args() which adds a --config
option to the parser's options and uses it to read in a config file
if it's given. |
source code
|
|
|
|
__package__ = 'jazzparser.utils'
|
parse_args_with_config(parser,
option_name='config')
| source code
|
An alternative to calling parser.parse_args() which adds a --config
option to the parser's options and uses it to read in a config file if
it's given.
The args will potentially get parsed twice: once to get the config
file and then again to incorporate options from the file.
- Returns:
- (options, arguments) tuple, as given by parser.parse_args().
|