Skip to content

Commit 03a3ecf

Browse files
authored
replace ZSV_OPT_PROPERTY_OVERRIDE_XXX with opts.option_overrides.xxx (#321)
1 parent 2d37382 commit 03a3ecf

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

app/cli.c

+2
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,8 @@ char havearg(const char *arg, const char *form1, size_t min_len1, const char *fo
506506

507507
static struct builtin_cmd *find_builtin(const char *cmd_name) {
508508
int builtin_cmd_count = sizeof(builtin_cmds) / sizeof(*builtin_cmds);
509+
if (!strcmp(cmd_name, "-h") || !strcmp(cmd_name, "--help"))
510+
cmd_name = "help";
509511
for (int i = 0; i < builtin_cmd_count; i++)
510512
if (havearg(cmd_name, builtin_cmds[i].name, 0, 0, 0))
511513
return &builtin_cmds[i];

app/utils/arg.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,13 @@ enum zsv_status zsv_args_to_opts(int argc, const char *argv[], int *argc_out, co
293293
}
294294
if (processed && opts_out) {
295295
if (arg == 'R')
296-
opts_out->property_overrides |= ZSV_OPT_PROPERTY_OVERRIDE_SKIP_HEAD;
296+
opts_out->option_overrides.skip_head = 1;
297297
else if (arg == 'd')
298-
opts_out->property_overrides |= ZSV_OPT_PROPERTY_OVERRIDE_HEADER_SPAN;
298+
opts_out->option_overrides.header_row_span = 1;
299+
else if (arg == 'c')
300+
opts_out->option_overrides.max_column_count = 1;
301+
else if (arg == 'u')
302+
opts_out->option_overrides.malformed_utf8_replacement = 1;
299303
}
300304
}
301305

app/utils/prop.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,13 @@ struct zsv_file_properties zsv_cache_load_props(const char *data_filepath, struc
187187
if (tmp.stat == zsv_status_ok) {
188188
// warn if the loaded properties conflict with command-line options
189189
if (fp->skip_specified) {
190-
if (opts && (opts->property_overrides & ZSV_OPT_PROPERTY_OVERRIDE_SKIP_HEAD))
190+
if (opts && opts->option_overrides.skip_head && opts->rows_to_ignore != fp->skip)
191191
fprintf(stderr, "Warning: file property 'skip-head' overridden by command option\n");
192192
else if (opts)
193193
opts->rows_to_ignore = fp->skip;
194194
}
195195
if (fp->header_span_specified) {
196-
if (opts && (opts->property_overrides & ZSV_OPT_PROPERTY_OVERRIDE_HEADER_SPAN))
196+
if (opts && opts->option_overrides.header_row_span && opts->header_span != fp->header_span)
197197
fprintf(stderr, "Warning: file property 'header-row-span' overridden by command option\n");
198198
else if (opts)
199199
opts->header_span = fp->header_span;

include/zsv/common.h

+14-10
Original file line numberDiff line numberDiff line change
@@ -265,20 +265,24 @@ struct zsv_opts {
265265
char malformed_utf8_replace;
266266

267267
/**
268-
* `property_overrides` is a bitfield of ZSV_OPT_PROPERTY_OVERRIDE_xxx
269-
* which, if set, indicates that the value of the related option in this
270-
* zsv_opts structure should override any default value such as might be
271-
* saved in an input file property
268+
* `overrides` is a bitfield that indicates what ZSV options, if any, were
269+
* specifically set in the command invocation and is used to ensure
270+
* that option values set in the command invocation take priority over
271+
* default values, or values saved in related property values such as
272+
* .zsv/data/<filename>/props.json
272273
*
273274
* For example, if a file has a saved header row span of 2, but the
274275
* command-line arguments explicitly included `--header-row-span 3`,
275-
* then setting header_span to 3 and setting property_overrides to
276-
* ZSV_OPT_PROPERTY_OVERRIDE_HEADER_SPAN ensures that the value of 3
277-
* is always used
276+
* then setting header_span to 3 and setting overrides.header_row_span
277+
* ensures that the value of 3 is used
278278
*/
279-
#define ZSV_OPT_PROPERTY_OVERRIDE_HEADER_SPAN 1
280-
#define ZSV_OPT_PROPERTY_OVERRIDE_SKIP_HEAD 2
281-
unsigned int property_overrides;
279+
struct {
280+
unsigned char header_row_span : 1;
281+
unsigned char skip_head : 1;
282+
unsigned char max_column_count : 1;
283+
unsigned char malformed_utf8_replacement : 1;
284+
unsigned char _ : 4;
285+
} option_overrides;
282286

283287
#ifdef ZSV_EXTRAS
284288
struct {

0 commit comments

Comments
 (0)