Skip to content

Commit 0bac82b

Browse files
authored
SIP78 Convert switches to run time options part 3 (#114)
1 parent e4abf14 commit 0bac82b

43 files changed

Lines changed: 47919 additions & 47547 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ sections to include in release notes:
3131
- Build docs and push to gh-pages (#41)
3232
- events.out file for agronomic event handling (#57)
3333
- Utility `tools/trim_first_chars.sh` to trim the first n characters from every row in a file, useful for updating old input files to remove location column
34-
- Expanded smoke test cases to better cover SIPNET modeling options (#109)
34+
- Expanded smoke test cases to better cover SIPNET modeling options (#109, #114)
35+
- Converted all compile-time switches not removed or hard-coded to be on into switches to run-time options (#114)
3536

3637
### Fixed
3738

3839
- Fixed OOM issue when reading bad data (#38, #45)
3940
- Event order checks no longer only compare to first record (#74, #77)
41+
- Fixed long-standing bug wherein microbePulseEff was not set to 0 when MICROBES was off (#114)
4042

4143
### Changed
4244

@@ -52,6 +54,7 @@ sections to include in release notes:
5254
- Removed obsolete run types senstest and montecarlo and associated code (#69, #76)
5355
- Removed obsolete estimate program and associated code (#70, #82)
5456
- Removed multi-site support; in particular, output files no longer have a location column (#92)
57+
- Removed or hard-codes 'on' many compile time switches (#114)
5558

5659
### Git SHA
5760
[TBD]

docs/developer-guide/cli-options.md

Lines changed: 91 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ When adding a new command-line option, you will need to add a corresponding memb
1111

1212
## Important Notes
1313

14-
**A note on naming:**
14+
### Naming
1515

1616
Each command-line option must have a corresponding entry in the `Context` struct. SIPNET uses a mapping system to
1717
link the command-line option (e.g. `print_header`) to its `Context` member (in this case, `printHeader`).
@@ -21,11 +21,12 @@ The mapping is created using the `nameToKey` function in `common/context.c` that
2121
- removes all non-alphanumeric characters.
2222
- converts alpha characters to lowercase.
2323

24-
For the mapping to work, the option name (e.g., `print_header`) and `Context` member name (in this case, `printHeader`) must generate the same key (e.g. `print_header` and `printHeader` both generate `printheader`).
24+
For the mapping to work, the option name (e.g., `print_header`) and `Context` member name (in this case,
25+
`printHeader`) must generate the same key (e.g. `print_header` and `printHeader` both generate `printheader`).
2526

2627
Each key must be unique across the `Context` member names.
2728

28-
**A note on precedence:**
29+
### Precedence
2930

3031
SIPNET options can be specified in either (or both) a config file or a command-line option. Command-line options are
3132
have precedence over config file options.
@@ -35,6 +36,8 @@ This means that:
3536
- Options must be set via the macros `CREATE_INT_CONTEXT`/`CREATE_CHAR_CONTEXT` when they are created.
3637
- The functions `updateIntContext`/`updateCharContext` must be used when updating a value.
3738

39+
See the Function/Macro Reference at the end for more information.
40+
3841
## Steps to Add Each Type of Option
3942

4043
For each type of option, the steps are similar:
@@ -97,6 +100,11 @@ Here are the steps to add a string-valued option:
97100
2. Update `parseCLIArgs()`... [details TBD]
98101
3. Update `usage()` to document the new option in the help text
99102

103+
### Long-Form Only Options
104+
105+
When adding to the `long_options` struct in step 3(i), give an int value as the last element of the the new
106+
struct. This is the index that should be used in the `parseCLIArgs()` switch statement for handling the new
107+
option. The int value must be unique among the other case labels.
100108

101109
## Additional Steps [TBD]
102110

@@ -111,10 +119,83 @@ Here are the steps to add a string-valued option:
111119
- Add tests for both the new option and its negation (if applicable) in [TBD].
112120
- Anything to here to make sure this works in `sipnet.in` [TBD]?
113121

114-
## Long-Form Only Options
115-
116-
[TBD: fold this in above? This isn't that complicated; let's see if anything changes in the next round]
117-
118-
When adding to the `long_options` struct in step 3(i), give an int value as the last element of the the new
119-
struct. This is the index that should be used in the `parseCLIArgs()` switch statement for handling the new
120-
option. The int value must be unique among the other case labels.
122+
## Function/Macro Reference
123+
124+
When adding a new CLI option, these functions and macros should be used to create and update
125+
the corresponding fields in the `Context` struct.
126+
127+
### `CREATE_INT_CONTEXT`
128+
129+
This macro is used to initialize the `Context` field as well as create the necessary
130+
metadata information for integer-based arguments, both flag and not.
131+
This macro is used in the `initContext` function in `src/common/context.c`.
132+
133+
Syntax:
134+
```c
135+
CREATE_INT_CONTEXT(name, printName, value, flag)
136+
```
137+
Arguments:
138+
- `name`: the exact name (no quotes) of the `Context` struct member used in `Context.h`
139+
- `printName`: the name used for this option when printing via `--dump-config`
140+
- `value`: the default integer value for this option; the defined values `ARG_ON` and `ARG_OFF` can be used for flag options
141+
- `flag`: `FLAG_YES` or `FLAG_NO` to indicate whether this is a flag option
142+
143+
Examples:
144+
<br>`CREATE_INT_CONTEXT(snow, "SNOW", ARG_ON, FLAG_YES);`
145+
<br>`CREATE_INT_CONTEXT(numSoilCarbonPools, "NUM_SOIL_CARBON_POOLS", 3, FLAG_NO);`
146+
147+
### `CREATE_CHAR_CONTEXT`
148+
149+
This macro is used to initialize the `Context` field as well as create the necessary
150+
metadata information for string-based arguments.
151+
This macro is used in the `initContext` function in `src/common/context.c`.
152+
153+
Syntax:
154+
```c
155+
CREATE_CHAR_CONTEXT(name, printName, value)
156+
```
157+
Arguments:
158+
- `name`: the exact name (no quotes) of the `Context` struct member used in `Context.h`
159+
- `printName`: the name used for this option when printing via `--dump-config`
160+
- `value`: the default string value for this option
161+
162+
Examples:
163+
<br>`CREATE_CHAR_CONTEXT(outConfigFile, "OUT_CONFIG_FILE", NO_DEFAULT_FILE);`
164+
<br>`CREATE_CHAR_CONTEXT(inputFile, "INPUT_FILE", DEFAULT_INPUT_FILE);`
165+
166+
### `updateIntContext`
167+
168+
This function is used to update an integer-based argument, both the stored value as well as the source of that value.
169+
This function is typically used in the `parseCommandLineArgs` function in `src/sipnet/cli.c`.
170+
Note that flag options should not need this, as they are handled automatically.
171+
172+
Syntax:
173+
```c
174+
updateIntContext(const char *name, int value, context_source_t source)
175+
```
176+
Arguments:
177+
- `name`: a string that can uniquely identify the option (see [Naming](#naming) above)
178+
- `value`: the new integer value for the option
179+
- `source`: where the value is coming from, e.g. `CTX_CONTEXT_FILE` or `CTX_COMMAND_LINE`
180+
181+
Examples:
182+
<br>`updateIntContext(inputName, intVal, CTX_CONTEXT_FILE);` (from parsing the input file)
183+
<br>`updateIntContext("numSoilCarbonPools", intVal, CTX_COMMAND_LINE);` (from parsing the command line)
184+
185+
### `updateCharContext`
186+
187+
This function is used to update a string-based argument, both the stored value as well as the source of that value.
188+
This function is typically used in the `parseCommandLineArgs` function in `src/sipnet/cli.c`.
189+
190+
Syntax:
191+
```c
192+
updateCharContext(const char *name, const char *value, context_source_t source)
193+
```
194+
Arguments:
195+
- `name`: a string that can uniquely identify the option (see [Naming](#naming) above)
196+
- `value`: the new string value for the option
197+
- `source`: where the value is coming from, e.g. `CTX_CONTEXT_FILE` or `CTX_COMMAND_LINE`
198+
199+
Examples:
200+
<br>`updateCharContext(inputName, inputValue, CTX_CONTEXT_FILE);` (from parsing the input file)
201+
<br>`updateCharContext("inputFile", optarg, CTX_COMMAND_LINE);` (from parsing the command line)

0 commit comments

Comments
 (0)